<template>
  <div class="exam-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"
              placeholder="学期"
              clearable
            ></semester-select>
          </el-form-item>
          <el-form-item label="启用/禁用：" label-width="90px">
            <el-select
              v-model="filter.enable"
              style="width: 120px"
              placeholder="启用/禁用"
              clearable
            >
              <el-option
                v-for="(val, key) in ABLE_TYPE"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'add')"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >新增</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="exams">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          prop="semesterName"
          label="学年学期"
          min-width="210"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="考试名称"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="category" label="考试类型" width="100">
          <span slot-scope="scope">
            {{ scope.row.category | examTypeFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="examModel" label="业务模式" width="100">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              :content="EXAM_MODE_TYPE[scope.row.examModel]"
              placement="top-start"
            >
              <span>{{ scope.row.examModel }}</span>
            </el-tooltip>
          </template>
        </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="enable" label="启用/禁用" width="100">
          <template slot-scope="scope">
            {{ scope.row.enable | enableFilter }}
          </template>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="180px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="
                checkPrivilege('link', 'Set') && scope.row.examModel != 'MODEL4'
              "
              class="btn-primary"
              type="text"
              @click="toEditConfig(scope.row)"
              >设置</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'Enable')"
              :class="scope.row.enable ? 'btn-danger' : 'btn-primary'"
              type="text"
              @click="toEnable(scope.row)"
              >{{ scope.row.enable ? "禁用" : "启用" }}</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'Delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(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>

    <!-- modify-exam -->
    <modify-exam
      ref="ModifyExam"
      v-if="checkPrivilege('link', 'edit')"
      :instance="curExam"
      @modified="examModified"
    ></modify-exam>
    <!-- modify-exam-config-detail -->
    <modify-exam-config-detail
      ref="ModifyExamConfigDetail"
      v-if="checkPrivilege('link', 'Set')"
      :exam="curExam"
    ></modify-exam-config-detail>
  </div>
</template>

<script>
import { examListQuery, deleteExam, ableExam } from "../api";
import { ABLE_TYPE, EXAM_MODE_TYPE } from "@/constants/enumerate";
import ModifyExam from "../components/ModifyExam";
import ModifyExamConfigDetail from "../components/ModifyExamConfigDetail";

export default {
  name: "exam-manage",
  components: { ModifyExam, ModifyExamConfigDetail },
  data() {
    return {
      filter: {
        semesterId: "",
        enable: "",
      },
      ABLE_TYPE,
      EXAM_MODE_TYPE,
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      exams: [],
      curExam: {},
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (datas.enable !== null && datas.enable !== "")
        datas.enable = !!datas.enable;

      const data = await examListQuery(datas);
      this.exams = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curExam = {};
      this.$refs.ModifyExam.open();
    },
    toEdit(row) {
      this.curExam = row;
      this.$refs.ModifyExam.open();
    },
    examModified({ isEdit, exam }) {
      if (!isEdit && exam.examModel != "MODEL4") {
        this.toEditConfig(exam);
      }
      this.getList();
    },
    toEditConfig(row) {
      this.curExam = row;
      this.$refs.ModifyExamConfigDetail.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除考试【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await deleteExam(row.id);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
    toEnable(row) {
      const action = row.enable ? "禁用" : "启用";
      this.$confirm(`确定要${action}考试【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          const enable = !row.enable;
          await ableExam({
            id: row.id,
            enable,
          });
          row.enable = enable;
          this.$message.success("操作成功！");
        })
        .catch(() => {});
    },
  },
};
</script>
