<template>
  <el-dialog
    :visible.sync="modalIsShow"
    title="同步成绩"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form ref="FilterForm" label-position="left" inline label-width="0px">
      <el-form-item>
        <exam-select
          v-model="filter.examId"
          :semester-id="filter.semesterId"
          placeholder="考试"
          :clearable="false"
        ></exam-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="TableList" :data="dataList">
      <el-table-column
        type="index"
        label="序号"
        width="70"
        :index="indexMethod"
      ></el-table-column>
      <el-table-column prop="paperNumber" label="试卷编号"> </el-table-column>
      <el-table-column prop="sync" label="状态">
        <span slot-scope="scope" :class="{ 'color-success': scope.row.sync }">
          {{ scope.row.sync ? "已同步" : "未同步" }}
        </span>
      </el-table-column>
      <el-table-column class-name="action-column" label="操作" width="80">
        <template slot-scope="scope">
          <el-button class="btn-primary" type="text" @click="toSync(scope.row)"
            >同步</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import { mapState, mapMutations } from "vuex";

import { endScoreSyncPaperList, endScoreSync } from "../../api";

const initFilter = { semesterId: "", examId: "", courseId: "" };

export default {
  name: "SyncPaperDialog",
  data() {
    return {
      modalIsShow: false,
      loading: false,
      dataList: [],
    };
  },
  computed: {
    ...mapState("target", ["course"]),
    filter() {
      return this.$objAssign(initFilter, this.course);
    },
  },
  methods: {
    ...mapMutations("target", ["setCourse"]),
    async getList() {
      const res = await endScoreSyncPaperList(this.filter);
      this.dataList = res || [];
      this.updateSyncStatus();
    },
    updateSyncStatus() {
      this.dataList = this.dataList.map((item) => {
        return {
          ...item,
          sync:
            this.course.sync && this.course.paperNumber === item.paperNumber,
        };
      });
    },
    visibleChange() {},
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async toSync(row) {
      if (this.course.sync) {
        const confirm = await this.$confirm(
          `已同步期末成绩，重新同步或切换试卷编号会清空原同步成绩及学生对应平时成绩，请仔细核对是否需要重新同步！`,
          "提示",
          {
            type: "warning",
          }
        ).catch(() => {});
        if (confirm !== "confirm") return;
      }

      if (this.loading) return;
      this.loading = true;

      const datas = {
        cultureProgramId: this.course.cultureProgramId,
        courseId: this.course.courseId,
        examId: row.examId,
        paperNumber: row.paperNumber,
      };
      const res = await endScoreSync(datas).catch(() => {});
      this.loading = false;

      if (!res) return;

      this.setCourse({
        examId: row.examId,
        paperNumber: row.paperNumber,
        sync: true,
      });
      this.updateSyncStatus();
      this.$message.success(`${res.success}，错误：${res.error}`);
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
