<template>
  <div class="audit-paper-audited">
    <el-form class="part-filter-form" :model="filter" inline>
      <el-form-item label="审核状态">
        <el-select v-model="filter.auditStatus" clearable placeholder="请选择">
          <el-option label="待审核" value="WITHDRAW"> </el-option>
          <el-option label="审核中" value="IN_REVIEW"> </el-option>
          <el-option label="已入库" value="PASS"> </el-option>
          <el-option label="已驳回" value="NOT_PASS"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="试卷名称">
        <el-input
          v-model="filter.paperName"
          placeholder="请输入试卷名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="toPage(1)">查询</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        :data="dataList"
      >
        <el-table-column label="试卷名称" prop="paperName"> </el-table-column>
        <el-table-column label="课程" prop="courseName"> </el-table-column>
        <el-table-column label="提交时间" prop="creationTime" width="170">
        </el-table-column>
        <el-table-column label="状态" prop="auditStatusName" width="100">
        </el-table-column>
        <el-table-column label="操作" width="170" fixed="right">
          <template slot-scope="scope">
            <div class="operate_left">
              <el-button
                size="mini"
                type="primary"
                plain
                @click="toDetail(scope.row)"
                >详情</el-button
              >
              <el-button
                size="mini"
                type="primary"
                plain
                @click="toWithdraw(scope.row)"
                >撤回</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 300]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @current-change="toPage"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { auditPaperApplyPageListApi, withdrawPaperApi } from "../api";

export default {
  name: "AuditPaperApply",
  data() {
    return {
      filter: { auditStatus: "", paperName: "" },
      dataList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false,
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    toPage(page) {
      this.currentPage = page;
      this.getList();
    },
    async getList() {
      this.loading = true;
      const res = await auditPaperApplyPageListApi({
        ...this.filter,
        curPage: this.currentPage,
        pageSize: this.pageSize,
      }).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.dataList = res.data.content;
      this.total = res.data.totalElements;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.toPage(1);
    },
    toDetail(row) {
      window.open(
        this.getRouterPath({
          name: "EditPaper",
          params: {
            id: row.id,
            parentView: "audit-paper",
          },
        })
      );
    },
    async toWithdraw(row) {
      const confirm = await this.$confirm(`确定要撤销该试卷的提交吗?`, "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await withdrawPaperApi([row.id]);
      this.$message.success("操作成功！");
    },
  },
};
</script>
