<template>
  <div class="warning-manage">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>预警提醒</h1></div>
      <div class="part-box-head-right" v-if="curExamRoomName">
        <span>考场名称：{{ curExamRoomName }}</span>
      </div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form ref="FilterForm" label-position="left" inline>
          <el-form-item>
            <el-select
              v-model="filter.examId"
              placeholder="请选择批次"
              @change="examChange"
            >
              <el-option
                v-for="item in examBatchs"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.roomCode"
              placeholder="请选择考场"
              @change="roomChange"
              clearable
            >
              <el-option
                v-for="item in examRooms"
                :key="item.id"
                :value="item.roomCode"
                :label="item.roomName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.courseCode"
              placeholder="请选择科目"
              clearable
            >
              <el-option
                v-for="item in examCourses"
                :key="item.courseCode"
                :value="item.courseCode"
                :label="item.courseName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.approveStatus"
              placeholder="审阅状态"
              clearable
            >
              <el-option
                v-for="(val, key) in APPROVE_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.name"
              placeholder="姓名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.identity"
              placeholder="证件号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="陌生人脸">
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.minMultipleFaceCount"
              placeholder="下限"
              :controls="false"
            ></el-input-number>
            <span class="line-split">-</span>
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.maxMultipleFaceCount"
              placeholder="上限"
              :controls="false"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="异常处理">
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.minExceptionCount"
              placeholder="下限"
              :controls="false"
            ></el-input-number>
            <span class="line-split">-</span>
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.maxExceptionCount"
              placeholder="上限"
              :controls="false"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="预警数">
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.minWarningCount"
              placeholder="下限"
              :controls="false"
            ></el-input-number>
            <span class="line-split">-</span>
            <el-input-number
              style="width: 52px;"
              v-model.trim="filter.maxWarningCount"
              placeholder="上限"
              :controls="false"
            ></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toSearch">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-clean" @click="cleanUnread"
            >清除未阅</el-button
          >
          <!-- <el-button type="danger" icon="icon icon-warning" @click="batchAction"
            >批量处理违纪</el-button
          > -->
        </div>
      </div>
    </div>

    <el-table
      ref="TableList"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column prop="examName" label="批次"></el-table-column>
      <el-table-column prop="examActivityCode" label="场次"></el-table-column>
      <el-table-column prop="roomName" label="考场"> </el-table-column>
      <el-table-column prop="examId" label="考试ID"></el-table-column>
      <el-table-column prop="identity" label="证件号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="courseName" label="科目名称"></el-table-column>
      <el-table-column prop="courseCode" label="科目代码"> </el-table-column>
      <el-table-column
        prop="multipleFaceCount"
        label="陌生人脸"
      ></el-table-column>
      <el-table-column prop="exceptionCount" label="异常处理"></el-table-column>
      <el-table-column prop="warningCount" label="预警数"></el-table-column>
      <el-table-column prop="breachStatus" label="是否违纪">
        <template slot-scope="scope">
          <span :class="{ 'color-danger': !scope.row.breachStatus }">
            {{ !scope.row.breachStatus ? "违纪" : "正常" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="approveStatus" label="审阅状态">
        <template slot-scope="scope">
          <span :class="{ 'color-danger': !scope.row.approveStatus }">
            {{ scope.row.approveStatus | zeroOneApproveStatusFilter }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button
            class="btn-table-icon"
            type="primary"
            icon="icon icon-view"
            @click="toDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        layout="prev, pager, next,total,sizes,jumper"
        :current-page="current"
        :total="total"
        :page-size.sync="size"
        @size-change="toPage(1)"
        @current-change="toPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  examBatchList,
  examActivityRoomList,
  invigilationWarningList,
  clearInvigilationFilterUnreadWarningList,
} from "@/api/invigilation";
import { APPROVE_STATUS } from "@/constant/constants";
import { mapState, mapActions, mapMutations } from "vuex";

export default {
  name: "WarningManage",
  data() {
    return {
      filter: {
        examId: null,
        roomCode: null,
        courseCode: null,
        approveStatus: null,
        name: "",
        identity: "",
        maxMultipleFaceCount: undefined,
        minMultipleFaceCount: undefined,
        maxExceptionCount: undefined,
        minExceptionCount: undefined,
        maxWarningCount: undefined,
        minWarningCount: undefined,
      },
      curExamRoomName: "",
      APPROVE_STATUS,
      multipleSelection: [],
      current: 1,
      total: 0,
      size: 10,
      examBatchs: [],
      examActivities: [],
      examRooms: [],
      examCourses: [],
      dataList: [],
    };
  },
  computed: {
    ...mapState("invigilation", ["selectedExamId"]),
    user() {
      return this.$store.state.user;
    },
    IS_INVIGILATE() {
      return this.user.roleCodes.includes("INVIGILATE");
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    ...mapActions("invigilation", [
      "updateDetailIds",
      "updateSelectedExamId",
      "fetchWarningManageCount",
    ]),
    ...mapMutations("invigilation", ["setDetailIds", "setSelectedExamId"]),
    async initData() {
      await this.getExamBatchList();
      if (!this.examBatchs.length) return;
      this.updateSelectedExamId({
        exams: this.examBatchs,
        selectedExamId: this.selectedExamId,
      });
      this.filter.examId = this.selectedExamId;
      // this.filter.examId = this.examBatchs[0] && this.examBatchs[0].id;
      this.toSearch();
      this.getExamActivityRoomList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };

      const res = await invigilationWarningList(datas);

      this.dataList = res.data.data.records;
      this.total = res.data.data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toSearch() {
      this.setSelectedExamId(this.filter.examId);
      this.current = 1;
      await this.getList();
      if (this.total > this.size) {
        this.updateDetailIds({
          filterData: this.filter,
          fetchFunc: invigilationWarningList,
        });
      } else {
        const ids = this.dataList.map((item) => item.examRecordId);
        this.setDetailIds([...new Set(ids)]);
      }
    },
    async getExamBatchList() {
      const userId = this.IS_INVIGILATE ? this.user.id : null;
      const res = await examBatchList(userId);
      this.examBatchs = res.data.data;
    },
    async getExamActivityRoomList() {
      if (!this.filter.examId) return;
      const res = await examActivityRoomList(this.filter.examId);
      this.examActivities = res.data.data.examActivitys;
      this.examRooms = res.data.data.examRooms;
      this.examCourses = res.data.data.examCourses;
    },
    examChange() {
      this.filter.examActivityId = null;
      this.filter.roomCode = null;
      this.filter.courseCode = null;
      this.getExamActivityRoomList();
    },
    roomChange() {
      if (!this.filter.roomCode) {
        this.curExamRoomName = "";
        return;
      }
      const room = this.examRooms.find(
        (item) => item.roomCode === this.filter.roomCode
      );
      this.curExamRoomName = room.roomName;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async cleanUnread() {
      await clearInvigilationFilterUnreadWarningList(this.filter);
      this.$message.success("操作成功！");
      this.getList();

      const user = this.$store.state.user;
      const datas = user.roleCodes.includes("INVIGILATE")
        ? { userId: user.id }
        : {};
      this.fetchWarningManageCount(datas);
    },
    toDetail(row) {
      const router = this.IS_INVIGILATE
        ? "WarningDetail"
        : "InvigilationWarningDetail";
      this.$router.push({
        name: router,
        params: { recordId: row.examRecordId },
      });
    },
  },
  beforeRouteLeave(to, from, next) {
    if (
      to.name !== "WarningDetail" &&
      to.name !== "InvigilationWarningDetail"
    ) {
      this.$destroy();
    }
    next();
  },
  beforeRouteEnter(to, from, next) {
    if (from.name === "WarningDetail") {
      next((vm) => vm.$nextTick(() => vm.getList()));
    } else {
      next();
    }
  },
};
</script>
