<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <div class="left_title">请假管理</div>
    </div>
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>姓名&nbsp;&nbsp;</span>
          <el-input
            :clearable="true"
            @clear="searchBtn"
            v-model="searchInput.keyWord"
            style="width: 180px"
            placeholder="请输入姓名"
          />
        </div>
        <div class="condition">
          <span>学号&nbsp;&nbsp;</span>
          <el-input
            :clearable="true"
            @clear="searchBtn"
            v-model="searchInput.studentId"
            style="width: 180px"
            placeholder="请输入学号"
          />
        </div>
        <el-button
          color="rgba(0, 97, 255, 1)"
          type="primary"
          class="search condition"
          @click="searchBtn"
        >
          查询</el-button
        >
        <div class="condition">
          <span>班级&nbsp;&nbsp;</span>
          <el-select
            v-model="searchInput.class"
            placeholder="请选择班级"
            style="width: 190px"
            filterable
            remote
            remote-show-suffix
            :remote-method="classDataMethod"
            @change="searchBtn"
            :clearable="userType==0?true:false"
          >
            <el-option
              v-for="i in classData"
              :label="`${i.className}`"
              :value="i.id"
            />
          </el-select>
        </div>
        <div class="condition">
          <span>状态&nbsp;&nbsp;</span>
          <el-select
            v-model="searchInput.type"
            placeholder="请选择状态"
            style="width: 160px"
            @change="searchBtn"
            :clearable="true"
          >
            <el-option label="全部" value="" />
            <el-option label="未核销" value="0" />
            <el-option label="已核销" value="1" />
          </el-select>
        </div>
        <div class="condition">
          <span>创建时间&nbsp;&nbsp;</span>
          <el-date-picker
            v-model="searchInput.createTime"
            type="daterange"
            range-separator="-"
            start-placeholder="起始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            :prefix-icon="Calendar"
            placeholder="请选择日期"
            style="width: 280px"
            @change="searchBtn"
          />
        </div>
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button
          type="primary"
          plain
          color="rgba(0, 97, 255, 1)"
          @click="editClick"
          ><span>请假配置</span></el-button
        >
      </div>
    </div>
    <div class="scroll">
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <!-- <el-table-column align="center" fixed type="selection" /> -->
          <el-table-column
            align="center"
            width="80"
            label="序号"
            type="index"
          />
          <el-table-column align="center" prop="userName" label="头像">
            <template #default="{ row }">
              <div v-viewer>
                <img style="width: 60px; height: 60px" :src="row.image" alt="" />
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="className" label="班级" />
          <el-table-column
            align="center"
            prop="xwUserName"
            label="请假人姓名"
          />
          <el-table-column align="center" prop="cardNo" label="学号" />
          <!-- <el-table-column align="center"  prop="userName" label="请假类型" /> -->
          <el-table-column
            align="center"
            width="320"
            prop="time"
            label="请假时间"
          />
          <el-table-column
            align="center"
            show-overflow-tooltip
            width="200"
            prop="reason"
            label="请假说明"
          />
          <el-table-column align="center" prop="userName" label="状态">
            <template #default="{ row }">
              {{ row.ifVerification == 0 ? "未核销" : "已核销" }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="createTime" label="创建时间" />
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="120"
          >
            <template #default="scope">
              <div class="edit" v-if="scope.row.ifVerification == 0">
                <!-- <div class="look" v-if="scope.row.ifVerification == 0" @click="editClick(scope.row)">核销</div> -->
                <el-popconfirm
                    width="220"
                    confirm-button-text="确认"
                    cancel-button-text="取消"
                    :icon="InfoFilled"
                    icon-color="#f89626"
                    title="是否需要核销此数据？"
                    @confirm="verificationClick(scope.row)"
                    @cancel="cancelVerification"
                  >
                    <template #reference>
                      <div class="del">核销</div>
                    </template>
                  </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页组件 -->
      <div class="pageSize">
        <span></span>
        <el-pagination
          background
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper, slot"
          :total="total"
          @update:current-page="handleCurrentChange"
        />
      </div>

      <!-- 请假配置 -->
      <el-dialog
        class="editDialog"
        v-model="editVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        title="请假配置"
        align-center
        width="500"
        :before-close="editCancel"
      >
        <div class="switch">
          <span>是否需要核销 </span>
          <el-switch
            style="--el-switch-on-color: rgba(41, 109, 227, 1)"
            v-model="editSwitch"
            :active-value="0"
            :inactive-value="1"
          />
        </div>
        <span class="attention"
          >注:如规则设为否，之前有预约单需要核销，自动变为已完成。</span
        >
        <div class="options">
          <el-button
            color="rgba(41, 109, 227, 1)"
            class="queding"
            type="primary"
            @click="editConfirm"
          >
            确认
          </el-button>
          <el-button @click="editCancel">取消</el-button>
        </div>
      </el-dialog>

      <div class="bgImg" v-if="bgImg">
        <el-carousel
          @click="bgImg = false"
          ref="bgImgs"
          indicator-position
          arrow="always"
          :autoplay="false"
          trigger
        >
          <el-carousel-item>
            <img :src="showImg" alt="" />
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  watch,
  nextTick,
  onBeforeMount,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { Calendar } from "@element-plus/icons-vue";
import vidiconsApi from "@/api/vidicons.js";
import { dayjs } from "element-plus";
import lodash from "lodash";
import axios from "axios";
import eds from "@/utils/eds.js";
import { useStore } from "vuex";
const store = useStore();
const api = ref("");
const router = useRouter();
// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [],
});
const userType = ref(); // 判断为班主任 还是 超级管理员
const classData = ref();
const searchInput = reactive({
  keyWord: "",
  studentId: "",
  type: "0",
  class: "",
  type: "",
  createTime: "", // 创建时间
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageCount = ref(10);
const total = ref(4); // 当前总数

// 请假配置////////////////////////////////////
const editVisible = ref(false);
const editSwitch = ref(1);
const editSwitchId = ref(1);

// 预约记录分页数据
const getList = async () => {
  // loading.value = true;
  let data = {
    currentPage: currentPage.value,
    pageCount: pageCount.value,
    cardNo: searchInput.studentId,
    name: searchInput.keyWord,
    ifVerification: searchInput.type,
    classId: searchInput.class,
  };
  if (searchInput.createTime) {
    data.startTime = searchInput.createTime[0];
    data.endTime = searchInput.createTime[1];
  }
  let res = await axios({
    method: "get",
    url: api.value + "/wanzai/api/smartAttendance/askForLeavePage",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, JSON.parse(eds.decryptDes(res.data.data)), "请假管理");
  if (res.data.code == 200) {
    loading.value = false;
    let data = JSON.parse(eds.decryptDes(res.data.data));
    data.list.forEach((i) => {
      i.time = `${dayjs(i.startTime).format("YYYY-MM-DD HH:mm:ss")} - ${dayjs(
        i.endTime
      ).format("YYYY-MM-DD HH:mm:ss")}`;
      i.createTime = dayjs(i.initiateTime).format("YYYY-MM-DD HH:mm:ss");
    });
    tableData.list = data.list;
    total.value = data.totalCount;
    // ElMessage({
    //   type: "success",
    //   showClose: true,
    //   message: res.data.message,
    //   center: true,
    // });
  } else {
    // loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 判断为班主任 还是 超级管理员
const userTypeChange = async (value) => {
  let res = await axios({
    method: "get",
    url: api.value + "/wanzai/api/smartAuthorGroup/getClassById",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: {
      userHead: sessionStorage.getItem("userhead"),
    },
  });
  // console.log(res, JSON.parse(eds.decryptDes(res.data.data)));
  if (res.data.code == 200) {
    userType.value = JSON.parse(eds.decryptDes(res.data.data)).schoolClass;
    classList()
  }else{
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 班级数据
const classList = async (val = "") => {
  let res = await axios({
    method: "get",
    url: api.value + "/wanzai/api/smartClass/getSchoolClasses",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: {
      schoolClass: userType.value,
      className: val,
    },
  });
  console.log(res, JSON.parse(eds.decryptDes(res.data.data)), "班级数据");
  let classList=JSON.parse(eds.decryptDes(res.data.data))
  if(userType.value==0){
    classData.value = classList;
  }else{
    classData.value=[{className:classList.className,id:classList.id}]
    searchInput.class=classList.id
  }
  getList()
};
const classDataMethod = async (query) => {
  if (query) {
    classList(query);
  } else {
    classList();
  }
};
// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 请假配置 （-------------------------------------------）
const editClick =async () => {
  editVisible.value = true;
  let res = await axios({
    method: "get",
    url: api.value + "/wanzai/api/smartAskForLeaveConfig/list",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
  });
  console.log(res, JSON.parse(eds.decryptDes(res.data.data)), "请假配置");
  let data = JSON.parse(eds.decryptDes(res.data.data))
  editSwitch.value=data.config
  editSwitchId.value=data.id
};
const editConfirm = async () => {
  let data = {
    "id":editSwitchId.value, //id
    "config":editSwitch.value //0：需要核销，1：不需要核销
  }
  let res = await axios({
     method: "post",
     url: api.value + "/wanzai/api/smartAskForLeaveConfig/update",
     headers: {
       token: sessionStorage.getItem("token"),
       user_head: sessionStorage.getItem("userhead"),
     },
     data: data,
   });
   console.log(res, "修改配置");
  if (res.data.code == 200) {
    getList()
    editVisible.value = false;
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
const editCancel = () => {
  editVisible.value = false;
};

// 核销按钮
const verificationClick=async (row)=>{
  let data = {
    id:row.id, //id
  }
  let res = await axios({
     method: "get",
     url: api.value + "/wanzai/api/smartAttendance/verification",
     headers: {
       token: sessionStorage.getItem("token"),
       user_head: sessionStorage.getItem("userhead"),
     },
     params: data,
   });
   console.log(res, "修改配置");
  if (res.data.code == 200) {
    getList()
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
}

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  // selectData.list = val;
};
//导出功能
// const importExcel = async () => {
//   if (searchInput.createTime == null) {
//     searchInput.createTime = "";
//   }
//   let data = new FormData();
//   data.set("car_number", searchInput.carnumber);
//   data.set("create_time", searchInput.createTime);
//   let res = await axios({
//     method: "post",
//     url: api.value + "/carBook/cinfotoExcel.action",
//     headers: {
//        token: sessionStorage.getItem("token"),
// user_head: sessionStorage.getItem("userhead"),
//     },
//     data: data,
//   });
//   // console.log(res, "导出账号");
//   if (res.data.code == 200) {
//     // const elt = document.createElement("a");
//     // elt.setAttribute(
//     //   "href",
//     //   "https://chtech.ncjti.edu.cn/carstop" + res.data.downurl
//     // );
//     // elt.setAttribute("download", "file.png");
//     // elt.style.display = "none";
//     // document.body.appendChild(elt);
//     // elt.click();
//     var downloadPath = "https://chtech.ncjti.edu.cn/carstop" + 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,
//     });
//   }
// };
// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};

// 查看表格图片 (----------------------------------------------)
const bgImg = ref(false);
const showImg = ref();
const imgClick = (row) => {
  console.log(typeof row);
  if (typeof row == "string") {
    bgImg.value = true;
    showImg.value = row;
  } else {
    bgImg.value = true;
    showImg.value = row.image;
  }
};

// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
  getList();
};

onBeforeMount(async () => {
  api.value = store.state.user.api;
  userTypeChange();
  // getList();
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  min-width: 1000px;
  width: calc(100vw - 260px);
  height: calc(100vh - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;
  box-shadow: 0px 3px 10px rgba(0, 97, 255, 0.2);

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: rgb(0, 0, 0);
    font-size: 18px;
    font-weight: 600;
    .left_title {
      height: 60px;
      line-height: 60px;
    }
  }
  .scroll {
    overflow: auto;
    flex: 1;
  }
  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;

    color: #000;

    // border-bottom: 1px solid rgb(231, 231, 231);
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      .search {
        color: #fff;
      }

      .condition {
        display: flex;
        align-items: center;
        margin: 10px 25px 10px 0;

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

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

    .gongneng {
      margin: 10px 0 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;
    }
  }

  .footer {
    width: calc(100% - 60px);
    height: calc(100% - 80px);
    margin: 10px auto 20px;

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

      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 14px;

        tr {
          // color: #000;
        }
      }
      :deep(.el-table__row):nth-child(2n) {
        // .el-table-fixed-column--left {
        //   background-color: rgba(240, 243, 247, 1);
        // }
        // .el-table-fixed-column--right {
        //   background-color: rgba(240, 243, 247, 1);
        // }
        td {
          background-color: rgba(240, 243, 247, 1);
        }
      }

      :deep(.el-table__row) {
        height: 50px;
        font-size: 14px;
        // color: #000;
        td {
          padding: 0;
          // border: 0;
        }
        &:hover {
          td {
            background-color: rgba(223, 236, 254, 1);
          }
        }
      }

      .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-table_1_column_1 {
        //   background-color: rgba(240, 243, 247, 1);
        // }
        // .el-table_1_column_2 {
        //   background-color: rgba(240, 243, 247, 1);
        // }
      }

      :deep(.edit) {
        display: flex;
        align-items: center;
        justify-content: center;
        color: rgba(111, 182, 184, 1);
      }

      :deep(.look) {
        padding: 0 10px;
        cursor: pointer;
        color: rgba(30, 125, 251, 1);
      }

      .del {
        padding: 0 10px;
        color: rgba(212, 48, 48, 1);
        cursor: pointer;
      }

      // :deep(.look):hover {
      //   color: red;
      // }
      // :deep(.del):hover {
      //   color: red;
      // }
    }
  }
  // 家长访客配置
  :deep(.editDialog) {
    //   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: 20px 30px;
      height: 300px;
      .switch {
        margin: 10px 0;
        span {
          font-size: 17px;
          margin-right: 10px;
        }
      }
      .attention {
        color: rgb(238, 190, 119);
      }
      .options {
        margin: 190px 20px 20px 0;
        width: 100%;
        display: flex;
        flex-direction: row-reverse;
        .queding {
          color: #fff;
          margin-left: 15px;
        }
      }
    }
  }

  .pageSize {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 30px;

    span {
      color: #000;
    }

    .el-pagination {
      // width: 1600px;
      :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(0, 97, 255, 0.8);
        border: 1px solid rgba(0, 97, 255, 1);
        color: rgba(0, 97, 255, 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;
      }
    }
  }

  .bgImg {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 99999999;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.8);
    :deep(.el-carousel__arrow){
      display:none;
    }
    // border-radius: 50px;
    :deep(.el-carousel__container) {
      width: 100%;
      height: calc(100vh);

      .el-carousel__item {
        display: flex;
        justify-content: center;

        img {
          height: 100%;
        }
      }
    }
  }
}

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