<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
              class="row-question-body"
              title="点击查看试题"
              :text-json="scope.row.quesBody"
              @click="toViewQuestion(scope.row)"
            ></rich-text>
          </template>
        </el-table-column>
        <el-table-column label="课程" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.course.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="题型" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.questionType | questionType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作人" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.creator }}</span>
          </template>
        </el-table-column>
        <el-table-column label="删除时间" width="170" prop="creationTime">
        </el-table-column>
        <el-table-column label="操作" width="180" 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 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 {
  questionRecycleListApi,
  recoverQuestionApi,
  thoroughDeleteQuestionApi,
  clearQuestionRecycleApi,
} from "../api";

export default {
  name: "QuestionRecycle",
  data() {
    return {
      dataList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      selectedIds: [],
    };
  },
  mounted() {
    // this.toPage(1);
  },
  methods: {
    toPage(page) {
      this.currentPage = page;
      this.getList();
    },
    async getList() {
      this.selectedIds = [];
      this.loading = true;
      const res = await questionRecycleListApi({
        curPage: this.currentPage,
        pageSize: this.pageSize,
      }).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.dataList = res.data.content;
      this.total = res.data.totalElements;
    },
    tableSelectChange(selections) {
      this.selectedIds = selections.map((item) => item.id);
    },
    async toClear() {
      const confirm = await this.$confirm("确认要清空回收站吗?", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.loading = true;
      const res = await clearQuestionRecycleApi().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([row.id]);
    },
    async deleteData(ids) {
      this.loading = true;
      const res = await thoroughDeleteQuestionApi(ids).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.selectedIds.length) {
        this.$message.error("请选择数据！");
        return;
      }

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

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

      this.recoverData([row.id]);
    },
    async recoverData(ids) {
      this.loading = true;
      const res = await recoverQuestionApi(ids).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.selectedIds.length) {
        this.$message.error("请选择数据！");
        return;
      }

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

      this.recoverData(this.selectedIds);
    },
    toViewQuestion(row) {
      console.log(row);
    },
  },
};
</script>
