<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')">
          <el-form-item label="学期：">
            <semester-select
              v-model.trim="filter.semesterId"
              placeholder="学期"
              clearable
            ></semester-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.printPlanIdList"
              placeholder="印刷计划"
              multiple
              clearable
            ></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="toAdd"
        >
          新建印刷计划
        </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="semesterName"
          label="学期"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="examName"
          label="考试"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="name"
          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="totalGates" label="总门次" width="80">
        </el-table-column>
        <el-table-column prop="totalSubjects" label="总科次" width="80">
        </el-table-column>
        <el-table-column prop="totalPackages" label="总卷袋数" 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
          prop="createName"
          label="创建人"
          min-width="100"
        ></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="
                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="
                scope.row.createId === curUserId &&
                  scope.row.status === 'NEW' &&
                  checkPrivilege('link', 'delete')
              "
              >删除</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>

    <!-- ModifyPrintPlan -->
    <modify-print-plan
      v-if="
        checkPrivilege('link', 'preview') ||
          checkPrivilege('link', 'edit') ||
          checkPrivilege('button', 'add')
      "
      ref="ModifyPrintPlan"
      :instance="curPrintPlan"
      :edit-type="editType"
      @modified="getList"
    ></modify-print-plan>
  </div>
</template>

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

export default {
  name: "print-plan-manage",
  components: { ModifyPrintPlan },
  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,
      // date-picker
      createTime: [],
      pickerOptions
    };
  },
  mounted() {
    this.search();
  },
  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() {
      this.curPrintPlan = {};
      this.editType = "ADD";
      this.$refs.ModifyPrintPlan.open();
    },
    toEdit(row) {
      this.curPrintPlan = row;
      this.editType = "EDIT";
      this.$refs.ModifyPrintPlan.open();
    },
    toPreview(row) {
      this.curPrintPlan = row;
      this.editType = "PREVIEW";
      this.$refs.ModifyPrintPlan.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除印刷计划【${row.name}】吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await removePrintPlan(row.id);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    }
  }
};
</script>
