<template>
  <div class="task-apply-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="filter.auditStatus"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(val, key) in AUDITING_STATUS"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核结果：">
          <el-select
            v-model="filter.reviewStatus"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(val, key) in AUDITING_RESULT"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题卡规则：">
          <card-rule-select
            ref="CardRuleSelect"
            v-model.trim="filter.cardRuleId"
            placeholder="请选择"
            clearable
          ></card-rule-select>
        </el-form-item>
        <el-form-item label="课程（代码）：" label-width="110px">
          <course-select
            ref="CourseSelect"
            v-model.trim="filter.courseCode"
            placeholder="请选择"
            clearable
          ></course-select>
        </el-form-item>
        <el-form-item label="试卷编号：">
          <paper-number-select
            ref="PaperNumberSelect"
            v-model="filter.paperNumber"
            placeholder="请选择"
            clearable
          ></paper-number-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>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="el-icon-search" @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="examTasks" border stripe>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          align="center"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号"></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column
          prop="specialty"
          label="适用专业（方向）"
        ></el-table-column>
        <el-table-column prop="cardRuleName" label="题卡规则"></el-table-column>
        <el-table-column
          prop="userName"
          label="命题老师"
          width="100"
        ></el-table-column>
        <el-table-column prop="startTime" label="命题开始时间">
          <span slot-scope="scope">{{
            scope.row.startTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="endTime" label="命题结束时间">
          <span slot-scope="scope">{{
            scope.row.endTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="auditStatus" label="审核状态" width="100">
          <template slot-scope="scope">
            {{ scope.row.auditStatus | auditStatusFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="reviewStatus" label="审核结果" width="100">
          <template slot-scope="scope">
            {{ scope.row.reviewStatus | reviewStatusFilter }}
          </template>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          align="center"
          width="120px"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.auditStatus || scope.row.reviewStatus"
              class="btn-table-icon"
              type="text"
              icon="icon icon-circle-right"
              @click="toPreview(scope.row)"
              title="查看详情"
            ></el-button>
            <el-button
              v-if="!scope.row.auditStatus && !scope.row.reviewStatus"
              class="btn-table-icon"
              type="text"
              icon="icon icon-edit"
              @click="toEdit(scope.row)"
              title="立即申请"
            ></el-button>
            <el-button
              v-if="
                scope.row.auditStatus === 'AUDITED' &&
                  scope.row.reviewStatus === 'NOT_PASS'
              "
              class="btn-table-icon"
              type="text"
              icon="icon icon-edit"
              @click="toEdit(scope.row)"
              title="重新申请"
            ></el-button>
            <el-button
              v-if="
                scope.row.auditStatus === 'NOT_AUDITED' &&
                  scope.row.status === 'SUBMIT'
              "
              class="btn-table-icon"
              type="text"
              icon="icon icon-close-act"
              @click="toCancel(scope.row)"
              title="撤销申请"
            ></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>

    <!-- ModifyTaskApply -->
    <modify-task-apply
      ref="ModifyTaskApply"
      :edit-type="editType"
      :instance="curExamTask"
      @modified="taskModified"
    ></modify-task-apply>
  </div>
</template>

<script>
import ModifyTaskApply from "../components/ModifyTaskApply";
import { AUDITING_STATUS } from "@/constants/enumerate";
import pickerOptions from "@/constants/datePickerOptions";
import { taskApplyListPage, cancelOrRestartTaskApply } from "../api";
import { mapActions } from "vuex";

export default {
  name: "task-apply-manage",
  components: {
    ModifyTaskApply
  },
  data() {
    return {
      filter: {
        auditStatus: "",
        reviewStatus: "",
        cardRuleId: "",
        courseCode: "",
        paperNumber: "",
        startTime: "",
        endTime: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      editType: "APPLY",
      AUDITING_STATUS,
      AUDITING_RESULT: { NOT_PASS: "未通过" },
      examTasks: [],
      curExamTask: {},
      curUserId: this.$ls.get("user", { id: "" }).id,
      userRoles: this.$ls.get("user", { roleList: [] }).roleList,
      // date-picker
      createTime: [],
      pickerOptions
    };
  },
  created() {
    this.getList();
  },
  methods: {
    ...mapActions("exam", ["updateWaitTaskCount"]),
    async getList() {
      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 taskApplyListPage(datas);
      this.examTasks = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toCancel(row) {
      this.$confirm("确定要撤销当前申请吗？", "提示", {
        cancelButtonClass: "el-button--danger is-plain",
        confirmButtonClass: "el-button--primary",
        type: "warning"
      })
        .then(async () => {
          const data = await cancelOrRestartTaskApply({
            id: row.id,
            status: "CANCEL"
          }).catch(() => {});
          if (!data) return;
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {});
    },
    toEdit(row) {
      console.log(row);
      this.curExamTask = row;
      this.editType = "APPLY";
      this.$refs.ModifyTaskApply.open();
    },
    toPreview(row) {
      this.curExamTask = row;
      this.editType = "PREVIEW";
      this.$refs.ModifyTaskApply.open();
    },
    taskModified() {
      this.getList();
      this.updateWaitTaskCount(this.userRoles);
    }
  }
};
</script>
