<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>
        <el-form-item label="使用学期：">
          <el-select
            v-model.trim="filter.semesterId"
            placeholder="请选择使用学期"
            clearable
            filterable
          >
            <el-option
              v-for="item in semesters"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试：">
          <exam-select
            v-model="filter.examId"
            :semester-id="filter.semesterId"
          ></exam-select>
        </el-form-item>
        <el-form-item label="印刷计划：">
          <print-plan-select
            v-model.trim="filter.printPlanId"
            placeholder="印刷计划"
            clearable
          ></print-plan-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          :disabled="loading"
          @click="toBatchPush"
        >
          批量推送
        </el-button>
        <el-button icon="el-icon-circle-plus-outline" @click="toMergePush">
          合并推送印刷计划
        </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"
          :selectable="row => row.canPush"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="印刷计划"
          min-width="180"
        ></el-table-column>
        <el-table-column prop="examStartTime" label="考试开始时间" width="170">
          <span slot-scope="scope">{{
            scope.row.examStartTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="examEndTime" label="考试结束时间" width="170">
          <span slot-scope="scope">{{
            scope.row.examEndTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="status" label="计划状态" width="100">
          <span slot-scope="scope">
            {{ scope.row.status | printPlanStatusFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="totalGates" label="总门次" width="70">
        </el-table-column>
        <el-table-column prop="totalSubjects" label="总科次" width="70">
        </el-table-column>
        <el-table-column prop="totalPackages" label="总卷袋数" width="80">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          prop="createName"
          label="创建人"
          min-width="80"
        ></el-table-column>
        <el-table-column
          prop="thirdRelateName"
          label="考试名称"
          min-width="180"
        >
          <span slot-scope="scope">{{
            scope.row.thirdRelateName | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="thirdRelateId" label="考试ID" min-width="80">
          <span slot-scope="scope">{{
            scope.row.thirdRelateId | defaultFieldFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="syncStatus" label="同步状态" width="100">
          <span slot-scope="scope">{{
            scope.row.syncStatus | syncPrintStatusFilter
          }}</span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toPreview(scope.row)"
              >查看</el-button
            >
            <el-button
              v-if="scope.row.canPush"
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toPush(scope.row)"
              >推送</el-button
            >
          </template>
        </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"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- MergePushDialog -->
    <merge-push-dialog
      ref="MergePushDialog"
      :ids="multipleSelection"
      @modified="mergePushed"
    ></merge-push-dialog>
    <!-- ModifyPrintPlan -->
    <modify-print-plan
      ref="ModifyPrintPlan"
      :instance="curPrintPlan"
      edit-type="PREVIEW"
    ></modify-print-plan>
  </div>
</template>

<script>
import {
  printPlanPushListQuery,
  printPlanBatchPush,
  printPlanPush
} from "../api";
import { examSemesterList } from "../../base/api";
import MergePushDialog from "../components/MergePushDialog";
import ModifyPrintPlan from "../../print/components/ModifyPrintPlan";

export default {
  name: "print-plan-push-manage",
  components: { MergePushDialog, ModifyPrintPlan },
  data() {
    return {
      filter: {
        semesterId: "",
        printPlanId: "",
        examId: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      loading: false,
      semesters: [],
      dataList: [],
      curPrintPlan: {},
      multipleSelection: []
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      await this.getSemesters();
      await this.getList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await printPlanPushListQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.multipleSelection = [];
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    async getSemesters() {
      const res = await examSemesterList({});
      this.semesters = res || [];
      if (!this.semesters.length) return;

      let curSemester = this.semesters.find(item => item.inUsed);
      curSemester = curSemester || this.semesters[0];
      this.filter.semesterId = curSemester.id;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.id);
    },
    async toBatchPush() {
      if (this.loading) return;
      if (!this.multipleSelection.length) {
        this.$message.error("请选择要批量推送的数据");
        return;
      }
      const result = await this.$confirm("确定要推送选中的计划吗？", "提示", {
        type: "warning"
      }).catch(() => {});
      if (result !== "confirm") return;

      this.loading = true;
      const data = await printPlanBatchPush(
        this.multipleSelection
      ).catch(() => {});
      this.loading = false;
      if (!data) return;

      this.$message.success("提交成功！");
      this.toPage(this.current);
    },
    toMergePush() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择要合并推送的数据");
        return;
      }
      this.$refs.MergePushDialog.open();
    },
    mergePushed() {
      this.toPage(this.current);
    },
    toPreview(row) {
      this.curPrintPlan = row;
      this.$refs.ModifyPrintPlan.open();
    },
    async toPush(row) {
      if (this.loading) return;
      const result = await this.$confirm("确定要推送当前计划吗？", "提示", {
        type: "warning"
      }).catch(() => {});
      if (result !== "confirm") return;

      this.loading = true;
      const data = await printPlanPush({
        printPlanId: row.id,
        thirdRelateId: row.thirdRelateId
      }).catch(() => {});
      this.loading = false;
      if (!data) return;

      this.$message.success("提交成功！");
      this.toPage(this.current);
    }
  }
};
</script>
