<template>
  <div class="audit-question-audited">
    <el-form class="part-filter-form" :model="filter" inline>
      <el-form-item label="提交人">
        <el-input
          v-model="filter.creatorName"
          placeholder="请输入提交人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @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="试题">
          <div slot-scope="scope" @click="toDetail(scope.row)">
            <rich-text
              class="row-question-body"
              title="点击查看试题"
              :text-json="scope.row.quesBody"
            ></rich-text>
          </div>
        </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 label="提交人" prop="creator" width="150">
        </el-table-column>
        <el-table-column label="提交时间" prop="updateTime" width="170">
        </el-table-column>
        <el-table-column label="当前审核状态" prop="inReviewStatus" width="140">
        </el-table-column>
        <el-table-column label="状态" prop="auditStatusName" width="100">
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <div class="operate_left">
              <el-button
                size="medium"
                type="text"
                class="normal"
                @click="toDetail(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>

    <!-- QuestionPreviewDialog -->
    <question-preview-dialog
      ref="QuestionPreviewDialog"
      :list-info="listInfo"
      :fetchPage="toPage"
      show-switch
    ></question-preview-dialog>
  </div>
</template>

<script>
import { auditQuestionAuditedPageListApi } from "../api";
import QuestionPreviewDialog from "./QuestionPreviewDialog.vue";

export default {
  name: "AuditQuestionAudited",
  components: { QuestionPreviewDialog },
  data() {
    return {
      filter: { creatorName: "" },
      dataList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      totalPages: 0,
      loading: false,
      curQuestion: {},
      curQuestionIndex: 0,
    };
  },
  computed: {
    listInfo() {
      return {
        dataList: this.dataList,
        curIndex: this.curQuestionIndex,
        pageNumber: this.currentPage,
        totalPages: this.totalPages,
      };
    },
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async toPage(page) {
      this.currentPage = page;
      await this.getList();
    },
    async getList() {
      this.loading = true;
      const res = await auditQuestionAuditedPageListApi({
        ...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;
      this.totalPages = res.data.totalPages;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.toPage(1);
    },
    toDetail(row) {
      this.curQuestionIndex = this.dataList.findIndex((q) => q.id === row.id);
      this.$refs.QuestionPreviewDialog.open();
    },
  },
};
</script>
