<template>
  <div class="content question-recycle">
    <div class="part-box">
      <div class="part-box-header">
        <h2 class="part-box-title">回收站</h2>

        <el-button type="danger" plain @click="goback">返回</el-button>
      </div>

      <div class="part-box-action">
        <div>
          <el-button
            type="primary"
            plain
            icon="el-icon-data-analysis"
            @click="toBatchRecover"
            >恢复</el-button
          >
          <el-button
            type="danger"
            plain
            icon="el-icon-lock"
            @click="toBatchDelete"
            >删除</el-button
          >
        </div>
        <div>
          <el-button
            type="primary"
            plain
            icon="el-icon-folder-opened"
            @click="toClear"
            >清空回收站</el-button
          >
        </div>
      </div>
    </div>

    <div class="part-box">
      <el-table
        v-loading="loading"
        element-loading-text="加载中"
        :data="dataList"
        @selection-change="tableSelectChange"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="试卷名称"> </el-table-column>
        <el-table-column label="课程名称">
          <template slot-scope="scope">
            <span
              >{{ scope.row.course.name }}（{{ scope.row.course.code }}）</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="operator" label="操作人" width="120">
        </el-table-column>
        <el-table-column prop="deleteTime" label="删除时间" width="170">
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right">
          <template slot-scope="scope">
            <div class="operate_left">
              <el-button
                size="mini"
                type="primary"
                plain
                @click="toRecover(scope.row)"
                >恢复</el-button
              >
              <el-button
                size="mini"
                type="danger"
                plain
                @click="toDelete(scope.row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  paperRecycleListApi,
  recoverPaperApi,
  thoroughDeletePaperApi,
  clearPaperRecycleApi,
} from "../api";

export default {
  name: "QuestionRecycle",
  data() {
    return {
      dataList: [],
      loading: false,
      recycleParamList: [],
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    toPage(page) {
      this.currentPage = page;
      this.getList();
    },
    async getList() {
      this.recycleParamList = [];
      this.loading = true;
      const res = await paperRecycleListApi().catch(() => {});
      this.loading = false;
      if (!res) return;
      res.data.forEach((item) => {
        if (item.recycleType === "QUESTION") {
          item.name = JSON.parse(item.name);
        }
        return item;
      });
      this.dataList = res.data;
    },
    tableSelectChange(selections) {
      this.recycleParamList = selections.map((item) => {
        return {
          id: item.id,
          recycleType: item.recycleType,
        };
      });
    },
    async toClear() {
      const confirm = await this.$confirm("确认要清空回收站吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.loading = true;
      const res = await clearPaperRecycleApi().catch((error) => {
        this.$notify({
          message: error.response.data.desc,
          type: "error",
        });
      });

      if (!res) return;

      this.$notify({
        message: "操作成功",
        type: "success",
      });
      this.toPage(1);
    },
    async toDelete(row) {
      const confirm = await this.$confirm("确认彻底删除数据吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.deleteData([
        {
          id: row.id,
          recycleType: row.recycleType,
        },
      ]);
    },
    async deleteData(recycleParamList) {
      this.loading = true;
      const res = await thoroughDeletePaperApi(recycleParamList).catch(
        (error) => {
          this.$notify({
            message: error.response.data.desc,
            type: "error",
          });
        }
      );

      if (!res) return;

      this.$notify({
        message: "删除成功",
        type: "success",
      });
      this.getList();
    },
    async toBatchDelete() {
      if (!this.recycleParamList.length) {
        this.$message.error("请选择数据！");
        return;
      }

      const confirm = await this.$confirm("确认彻底删除选中数据吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.deleteData(this.recycleParamList);
    },
    async toRecover(row) {
      const confirm = await this.$confirm("确认恢复数据吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.recoverData([
        {
          id: row.id,
          recycleType: row.recycleType,
        },
      ]);
    },
    async recoverData(recycleParamList) {
      this.loading = true;
      const res = await recoverPaperApi(recycleParamList).catch((error) => {
        this.$notify({
          message: error.response.data.desc,
          type: "error",
        });
      });

      if (!res) return;

      this.$notify({
        message: "操作成功",
        type: "success",
      });
      this.getList();
    },
    async toBatchRecover() {
      if (!this.recycleParamList.length) {
        this.$message.error("请选择数据！");
        return;
      }

      const confirm = await this.$confirm("确认恢复选中数据吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.recoverData(this.recycleParamList);
    },
  },
};
</script>
