<template>
  <div class="print-plan-push-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select
              v-model="filter.semesterId"
              default-select
              @default-selected="semesterChange"
              @change="semesterChange"
            ></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
              @change="examChange"
            ></exam-select>
          </el-form-item>
          <el-form-item label="课程（代码）：" label-width="110px">
            <course-select
              ref="CourseSelect"
              v-model="filter.courseCode"
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              placeholder="课程（代码）"
              clearable
            ></course-select>
          </el-form-item>
          <el-form-item label="试卷编号：">
            <paper-number-select
              ref="PaperNumberSelect"
              v-model="filter.paperNumber"
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              :course-code="filter.courseCode"
              placeholder="试卷编号"
              clearable
            ></paper-number-select>
          </el-form-item>
          <el-form-item label="学院：">
            <college-select
              v-model="filter.orgId"
              placeholder="学院"
            ></college-select>
          </el-form-item>
          <el-form-item label="推送状态：">
            <el-select
              v-model="filter.syncStatus"
              style="width: 120px"
              placeholder="推送状态"
              clearable
            >
              <el-option
                v-for="(val, key) in PUSH_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="createTime"
              type="datetimerange"
              range-separator="至"
              start-placeholder="考试开始时间"
              end-placeholder="考试结束时间"
              value-format="timestamp"
              align="right"
              unlink-panels
            >
            </el-date-picker>
          </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="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'BatchSend')"
          icon="el-icon-top-right"
          type="primary"
          @click="toBatchPush"
        >
          批量推送
        </el-button>
        <el-button
          v-if="checkPrivilege('button', 'BatchSend')"
          type="primary"
          icon="el-icon-s-order"
          @click="toResult"
        >
          推送结果
        </el-button>
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="课程名称（代码）" min-width="180">
          <span slot-scope="scope"
            >{{ scope.row.courseName }}（{{ scope.row.courseCode }}）</span
          >
        </el-table-column>
        <el-table-column
          prop="sequence"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          min-width="100"
        ></el-table-column>
        <el-table-column prop="name" label="学院（代码）" min-width="180">
          <span slot-scope="scope"
            >{{ scope.row.collegeName }}（{{ scope.row.collegeCode }}）</span
          >
        </el-table-column>
        <el-table-column
          prop="paperType"
          label="卷型"
          width="60"
        ></el-table-column>
        <el-table-column
          prop="syncCardType"
          label="题卡类型"
          width="100"
        ></el-table-column>
        <el-table-column prop="examDate" label="考试日期" width="160">
        </el-table-column>
        <el-table-column prop="examTime" label="考试时间" width="120">
        </el-table-column>
        <el-table-column
          prop="totalSubjects"
          label="科次"
          width="60"
        ></el-table-column>
        <el-table-column
          prop="syncStatusDisplay"
          label="推送状态"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="syncUserName"
          label="推送人"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="thirdRelateId"
          label="考试ID"
          width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'Send')"
              class="btn-primary"
              type="text"
              @click="toPush(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 class="tips-info">
      <p>说明：</p>
      <p>1、印刷计划下的印刷任务，印刷完成后才允许推送；</p>
      <p>
        2、未开始阅卷工作前，可以重复推送，云阅卷会以最后一次推送的数据为准；
      </p>
      <p>
        3、课程数：指该计划下包含的课程总数量；推送成功课程数：成功推送到云阅卷系统课程数量；推送失败课程数：未成功推送到云阅卷系统课程数量；未推送课程数：未推送到云阅卷系统课程数量；
      </p>
    </div>

    <!-- PrintPlanBatchPushDialog -->
    <print-plan-batch-push-dialog
      v-if="
        checkPrivilege('link', 'Send') || checkPrivilege('button', 'BatchSend')
      "
      ref="PrintPlanBatchPushDialog"
      :courses="curSelectedCourses"
      @modified="mergePushed"
    ></print-plan-batch-push-dialog>
    <!-- DataSyncDialog -->
    <data-sync-dialog
      v-if="
        checkPrivilege('link', 'Send') || checkPrivilege('button', 'BatchSend')
      "
      ref="DataSyncDialog"
      sync-type="EXAM_PUSH"
    ></data-sync-dialog>
  </div>
</template>

<script>
import { PUSH_TYPE } from "@/constants/enumerate";
import { printPlanPushListQuery, printPlanCheckSyncMode } from "../api";
import PrintPlanBatchPushDialog from "../components/PrintPlanBatchPushDialog.vue";
import DataSyncDialog from "../../stmms/components/DataSyncDialog.vue";

export default {
  name: "print-plan-push-manage",
  components: { PrintPlanBatchPushDialog, DataSyncDialog },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        orgId: "",
        syncStatus: "",
        courseCode: "",
        paperNumber: "",
        startTime: "",
        endTime: "",
      },
      filterInfo: { semesterName: "", examName: "" },
      PUSH_TYPE,
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      loading: false,
      semesters: [],
      dataList: [],
      curPrintPlan: {},
      multipleSelection: [],
      curSelectedCourses: [],
      createTime: [],
      orgCode: "",
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      if (!this.filter.semesterId || !this.filter.examId) return;

      let datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (this.createTime) {
        datas.startTime = this.createTime[0];
        datas.endTime = this.createTime[1];
      }
      const data = await printPlanPushListQuery(datas);
      this.dataList = data.records.map((item) => {
        return {
          ...item,
          ...this.filterInfo,
        };
      });
      this.total = data.total;
    },
    toPage(page) {
      this.multipleSelection = [];
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    semesterChange(val) {
      this.filterInfo.semesterName = val ? val.name : "";
    },
    examChange(val) {
      this.filterInfo.examName = val ? val.name : "";
    },
    sizeChange(size) {
      this.size = size;
      this.toPage(1);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async toBatchPush() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择要批量推送的数据");
        return;
      }
      this.curSelectedCourses = this.multipleSelection;
      this.orgCode = await printPlanCheckSyncMode({
        list: this.curSelectedCourses,
      });
      this.$refs.PrintPlanBatchPushDialog.open();
    },
    async toPush(row) {
      this.curSelectedCourses = [row];
      this.orgCode = await printPlanCheckSyncMode({
        list: this.curSelectedCourses,
      });

      this.$refs.PrintPlanBatchPushDialog.open();
    },
    mergePushed() {
      this.toPage(this.current);
    },
    toResult() {
      this.$refs.DataSyncDialog.open();
    },
  },
};
</script>
