<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 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 { endScoreSyncPaperList, endScoreSync } from "../../api";
const initFilter = { semesterId: "", examId: "", courseId: "" };

export default {
  name: "SyncPaperDialog",
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      loading: false,
      filter: { ...initFilter },
      dataList: [],
    };
  },
  methods: {
    async getList() {
      const res = await endScoreSyncPaperList(this.filter);
      this.dataList = res || [];
    },
    visibleChange() {
      this.filter = this.$objAssign(initFilter, this.course);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async toSync(row) {
      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.$message.success(`${res.success}，错误：${res.error}`);
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
