<template>
  <div class="content-box">
    <div class="left">
      <div class="cameratxt">退款管理</div>
    </div>
    <!-- 暖心认证 -->
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>用户名 : </span>
          <el-input
            clearable
            v-model="searchInput.username"
            class="w-50 m-2"
            placeholder="请输入用户名"
            style="width: 200px"
          />
        </div>
        <div class="condition">
          <span>交通卡类型 : </span>
          <el-select
            v-model="searchInput.type"
            class="m-2"
            placeholder="请选择交通卡类型"
            style="width: 280px"
          >
            <el-option label="优行卡" :value="0" />
            <el-option label="教师卡" :value="1" />
            <el-option label="暖心卡" :value="2" />
          </el-select>
        </div>
        <div class="condition">
          <span>手机号码 : </span>
          <el-input
            clearable
            v-model="searchInput.mobile"
            class="w-50 m-2"
            placeholder="请输入手机号码"
            style="width: 200px"
          />
        </div>
        <div class="condition">
          <span>审核结果 : </span>
          <el-select
            v-model="searchInput.vertifyState"
            class="m-2"
            placeholder="请选择审核结果"
          >
            <el-option label="待审核" :value="0" />
            <el-option label="通过" :value="1" />
            <el-option label="拒绝" :value="2" />
          </el-select>
        </div>
        <div class="condition">
          <span>申请时间 : </span>
          <el-date-picker
            v-model="searchInput.createTime"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            unlink-panels
          />
        </div>
        <div class="condition">
          <span>退款状态 : </span>
          <el-select
            v-model="searchInput.payState"
            class="m-2"
            placeholder="请选择退款状态"
          >
            <!-- <el-option label="待支付" :value="0" /> -->
            <!-- <el-option label="已支付" :value="1" /> -->
            <el-option label="退款中" :value="2" />
            <el-option label="已退款" :value="3" />
          </el-select>
        </div>
        <el-button
          style="margin-left: 20px"
          color="rgba(61, 81, 232, 1)"
          type="primary"
          class="search"
          @click="search"
          ><el-icon> <Search /> </el-icon><span>搜索</span></el-button
        >
        <el-button
          style="margin-left: 20px"
          color="rgba(61, 81, 232, 1)"
          type="primary"
          class="search"
          @click="resetInput"
          ><el-icon> <Refresh /> </el-icon><span>重置</span></el-button
        >
        <!-- <el-button
          style="margin-left: 20px"
          color="rgba(61, 81, 232, 1)"
          class="import"
          type="primary"
          @click="downLoad"
          ><img
            src="@/assets/refund.png"
            style="width: 16px; height: 14px; margin-right: 4px"
            alt=""
          />
          <span>批量退款</span></el-button
        > -->
      </div>
    </div>
    <!-- 审核 -->
    <el-dialog
      class="sendBus"
      v-model="centerDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="审核"
      align-center
      width="500"
      :before-close="auditClose"
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
        :size="formSize"
        label-position="left"
        status-icon
      >
        <el-form-item label="审核结果 :" prop="vertifyState">
          <el-select
            v-model="ruleForm.vertifyState"
            class="m-2"
            placeholder="请选择审核结果"
          >
            <el-option label="通过" value="1" />
            <el-option label="拒绝" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核理由 :" prop="remark">
          <el-input
            v-model="ruleForm.remark"
            placeholder="请输入审核理由"
            clearable
            style="width: 280px"
            :rows="4"
            type="textarea"
          />
        </el-form-item>
        <el-form-item class="options">
          <el-button class="congzhi" @click="auditClose(ruleFormRef)"
            >取消</el-button
          >
          <el-button
            color="rgba(61, 81, 232, 1)"
            class="queding"
            type="primary"
            @click="submitAdd(ruleFormRef)"
          >
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 暖心认证 -->
    <div class="MingXi">
      <div class="footers footer mingxi">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            border: 0,
          }"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column align="center" type="selection" width="60" /> -->
          <el-table-column align="center" prop="username" label="用户名" />
          <el-table-column align="center" prop="mobile" label="手机号码" />
          <!-- <el-table-column align="center" label="消费类型">
            <template #default="scope">
              <div v-if="scope.row.consumeType == 1" style="color: #67c23a">
                充值
              </div>
              <div v-if="scope.row.consumeType == 2">消费</div>
              <div v-if="scope.row.consumeType == 3">补贴</div>
              <div v-if="scope.row.consumeType == 4" style="color: red">
                退款
              </div>
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="交通卡类型">
            <template #default="scope">
              <div v-if="scope.row.type == 0">优行卡</div>
              <div v-if="scope.row.type == 1">教师卡</div>
              <div v-if="scope.row.type == 2">暖心卡</div>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="account1" label="主余额" />
          <el-table-column align="center" prop="account2" label="补贴余额" /> -->
          <el-table-column align="center" prop="account3" label="余额" />
          <el-table-column
            align="center"
            prop="createTime"
            width="100"
            label="申请时间"
          />
          <el-table-column align="center" label="状态" width="100">
            <template #default="scope">
              <div v-if="scope.row.payState == 0">待支付</div>
              <div v-if="scope.row.payState == 1">已支付</div>
              <div v-if="scope.row.payState == 3">退款中</div>
              <div v-if="scope.row.payState == 4">已退款</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="account4" label="退款金额" />
          <el-table-column align="center" label="审核结果" width="100">
            <template #default="scope">
              <div v-if="scope.row.vertifyState == 0">待审核</div>
              <div v-if="scope.row.vertifyState == 1" style="color: #67c23a">
                通过
              </div>
              <div v-if="scope.row.vertifyState == 2" style="color: red">
                拒绝
              </div>
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            align="center"
            prop="remark"
            label="审核理由"
          />
          <el-table-column
            align="center"
            prop="updateTime"
            width="100"
            label="审核时间"
          />
          <el-table-column align="center" label="操作" width="140">
            <template #default="scope">
              <el-button
                v-if="scope.row.vertifyState == 0"
                link
                type="primary"
                @click="audit(scope.row)"
              >
                <div class="look">审核</div>
              </el-button>
              <!-- <el-button link type="danger" @click="refund(scope.row)"
                ><div class="del">删除</div></el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination
          background
          v-model:page-size="pageSize"
          v-model:current-page="currentPage"
          layout="total, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
        />
        <div
          class="nowday"
          style="
            position: relative;
            width: 1200px;
            color: #000;
            top: -45px;
            font-size: 18px;
            z-index: 0;
          "
        >
          <div>
            <div style="display: inline-block; margin-left: 10px">
              <div
                style="display: inline-block; width: 600px; margin-left: 20px"
              >
                退款金额汇总 :
                <span style="color: red">{{ sumMoney.refund }}</span> 元
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch, onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import { Select, Pointer, Upload } from "@element-plus/icons-vue";
import { JSEncrypt } from "jsencrypt"; // 加密密码
import { ElMessage, ElMessageBox } from "element-plus";
import { dayjs } from "element-plus";
import axios from "axios";
import lodash from "lodash";
import adminApi from "@/api/admin.js";
import { useStore } from "vuex";
const store = useStore();
const router = useRouter();

