<template>
  <div class="score-sync">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select
              v-model="filter.semesterId"
              default-select
            ></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
            ></exam-select>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            :disabled="!filter.semesterId || !filter.examId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </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="examName" label="考试名称" min-width="200">
          <span slot-scope="scope"
            >{{ scope.row.examName | defaultFieldFilter }}（{{
              scope.row.thirdRelateId
            }}）</span
          >
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="170">
          <span slot-scope="scope">{{
            scope.row.startTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="170">
          <span slot-scope="scope">{{
            scope.row.endTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="statusStr" label="状态" width="100">
          <span slot-scope="scope">{{
            scope.row.statusStr | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="totalCourses" label="课程数" width="100">
          <span slot-scope="scope">{{
            scope.row.totalCourses | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="totalStudents" label="考生数" width="100">
          <div slot-scope="scope">
            <span v-if="scope.row.totalStudents">
              <span>{{ scope.row.totalStudents }}</span>
              <span class="color-gray-2 mlr-1">/</span>
              <span class="color-success" title="成功数">{{
                scope.row.successStudents
              }}</span>
              <span class="color-gray-2 mlr-1">/</span>
              <span class="color-danger" title="失败数">{{
                scope.row.failStudents
              }}</span>
            </span>
            <span v-else>--</span>
          </div>
        </el-table-column>
        <el-table-column prop="resultStr" label="结果" width="100">
          <span slot-scope="scope">{{
            scope.row.resultStr | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="140"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'sync')"
              class="btn-primary"
              type="text"
              @click="toSync(scope.row)"
              >同步</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'sync') && scope.row.errorFileUrl"
              class="btn-primary"
              type="text"
              @click="toExporLog(scope.row)"
              >导出日志</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { scoreLogListPage, scoreLogSync } from "../api";

export default {
  name: "score-archive",
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curImage: {},
      curRow: {},
      curPapers: [],
      downloading: false,
      syncLoading: false,
      exportLoading: false,
      loading: false,
      taskType: "",
    };
  },
  mounted() {
    // this.toPage(1);
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await scoreLogListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toSync(row) {
      if (this.syncLoading) return;
      this.syncLoading = true;
      const res = await scoreLogSync({
        semesterId: this.filter.semesterId,
        thirdRelateId: row.thirdRelateId,
        examId: this.filter.examId,
      }).catch(() => {});
      this.syncLoading = false;
      if (!res) return;
      this.$message.success("同步任务已经提交");
      this.getList();
    },
    toExporLog(row) {
      if (!row.errorFileUrl) return;
      window.open(row.errorFileUrl);
    },
  },
};
</script>
