<template>
  <div class="progress-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 ref="FilterForm" label-position="left" inline>
          <el-form-item>
            <el-select
              v-model="form.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="form.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="form.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="form.status" placeholder="进度状态" clearable>
              <el-option
                v-for="(val, key) in BOOLEAN_TYPE"
                :key="key"
                :value="key * 1"
                :label="key === '0' ? '未完成' : '已完成'"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="form.name"
              placeholder="姓名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="form.identity"
              placeholder="证件号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toPage(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="part-filter-info">
        <summary-line
          class="part-filter-info-main"
          data-type="progress"
          :exam-id="form.examId"
          v-if="form.examId"
        ></summary-line>
        <div class="part-filter-info-sub">
          <el-button
            type="primary"
            icon="icon icon-upload"
            @click="toExport"
            :loading="isDownload"
            >导出查询结果</el-button
          >
        </div>
      </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="考场名称（代码）">
        <span slot-scope="scope"
          >{{ scope.row.roomName }}({{ scope.row.roomCode }})</span
        >
      </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="courseName" label="科目（代码）">
        <span slot-scope="scope"
          >{{ scope.row.courseName }}({{ scope.row.courseCode }})</span
        >
      </el-table-column>
      <el-table-column
        prop="leftExamCount"
        label="剩余考试次数"
      ></el-table-column>
      <el-table-column prop="status" label="完成状态"></el-table-column>
      <el-table-column :context="_self" label="操作" width="120" fixed="right">
        <div slot-scope="scope">
          <el-button
            v-if="scope.row.videoCount"
            size="mini"
            type="primary"
            plain
            @click="openMonitorRecord(scope.row)"
          >
            监考回放
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        layout="prev, pager, next,total,sizes,jumper"
        :current-page="currentPage"
        :total="total"
        :page-size.sync="pageSize"
        @size-change="toPage(1)"
        @current-change="toPage"
      >
      </el-pagination>
    </div>

    <StudentTrackRecordDialog
      ref="StudentTrackRecordDialog"
      :data="selectedStudentTrack"
    />
  </div>
</template>

<script>
import {
  examBatchList,
  examActivityRoomList,
  progressDetailList,
  downloadProgressResult,
} from "@/api/invigilation";
import SummaryLine from "../common/SummaryLine";
import { downloadBlob } from "@/utils/utils";
import { mapState, mapActions, mapMutations } from "vuex";
import { BOOLEAN_TYPE } from "@/constant/constants";
import StudentTrackRecordDialog from "../../examwork/StudentManagement/StudentTrackRecordDialog";

export default {
  name: "progress-detail",
  components: {
    SummaryLine,
    StudentTrackRecordDialog,
  },
  data() {
    return {
      form: {
        examId: "",
        roomCode: "",
        courseCode: "",
        name: "",
        identity: "",
        status: null,
      },
      currentPage: 1,
      total: 0,
      pageSize: 10,
      BOOLEAN_TYPE,
      examBatchs: [],
      examActivities: [],
      examRooms: [],
      examCourses: [],
      dataList: [],
      isDownload: false,
      selectedStudent: {},
      selectedStudentTrack: null,
    };
  },
  computed: {
    ...mapState("invigilation", ["selectedExamId"]),
  },
  mounted() {
    this.initData();
  },
  methods: {
    ...mapActions("invigilation", ["updateSelectedExamId"]),
    ...mapMutations("invigilation", ["setSelectedExamId"]),
    async initData() {
      await this.getExamBatchList();
      if (!this.examBatchs.length) return;
      this.updateSelectedExamId({
        exams: this.examBatchs,
        selectedExamId: this.selectedExamId,
      });
      this.form.examId = this.selectedExamId;
      // this.form.examId = this.examBatchs[0] && this.examBatchs[0].id;
      this.getExamActivityRoomList();
      this.toPage(1);
      window.sessionStorage.removeItem("studentManageCache");
    },
    async getList() {
      const datas = {
        ...this.form,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      };

      const res = await progressDetailList(datas);

      this.dataList = res.data.data.records;
      this.total = res.data.data.total;
    },
    async toPage(page) {
      this.setSelectedExamId(this.form.examId);
      this.currentPage = page;
      await this.getList();
    },
    async getExamBatchList() {
      const user = this.$store.state.user;
      const userId = user.roleCodes.includes("INVIGILATE") ? user.id : null;
      const res = await examBatchList({ userId });
      this.examBatchs = res.data.data;
    },
    async getExamActivityRoomList() {
      if (!this.form.examId) return;
      const res = await examActivityRoomList(this.form.examId);
      this.examActivities = res.data.data.examActivitys;
      this.examRooms = res.data.data.examRooms;
      this.examCourses = res.data.data.examCourses;
    },
    examChange() {
      this.form.examActivityId = null;
      this.form.roomCode = null;
      this.form.courseCode = null;
      this.getExamActivityRoomList();
    },
    async toExport() {
      this.isDownload = true;
      const res = await downloadBlob(() => {
        return downloadProgressResult(this.form);
      }, `进度查询结果-${Date.now()}.xlsx`).catch(() => {});

      this.isDownload = false;

      if (res) {
        this.$message.success("导出成功！");
      } else {
        this.$message.error("导出失败，请重新尝试！");
      }
    },
    openMonitorRecord(row) {
      this.selectedStudentTrack = row;
      console.log(row);
      this.$refs.StudentTrackRecordDialog.openDialog();
    },
  },
};
</script>