const api = ref("");
const searchInput = reactive({
  username: "",
  type: "",
  mobile: "",
  createTime: "",
  vertifyState: "",
  payState: "",
});

// 表格数据
const tableData = reactive({
  list: [], //汇总数据
  account: "",
});

const sumMoney = reactive({
  refund: "",
});

const dialongTitle = ref("候补派车"); // 弹窗标题
const centerDialogVisible = ref(false); // 候补弹窗显示

const pageSize = ref(9);
const currentPage = ref(1); // 当前页
const total = ref(10); // 当前总数
const selectData = reactive({ list: [] }); // 多选框选择的数据

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();

const ruleForm = reactive({
  vertifyState: "",
  remark: "",
  id: "",
});
// 表单验证
const rules = reactive({
  vertifyState: [
    {
      required: true,
      message: "审核结果不能为空",
      trigger: "change",
    },
  ],
  remark: [
    {
      required: true,
      message: "审核理由不能为空",
      trigger: "blur",
    },
  ],
});

// 获取数据列表
const getList = async () => {
  let data = {};
  if (searchInput.username) {
    data.username = searchInput.username;
  }
  // 交通卡类型
  if (searchInput.type) {
    data.type = searchInput.type;
  }

  if (searchInput.mobile) {
    data.mobile = searchInput.mobile;
  }
  // 审核结果
  data.vertifyState = searchInput.vertifyState;

  data.payState = searchInput.payState;
  if (searchInput.createTime) {
    data.createStart = searchInput.createTime[0];
    data.createEnd = searchInput.createTime[1];
  }
  data.consumeType = 4;
  data.page = currentPage.value;
  data.rows = pageSize.value;
  let res = await axios({
    method: "post",
    url: api.value + "/cardlist.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    params: data,
  });
  console.log(res, "汇总");
  if (res.status == 200) {
    tableData.list = res.data.rows;
    // currentPage.value = res.data.currentPage;
    total.value = res.data.total;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.message == "token错误") {
      router.push({
        path: `/login`,
      });
    }
  }
};
const getMoney = async () => {
  let data = {};
  if (searchInput.username) {
    data.username = searchInput.username;
  }
  // 交通卡类型
  if (searchInput.type) {
    data.type = searchInput.type;
  }

  if (searchInput.mobile) {
    data.mobile = searchInput.mobile;
  }
  // 审核结果
  data.vertifyState = searchInput.vertifyState;

  data.payState = searchInput.payState;
  if (searchInput.createTime) {
    data.createStart = searchInput.createTime[0];
    data.createEnd = searchInput.createTime[1];
  }
  data.consumeType = 4;
  let res = await axios({
    method: "post",
    url: api.value + "/cardsummary.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res, "交通卡明细流水");
  if (res.data.code == 200) {
    let s = res.data.data;
    sumMoney.refund = s.refund.money;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.message == "token错误") {
      router.push({
        path: `/login`,
      });
    }
  }
};
// 搜索按钮
const search = lodash.debounce(() => {
  getList("查询成功");
}, 300);
// 重置搜索框
const resetInput = lodash.debounce(() => {
  searchInput.username = "";
  searchInput.type = "";
  searchInput.mobile = "";
  searchInput.createTime = "";
  searchInput.vertifyState = "";
  searchInput.payState = "";
  searchInput.mobile = "";
  getList();
}, 300);
// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 派车按钮
const audit = (row) => {
  console.log(row);
  centerDialogVisible.value = true;
  ruleForm.vertifyState = "";
  ruleForm.remark = "";
  ruleForm.id = row.id;
};
// 派车弹窗关闭
const auditClose = () => {
  centerDialogVisible.value = false;
  ruleFormRef.value.resetFields();
};
// 确认派车
const submitAdd = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
     let data = new FormData();
      data.set("id", ruleForm.id);
      data.set("vertifyState", ruleForm.vertifyState); //前面的key记得对应！
      data.set("remark", ruleForm.remark);
      let res = await axios({
        method: "post",
        url: api.value + "/cardvertify.action",
        headers: {
          token: sessionStorage.getItem("token"),
        },
        data: data,
      });
      // console.log(res, "添加账号");
      if (res.data.code == 200) {
        getList();
        ElMessage({
          type: "success",
          showClose: true,
          message: res.data.message,
          center: true,
        });
        centerDialogVisible.value = false;
        ruleFormRef.value.resetFields();
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 导出按钮
const downLoad = lodash.debounce(async () => {
  let data = new FormData();
  if (searchInput.car_number) {
    data.set("car_number", searchInput.car_number);
  }
  if (searchInput.yy_date) {
    data.set("yy_date", searchInput.yy_date);
  }
  if (searchInput.createTime) {
    data.set("startTime", searchInput.createTime[0]);
    data.set("endTime", searchInput.createTime[1]);
  }
  if (searchInput.route) {
    data.set("route", searchInput.route);
  }
  data.set("mobile", searchInput.mobile);
  data.set("state", searchInput.state);
  // data.set("yy_date", searchInput.yy_date);
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/brectoExcel.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.data.code == 200) {
    // const elt = document.createElement("a");
    // elt.setAttribute(
    //   "href",
    //   "https://car.meiyishuoo.com/ride-sharing" + res.data.downurl
    // );
    // elt.setAttribute("download", "file.png");
    // elt.style.display = "none";
    // document.body.appendChild(elt);
    // elt.click();
    var downloadPath =
      "https://car.meiyishuoo.com/ride-sharing" + res.data.downurl;
    console.log("获得地址数据:", downloadPath);
    var downloadLink = document.createElement("a");
    downloadLink.style.display = "none"; // 使其隐藏
    downloadLink.href = downloadPath;
    downloadLink.download = "";
    downloadLink.click();
    document.body.appendChild(downloadLink);
    document.body.removeChild(downloadLink);
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
}, 300);

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 分页
const handleCurrentChange = (value) => {
  currentPage.value = value;
  getList();
};

onBeforeMount(() => {
  api.value = store.state.user.api;
  getList();
  getMoney();
});
</script>

<style scoped lang="scss">
.content-box {
  width: calc(100% - 40px);
  height: calc(100% - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;

  .left {
    width: calc(100% - 60px);
    display: flex;
    align-items: center;
    height: 60px !important;
    margin: 0 30px;
    // padding: 10px 0;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;

    .cameratxt {
      margin-right: 15px;
      display: flex;
      align-items: center;
      height: 60px !important;
    }
  }

  :deep(.sendBus) {
    //   height: 420px;
    border-radius: 11px;

    .el-dialog__header {
      border-radius: 11px 11px 0 0;
      background: rgba(237, 241, 245, 1);
      font-weight: 600;
      margin: 0;

      .el-dialog__headerbtn {
        outline: none;
      }
    }

    .el-dialog__body {
      padding: 30px 20px 10px 20px;

      .el-form-item--feedback {
        .el-input__validateIcon {
          color: rgba(61, 81, 232, 1);
        }
      }

      .el-form-item.is-error .el-input__validateIcon {
        color: red;
      }

      .el-input {
        width: 280px;
      }

      .options {
        margin-left: 200px;
      }
    }
  }

  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    color: #000;
    border-bottom: 1px solid rgb(231, 231, 231);
    padding: 10px 0;
    // 添加员工弹窗样式

    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      //   width: 100%;
      .condition {
        display: flex;
        align-items: center;
        margin: 10px 20px 10px 0;

        :deep(.el-input .el-input__inner) {
          font-size: 16px;
        }

        span {
          margin: 0 5px 0 0;
        }
      }
    }

    .import {
      margin: 10px 0;
    }

    .gongneng {
      margin: 10px 0;
    }

    :deep(.cont) {
      width: 60%;
      margin: 20px auto;
    }

    :deep(.download) {
      display: flex;
      align-items: center;
      margin: 10px;
    }

    :deep(.download span) {
      font-size: 16px;
      margin-left: 20px;
    }

    :deep(.cont .el-button) {
      margin-left: 60px;
      margin-bottom: 30px;
    }

    :deep(.cont .accomplish) {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    :deep(.cont .accomplish .el-button) {
      width: 50%;
      margin: 0;
    }
  }

  .footerMenu {
    width: calc(100% - 60px);
    margin: 10px auto 0;
    display: flex;
    align-items: center;

    // height: 40px;
    .menuList {
      padding: 5px 12px;
      border-bottom: 2px solid transparent;
      //   color: rgba(61, 81, 232, 1);
      color: rgba(128, 128, 128, 1);
      cursor: pointer;
      //   font-size: 16px;
      font-weight: 600;
    }

    .menuListActive {
      border-bottom: 2px solid rgba(61, 81, 232, 1);
      color: rgba(61, 81, 232, 1);
      font-size: 17px;
    }
  }
  .MingXi {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: auto;
    .mingxi {
      flex: 1;
      overflow: auto;
      :deep(.el-table__header-wrapper) {
        height: 50px;

        .el-table__header {
          height: 50px;
        }
      }

      .el-table--fit {
        height: 100%;

        :deep(.el-table__row) {
          height: 50px;
        }
      }
    }
    .pagination {
      width: 100%;
      height: 70px;
      .el-pagination {
        position: relative;
        z-index: 999;
      }
    }
  }

  .footer {
    width: calc(100% - 60px);
    // height: 510px;
    margin: 0 auto;

    :deep(.el-table__header-wrapper) {
      height: 48px;

      .el-table__header {
        height: 48px;
        font-size: 16px;
        color: #000;
      }
    }

    .el-table--fit {
      height: 100%;

      :deep(.el-table__row) {
        height: 48px;
        font-size: 16px;
        color: #000;
      }

      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      :deep(.look) {
        padding: 5px 10px;
        border-radius: 40px;
        border: 0.74px solid rgba(30, 125, 251, 1);
      }
      :deep(.del) {
        padding: 5px 10px;
        border-radius: 40px;
        border: 0.74px solid #f56c6c;
      }

      // 输出链接弹窗样式
      :deep(.link .el-dialog__header) {
        text-align: left;
        font-size: 20px;
        font-weight: 600;
      }

      :deep(.link .el-dialog__body) {
        padding: 10px 20px;
        height: 100px;
        margin-bottom: 10px;
      }

      .link {
        .linkcontent {
          text-align: left;
          height: 100px;
          border: 1px solid #ccc;

          p {
            margin: 5px 10px;
          }
        }
      }

      :deep(.playVideo .el-dialog__header) {
        text-align: left;
        font-size: 20px;
        font-weight: 600;
        background-color: #ccc;
        margin-right: 0;
      }

      :deep(.playVideo .el-dialog__body) {
        height: 600px;
      }

      .el-button--primary {
        // margin-left: 5px;
      }

      :deep(.el-table__body .even) {
        background-color: #fff;
      }

      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }
    }
  }

  .footers {
    width: 96%;
    // height: 550px;
    // margin: 20px auto 20px;

    :deep(.el-table__header-wrapper) {
      height: 48px;

      .el-table__header {
        height: 48px;
        font-size: 16px;
        color: #000;
      }
    }

    .el-table--fit {
      height: 100%;

      :deep(.el-table__row) {
        height: 48px;
        font-size: 16px;
        color: #000;
      }

      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      :deep(.look) {
        padding: 5px 10px;
        border-radius: 40px;
        border: 0.74px solid rgba(30, 125, 251, 1);
      }

      // 输出链接弹窗样式
      :deep(.link .el-dialog__header) {
        text-align: left;
        font-size: 20px;
        font-weight: 600;
      }

      :deep(.link .el-dialog__body) {
        padding: 10px 20px;
        height: 100px;
        margin-bottom: 10px;
      }

      .link {
        .linkcontent {
          text-align: left;
          height: 100px;
          border: 1px solid #ccc;

          p {
            margin: 5px 10px;
          }
        }
      }

      :deep(.playVideo .el-dialog__header) {
        text-align: left;
        font-size: 20px;
        font-weight: 600;
        background-color: #ccc;
        margin-right: 0;
      }

      :deep(.playVideo .el-dialog__body) {
        height: 600px;
      }

      .el-button--primary {
        // margin-left: 5px;
      }

      :deep(.el-table__body .even) {
        background-color: #fff;
      }

      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }
    }
  }

  .el-pagination {
    // width: 1600px;
    width: calc(100% - 60px);
    height: 60px;
    margin: 0 30px;
    justify-content: flex-end;

    :deep(.el-pagination__total) {
      color: #000;
    }

    :deep(.el-pagination__goto) {
      color: #000;
    }

    :deep(.el-pagination__classifier) {
      color: #000;
    }

    :deep(.el-input__wrapper) {
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      box-shadow: none;
    }

    :deep(.el-pager li) {
      margin: 0 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.el-pager li.is-active) {
      background-color: rgba(30, 125, 251, 1);
    }

    :deep(.btn-prev) {
      margin-right: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.btn-next) {
      margin-left: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
  }
}

.el-input {
  width: 192px;
}
</style>
