<template>
  <div class="print-plan-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')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="search"
          ></secp-select>
          <el-form-item label="印刷计划：">
            <print-plan-select
              v-model.trim="filter.printPlanIdList"
              placeholder="印刷计划"
              multiple
              clearable
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              style="width: 300px"
            ></print-plan-select>
          </el-form-item>
          <el-form-item label="计划状态：">
            <el-select
              v-model="filter.status"
              style="width: 142px"
              placeholder="计划状态"
              clearable
            >
              <el-option
                v-for="(val, key) in PRINT_PLAN_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间：">
            <el-date-picker
              v-model="createTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              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"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'add')"
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="modalIsShow = true"
        >
          新增
        </el-button>
      </div>
    </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="name"
          label="印刷计划"
          min-width="200"
        ></el-table-column>
        <!-- <el-table-column
          prop="semesterName"
          label="学期"
          min-width="210"
        ></el-table-column>
        <el-table-column
          prop="examName"
          label="考试"
          min-width="200"
        ></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="createName"
          label="创建人"
          min-width="100"
        ></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
          class-name="action-column"
          label="操作"
          width="140"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'preview')"
              class="btn-primary"
              type="text"
              @click="toPreview(scope.row)"
              >查看</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              v-if="
                (isSchoolAdmin || scope.row.createId === curUserId) &&
                scope.row.status === 'NEW' &&
                checkPrivilege('link', 'edit')
              "
              >编辑</el-button
            >
            <el-button
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              v-if="
                (isSchoolAdmin || scope.row.createId === curUserId) &&
                scope.row.status === 'NEW' &&
                checkPrivilege('link', 'delete')
              "
              >删除</el-button
            >
            <el-button
              class="btn-danger"
              type="text"
              @click="toFinish(scope.row)"
              v-if="
                scope.row.status !== 'END' && checkPrivilege('link', 'Finish')
              "
              >结束</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>

    <!-- ModifyPrintPlan -->
    <modify-print-plan
      v-if="checkPrivilege('link', 'edit') || checkPrivilege('button', 'add')"
      ref="ModifyPrintPlan"
      :instance="curPrintPlan"
      :edit-type="editType"
      @modified="getList"
    ></modify-print-plan>
    <!-- PrintPlanDetail -->
    <print-plan-detail
      v-if="checkPrivilege('link', 'preview')"
      ref="PrintPlanDetail"
      :plan="curPrintPlan"
    ></print-plan-detail>
    <!-- PrintPlanBkDetail -->
    <print-plan-bk-detail
      v-if="checkPrivilege('link', 'preview')"
      ref="PrintPlanBkDetail"
      :plan="curPrintPlan"
    ></print-plan-bk-detail>
    <!-- print-type-dialog -->
    <el-dialog
      :visible.sync="modalIsShow"
      title="新建印刷计划"
      top="10vh"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <div class="big-btns">
        <el-button
          v-for="(val, key) in PRINT_PLAN_TYPE"
          :key="key"
          type="primary"
          @click="toAdd(key)"
          >{{ val }}</el-button
        >
      </div>
      <div slot="footer"></div>
    </el-dialog>
  </div>
</template>

<script>
import { printPlanListPage, removePrintPlan, finishPrintPlan } from "../api";
import { PRINT_PLAN_STATUS, PRINT_PLAN_TYPE } from "@/constants/enumerate";
import pickerOptions from "@/constants/datePickerOptions";
import ModifyPrintPlan from "../components/ModifyPrintPlan";
import PrintPlanDetail from "../components/PrintPlanDetail.vue";
import PrintPlanBkDetail from "../components/PrintPlanBkDetail.vue";

export default {
  name: "print-plan-manage",
  components: { ModifyPrintPlan, PrintPlanDetail, PrintPlanBkDetail },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        printPlanIdList: [],
        status: "",
        startTime: "",
        endTime: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curPrintPlan: {},
      editType: "ADD",
      PRINT_PLAN_STATUS,
      curUserId: this.$ls.get("user", { id: "" }).id,
      isSchoolAdmin: this.$ls
        .get("user", { role: [] })
        .role.includes("SCHOOL_ADMIN"),
      modalIsShow: false,
      PRINT_PLAN_TYPE,
      // date-picker
      createTime: [],
      pickerOptions,
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const 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 printPlanListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toAdd(category) {
      this.modalIsShow = false;
      this.curPrintPlan = { category };
      this.editType = "ADD";
      this.$refs.ModifyPrintPlan.open();
    },
    toEdit(row) {
      this.curPrintPlan = row;
      this.editType = "EDIT";
      this.$refs.ModifyPrintPlan.open();
    },
    toPreview(row) {
      this.curPrintPlan = row;
      if (row.category === "FORMAL" || !row.category) {
        this.$refs.PrintPlanDetail.open();
      } else {
        this.$refs.PrintPlanBkDetail.open();
      }
    },
    toDelete(row) {
      this.$confirm(`确定要删除印刷计划【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await removePrintPlan(row.id);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
    toFinish(row) {
      this.$confirm(`结束后状态无法回退，是否继续操作？`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await finishPrintPlan(row.id);
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
