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

        <el-button type="danger" plain @click="goback">返回</el-button>
      </div> -->
    <el-form class="part-filter-form" inline :model="searchForm">
      <el-form-item label="课程">
        <el-select
          v-model="searchForm.courseId"
          :remote-method="getCoursesList"
          :loading="courseLoading4Search"
          remote
          filterable
          clearable
          placeholder="请选择"
          @clear="getCoursesList('')"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.name + ' - ' + item.code"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleCurrentChange(1)">
          查询
        </el-button>
      </el-form-item>
    </el-form>
    <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 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 prop="courseName" label="课程名称"> </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 class="part-page">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import {
  paperRecycleListApi,
  recoverPaperApi,
  thoroughDeletePaperApi,
  clearPaperRecycleApi,
} from "../api";
import { courseQueryApi } from "../../card/api";
export default {
  name: "QuestionRecycle",
  data() {
    return {
      courseList: [],
      dataList: [],
      loading: false,
      recycleParamList: [],
      courseLoading4Search: false,
      searchForm: {
        courseId: "",
      },
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  created() {
    this.getCoursesList();
    this.handleCurrentChange(1);
  },
  methods: {
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.search();
    },
    async getCoursesList(query) {
      this.courseLoading4Search = true;
      const res = await courseQueryApi(query);
      this.courseList = res.data || [];
      this.courseLoading4Search = false;
    },
    async search() {
      this.recycleParamList = [];
      this.loading = true;
      const res = await paperRecycleListApi({
        ...this.searchForm,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      }).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.total = res.data.totalElements;
      this.dataList = res.data.content || [];
    },
    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(() => {});
      this.loading = false;
      if (!res) return;

      this.$notify({
        message: "操作成功",
        type: "success",
      });
      this.handleCurrentChange(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(
        () => {}
      );
      this.loading = false;

      if (!res) return;

      this.$notify({
        message: "删除成功",
        type: "success",
      });
      this.search();
    },
    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(() => {});
      this.loading = false;

      if (!res) return;

      this.$notify({
        message: "操作成功",
        type: "success",
      });
      this.search();
    },
    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>
