<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><UserFilled /></el-icon> -->
      <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-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.type"
            class="m-2"
            placeholder="请选择交通卡类型"
            style="width: 200px"
          >
            <el-option label="优行卡" :value="0" />
            <el-option label="教师卡" :value="1" />
            <el-option label="暖心卡" :value="2" />
          </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
        >
      </div>
      <!-- <div class="gongneng">
        <el-button
          color="rgba(61, 81, 232, 1)"
          class="import"
          type="primary"
          @click="subsidyClick"
        >
          一键发放补助</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="editClose"
    >
      <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="username">
          <el-input
            disabled
            v-model="ruleForm.username"
            placeholder="请输入用户名"
            clearable
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="手机号码 :" prop="mobile">
          <el-input
            v-model="ruleForm.mobile"
            disabled
            placeholder="请输入手机号码"
            clearable
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="交通卡类型 :" prop="type">
          <el-select
            v-model="ruleForm.type"
            disabled
            class="m-2"
            placeholder="请选择交通卡类型"
            style="width: 280px"
          >
            <el-option label="优行卡" :value="0" />
            <el-option label="教师卡" :value="1" />
            <el-option label="暖心卡" :value="2" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="充值余额 :" prop="account1">
          <el-input
            v-model="ruleForm.account1"
            placeholder="请输入主余额"
            clearable
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="补贴余额 :" prop="account2">
          <el-input
            v-model="ruleForm.account2"
            placeholder="请输入补贴余额"
            clearable
            style="width: 280px"
          />
        </el-form-item> -->
        <el-form-item label="余额 :" prop="account1">
          <el-input
            v-model="ruleForm.account1"
            placeholder="请输入余额"
            clearable
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item class="options">
          <el-button class="congzhi" @click="editClose(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>

    <el-dialog
      class="sendBus"
      v-model="subsidyDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="一键发放补助"
      align-center
      width="500"
      :before-close="subsidyClose"
    >
      <el-form
        ref="subsidyRef"
        :model="subsidyForm"
        :rules="subsidyRules"
        label-width="100px"
        class="demo-ruleForm"
        :size="formSize"
        label-position="left"
        status-icon
      >
        <el-form-item label="交通卡类型 :" prop="type">
          <el-select
            v-model="subsidyForm.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>
        </el-form-item>
        <el-form-item label="补贴余额 :" prop="money">
          <el-input
            v-model="subsidyForm.money"
            placeholder="请输入补贴余额"
            clearable
            style="width: 280px"
          />
        </el-form-item>
        <div class="title"> 注 : 补贴金额直接发放到所有教师身份可用余额中（教师卡）</div>
        <div class="title" style="margin-left: 21px;">    补贴金额直接发放到所有贫困生身份可用余额中（暖心卡）</div>
        <el-form-item class="options">
          <el-button class="congzhi" @click="subsidyClose(subsidyRef)"
            >取消</el-button
          >
          <el-button
            color="rgba(61, 81, 232, 1)"
            class="queding"
            type="primary"
            @click="subsidyAdd(subsidyRef)"
          >
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="MingXi">
      <div class="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="80" /> -->
          <el-table-column align="center" prop="username" label="用户名" />
          <el-table-column align="center" prop="mobile" label="手机号码" />
          <el-table-column align="center" prop="type" 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="account1" label="余额" />
          <el-table-column align="center" label="操作" width="150">
            <template #default="scope">
              <el-button link type="primary" @click="info(scope.row)">
                <div class="look">编辑</div>
              </el-button>
              <!-- <el-button link type="danger" @click="del(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.mainBalance }}</span>
                元 ; 补贴余额汇总:
                <span style="color: red">{{ sumMoney.allowanceBalance }}</span> 元 ; -->
                余额汇总:
                <span style="color: red">{{ sumMoney.freeBalance }}</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 { useStore } from "vuex";
const store = useStore();
const router = useRouter();

const api = ref("");
// 明细列表搜索框数据
const searchInput = reactive({
  username: "",
  mobile: "",
  type: "", // 0优先卡,1教师卡,2暖心卡
});

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

const sumMoney = reactive({
  allowanceBalance: "",
  freeBalance: "",
  mainBalance: "",
});

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

