<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 label="文件夹/试题">
          <template slot-scope="scope">
            <rich-text
              v-if="scope.row.recycleType === 'QUESTION'"
              class="row-question-body"
              title="点击查看试题"
              :text-json="scope.row.name"
            ></rich-text>
            <div v-else>{{ scope.row.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="courseName" label="课程" width="120">
        </el-table-column>
        <el-table-column prop="sourceDetailName" label="题型" width="100">
        </el-table-column>
        <el-table-column prop="operator" label="操作人" width="120">
        </el-table-column>
        <el-table-column label="删除时间" width="170" prop="deleteTime">
        </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 {
  questionRecycleListApi,
  recoverQuestionApi,
  thoroughDeleteQuestionApi,
  clearQuestionRecycleApi,
} 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 questionRecycleListApi().catch(() => {});
      this.loading = false;
      if (!res) return;
      res.data.forEach((item) => {
        if (item.recycleType === "QUESTION") {
          item.name = item.name ? JSON.parse(item.name) : null;
        }
      });
      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 clearQuestionRecycleApi().catch(() => {});
      this.loading = false;
      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 thoroughDeleteQuestionApi(recycleParamList).catch(
        () => {}
      );
      this.loading = false;

      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 recoverQuestionApi(recycleParamList).catch(() => {});
      this.loading = false;

      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>
