<template>
  <div class="invigilation-detail">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>监考明细管理</h1></div>
    </div>

    <div class="part-filter">
      <div class="part-filter-form">
        <el-form
          class="part-box-filter-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.examActivityId"
              placeholder="请选择场次"
              clearable
            >
              <el-option
                v-for="item in examActivities"
                :key="item.id"
                :value="item.id"
                :label="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.roomCode"
              placeholder="请选择考场"
              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-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>
            <el-select
              v-model="filter.finishType"
              placeholder="交卷方式"
              clearable
            >
              <el-option
                v-for="(val, key) in STUDENT_FINISH_EXAM_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filter.status" placeholder="筛选状态" clearable>
              <el-option
                v-for="(val, key) in STUDENT_ONLINE_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.breachStatus"
              placeholder="违纪状态"
              clearable
            >
              <el-option
                v-for="(val, key) in STUDENT_BEHAVIOR_STATUS"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="陌生人脸" v-if="showAdvancedFilter">
            <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="异常处理" v-if="showAdvancedFilter">
            <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="预警数" v-if="showAdvancedFilter">
            <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-button type="primary" @click="changeFilter">{{
              showAdvancedFilter ? "隐藏高级查询" : "高级查询"
            }}</el-button>
            <el-button type="primary" :loading="isDownload" @click="toExport"
              >导出</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="part-filter-info">
        <summary-line
          class="part-filter-info-main"
          data-type="complete"
          :exam-id="filter.examId"
        ></summary-line>
      </div>
    </div>

    <el-table ref="TableList" :data="dataList">
      <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="mobileNumber" label="联系电话"></el-table-column>
      <el-table-column prop="courseCode" label="科目（代码）">
      </el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="finishType" label="交卷方式">
        <template slot-scope="scope">
          <div>{{ STUDENT_FINISH_EXAM_TYPE[scope.row.finishType] }}</div>
        </template>
      </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 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,
  invigilationHistoryList,
  exportInvigilationHistory,
} from "@/api/invigilation";
import { downloadBlob } from "@/utils/utils";
import {
  STUDENT_FINISH_EXAM_TYPE,
  STUDENT_ONLINE_STATUS,
  STUDENT_BEHAVIOR_STATUS,
} from "@/constant/constants";
import SummaryLine from "../common/SummaryLine";
import { mapState, mapActions, mapMutations } from "vuex";

export default {
  name: "InvigilationDetail",
  components: { SummaryLine },
  data() {
    return {
      filter: {
        examId: "",
        examActivityId: null,
        roomCode: null,
        courseCode: null,
        auditStatus: null,
        name: "",
        identity: "",
        maxMultipleFaceCount: undefined,
        minMultipleFaceCount: undefined,
        maxExceptionCount: undefined,
        minExceptionCount: undefined,
        maxWarningCount: undefined,
        minWarningCount: undefined,
      },
      STUDENT_FINISH_EXAM_TYPE,
      STUDENT_ONLINE_STATUS,
      STUDENT_BEHAVIOR_STATUS,
      showAdvancedFilter: false,
      current: 1,
      total: 0,
      size: 10,
      examBatchs: [],
      examActivities: [],
      examRooms: [],
      examCourses: [],
      dataList: [],
      isDownload: false,
    };
  },
  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"]),
    ...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 invigilationHistoryList(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: invigilationHistoryList,
        });
      } else {
        const ids = this.dataList.map((item) => item.examRecordId);
        this.setDetailIds([...new Set(ids)]);
      }
    },
    async toExport() {
      this.isDownload = true;
      const res = await downloadBlob(() => {
        return exportInvigilationHistory(this.filter);
      }).catch(() => {});

      this.isDownload = false;

      if (res) {
        this.$message.success("导出成功！");
      } else {
        this.$message.error("导出失败，请重新尝试！");
      }
    },
    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();
    },
    changeFilter() {
      this.showAdvancedFilter = !this.showAdvancedFilter;
      if (!this.showAdvancedFilter) {
        this.filter.maxMultipleFaceCount = null;
        this.filter.minMultipleFaceCount = null;
        this.filter.maxExceptionCount = null;
        this.filter.minExceptionCount = null;
        this.filter.maxWarningCount = null;
        this.filter.minWarningCount = null;
      }
    },
    toDetail(row) {
      const router = this.IS_INVIGILATE
        ? "WarningDetail"
        : "InvigilationWarningDetail";
      this.$router.push({
        name: router,
        params: { recordId: row.examRecordId },
      });
    },
  },
};
</script>
