<template>
  <div class="ecs-data-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item prop="semesterId" label="学期：">
          <semester-select
            ref="SemesterSelect"
            v-model="filter.semesterId"
            :clearable="false"
            manual-fetch
            select-default
          ></semester-select>
        </el-form-item>
        <el-form-item prop="examTypeId" label="考试类型：">
          <exam-type-select v-model="filter.examTypeId"></exam-type-select>
        </el-form-item>
        <el-form-item label="学院：">
          <college-select v-model="filter.collegeId"></college-select>
        </el-form-item>
        <el-form-item label="考试：">
          <ecs-exam-select
            v-model="filter.examId"
            :college-id="filter.collegeId"
          ></ecs-exam-select>
        </el-form-item>
        <el-form-item label="云阅卷课程代码">
          <el-input
            v-model="filter.cloudMarkingCourseCode"
            placeholder="云阅卷课程代码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="考务数据课程代码">
          <el-input
            v-model="filter.syncCourseCode"
            placeholder="考务数据课程代码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="学号">
          <el-input
            v-model="filter.studentCode"
            placeholder="学号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="是否关联">
          <el-select v-model="filter.related" placeholder="是否关联" clearable>
            <el-option
              v-for="(val, key) in BOOLEAN_TYPE"
              :key="key"
              :value="key * 1"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toPage(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button type="primary" icon="el-icon-refresh-left" @click="toSync"
          >同步</el-button
        >
        <el-button type="success" icon="el-icon-s-order" @click="toSyncResult"
          >同步结果</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          prop="examId"
          label="云阅卷id"
          width="100"
        ></el-table-column>
        <el-table-column prop="className" label="班级"></el-table-column>
        <el-table-column prop="college" label="学院"></el-table-column>
        <el-table-column prop="name" label="学生"> </el-table-column>
        <el-table-column prop="studentCode" label="学号"> </el-table-column>
        <el-table-column prop="subjectName" label="科目"> </el-table-column>
        <el-table-column prop="subjectCode" label="科目代码"> </el-table-column>
        <el-table-column prop="totalScore" label="总分" width="80">
        </el-table-column>
        <el-table-column prop="assignScore" label="赋分" width="80">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
        </el-table-column>
        <el-table-column
          prop="relateExamData"
          label="是否关联考务数据"
          width="80"
        >
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          :page-sizes="[20, 50, 100]"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- DataTaskDialog -->
    <data-task-dialog
      ref="DataTaskDialog"
      task-type="CLOUD_MARKING_STUDENT_SCORE"
    ></data-task-dialog>
  </div>
</template>

<script>
import { ecsDataList, syncEcsData } from "../api";
import DataTaskDialog from "../components/DataTaskDialog.vue";
import { BOOLEAN_TYPE } from "@/constants/enumerate";

export default {
  name: "ecs-data-manage",
  components: { DataTaskDialog },
  data() {
    return {
      filter: {
        semesterId: "",
        examTypeId: "",
        collegeId: "",
        examId: "",
        cloudMarkingCourseCode: "",
        syncCourseCode: "",
        studentCode: "",
        related: null,
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      BOOLEAN_TYPE,
    };
  },
  async mounted() {
    await this.$refs.SemesterSelect.search();
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };

      const data = await ecsDataList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toSync() {
      if (!this.filter.collegeId) {
        this.$message.error("请选选择学院");
        return;
      }

      const result = await this.$confirm(
        `请确认下载的考务数据文件中的课程代码与导入云阅卷课程代码是否相同。相同，则点击【是】继续同步操作。不相同，则点击【否】进入课程关联管理进行对应关系导入操作。`,
        "提示",
        {
          type: "warning",
          cancelButtonText: "否",
          confirmButtonText: "是",
        }
      ).catch(() => {});
      if (result !== "confirm") {
        this.$router.push({ name: "CourseLinkManage" });
        return;
      }

      await syncEcsData({
        type: "CLOUD_MARKING_STUDENT_SCORE",
        ...this.filter,
      });
      this.$message.success("任务提交成功！");
    },
    toSyncResult() {
      this.$refs.DataTaskDialog.open();
    },
  },
};
</script>