const centerDialogVisible = ref(false); // 候补弹窗显示
// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  username: "",
  mobile: "",
  type: "",
  account1: "", // 充值余额
  account2: "", // 补贴余额
  account3: "", // 可用余额
  id: "",
});
// 表单验证
const rules = reactive({
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "change",
    },
  ],
  mobile: [
    {
      required: true,
      message: "手机号码不能为空",
      trigger: "blur",
    },
  ],
  type: [
    {
      required: true,
      message: "交通卡类型不能为空",
      trigger: "change",
    },
  ],
  account1: [
    {
      required: true,
      message: "余额不能为空",
      trigger: "blur",
    },
    {
      pattern: /^(0|([1-9]\d*)(\.\d{1,2})?)$|^(0\.\d{1,2})$/,
      message: "金额输入有误，请重新输入",
      trigger: "blur",
    },
  ],
  account2: [
    {
      required: true,
      message: "补贴余额不能为空",
      trigger: "change",
    },
    {
      pattern: /^(0|([1-9]\d*)(\.\d{1,2})?)$|^(0\.\d{1,2})$/,
      message: "金额输入有误，请重新输入",
      trigger: "blur",
    },
  ],
  account3: [
    {
      required: true,
      message: "可用余额不能为空",
      trigger: "change",
    },
    {
      pattern: /^(0|([1-9]\d*)(\.\d{1,2})?)$|^(0\.\d{1,2})$/,
      message: "金额输入有误，请重新输入",
      trigger: "blur",
    },
  ],
});

// 一键发放补贴
const subsidyDialogVisible = ref(false);
const subsidyRef = ref();
const subsidyForm = reactive({
  type: "",
  money: "", // 补贴余额
});
// 表单验证
const subsidyRules = reactive({
  type: [
    {
      required: true,
      message: "交通卡类型不能为空",
      trigger: "change",
    },
  ],
  money: [
    {
      required: true,
      message: "补贴余额不能为空",
      trigger: "change",
    },
    {
      pattern: /^(0|([1-9]\d*)(\.\d{1,2})?)$|^(0\.\d{1,2})$/,
      message: "金额输入有误，请重新输入",
      trigger: "blur",
    },
  ],
});

// 获取数据列表
const getList = async (message) => {
  let data = {
    page: currentPage.value,
    rows: pageSize.value,
    username: searchInput.username,
    mobile: searchInput.mobile,
    type: searchInput.type, // 0优先卡,1教师卡,2暖心卡
  };

  let res = await axios({
    method: "post",
    url: api.value + "/tAppcardList.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    params: data,
  });
  console.log(res, "交通卡管理");
  if (res.status == 200) {
    tableData.list = res.data.rows;
    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 (message) => {
  let data = {
    username: searchInput.username,
    mobile: searchInput.mobile,
    type: searchInput.type, // 0优先卡,1教师卡,2暖心卡
  };

  let res = await axios({
    method: "post",
    url: api.value + "/tAppsummary.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res, "交通卡余额");
  if (res.data.code == 200) {
    let s = res.data.data;
    sumMoney.allowanceBalance = s.allowanceBalance.money.toFixed(2);
    sumMoney.freeBalance = s.freeBalance.money.toFixed(2);
    sumMoney.mainBalance = s.mainBalance.money.toFixed(2);
  } 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();
  getMoney();
}, 300);
// 重置搜索框
const resetInput = lodash.debounce(() => {
  searchInput.username = "";
  searchInput.mobile = "";
  searchInput.type = "";
  getList();
  getMoney();
}, 300);
// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 派车按钮
const info = (row) => {
  console.log(row);
  centerDialogVisible.value = true;
  ruleForm.username = row.username;
  ruleForm.mobile = row.mobile;
  ruleForm.type = row.type;
  ruleForm.account1 = row.account1;
  // ruleForm.account2 = row.account2;
  // ruleForm.account3 = row.account3;
  ruleForm.id = row.id;
};

const editClose = () => {
  centerDialogVisible.value = false;
  ruleFormRef.value.resetFields();
};
const submitAdd = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        id: ruleForm.id,
        account1: ruleForm.account1,
        // account2: ruleForm.account2,
        // account3: ruleForm.account3,
      };
      let res = await axios({
        method: "post",
        url: api.value + "/tAppupdateTempUser.action",
        headers: {
          token: sessionStorage.getItem("token"),
        },
        data: data,
      });
      // console.log(res, "添加账号");
      if (res.data.code == 200) {
        getList();
        getMoney()
        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 subsidyClick = () => {
  subsidyDialogVisible.value = true;
  subsidyForm.type = "";
  subsidyForm.money = "";
};
const subsidyClose = () => {
  subsidyDialogVisible.value = false;
  subsidyRef.value.resetFields();
};
const subsidyAdd = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = new FormData();
      data.set("type", subsidyForm.type);
      data.set("money", subsidyForm.money); 
      let res = await axios({
        method: "post",
        url: api.value + "/tAppsubside.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,
        });
        subsidyDialogVisible.value = false;
        subsidyRef.value.resetFields();
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 表格斑马纹颜色修改
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;
      }
      .title {
        color: #e9331f;
        font-size: 13px;
      }
      .options {
        margin: 20px 0 10px 200px;
      }
    }
  }

  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;
    color: #000;
    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;
    }

    :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 rgb(251, 30, 30);
      }

      // 输出链接弹窗样式
      :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: 3px 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>
