<template>
  <div class="source-detail-manage">
    <div class="part-box">
      <h2 class="part-box-title">
        题型管理 - {{ courseInfo.name }}（{{ courseInfo.code }}）
      </h2>
      <!-- 搜索 -->
      <div class="box-justify">
        <el-form class="part-filter-form" inline>
          <el-form-item label="题型类型">
            <question-type-select
              v-model="searchForm.questionType"
            ></question-type-select>
          </el-form-item>
          <el-form-item label="题型名称">
            <el-input
              v-model="searchForm.name"
              placeholder="请输入题型名称"
              maxlength="50"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="handleCurrentChange(1)">
              查询
            </el-button>
          </el-form-item>
        </el-form>

        <div>
          <el-button
            type="primary"
            plain
            icon="icon icon-edit"
            @click="toCreate"
            >新建
          </el-button>
          <el-button
            type="danger"
            size="small"
            plain
            icon="icon icon-back"
            @click="toback"
            >返回</el-button
          >
        </div>
      </div>
    </div>

    <div class="part-box">
      <!-- 页面列表 -->
      <el-table ref="table" :data="tableData">
        <el-table-column prop="name" label="题型名称"> </el-table-column>
        <el-table-column prop="questionTypeName" label="题型类型">
        </el-table-column>
        <el-table-column width="170" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              plain
              @click="toEdit(scope.row)"
              >重命名
            </el-button>
            <el-button
              size="mini"
              type="danger"
              plain
              @click="toDelete(scope.row)"
              >删除
            </el-button>
          </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="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <!-- ModifySourceDetail -->
    <modify-source-detail
      ref="ModifySourceDetail"
      :instance="curRow"
      @modified="search"
    ></modify-source-detail>
  </div>
</template>

<script>
import { sourceDetailPageListApi, deleteSourceDetailApi } from "../api";

import ModifySourceDetail from "../components/ModifySourceDetail.vue";

export default {
  name: "SourceDetailManage",
  components: { ModifySourceDetail },
  data() {
    return {
      loading: false,
      searchForm: { name: "", questionType: "" },
      tableData: [],
      curRow: {},
      currentPage: 1,
      pageSize: 10,
      total: 10,
      courseInfo: {},
      info: {},
    };
  },
  mounted() {
    let courseInfo = sessionStorage.getItem("courseInfo");
    this.courseInfo = courseInfo ? JSON.parse(courseInfo) : {};
    this.info = {
      courseId: this.courseInfo.id,
      rootOrgId: this.$store.state.user.rootOrgId,
    };
    this.search();
  },
  methods: {
    async search() {
      if (this.loading) return;
      this.loading = true;

      const res = await sourceDetailPageListApi({
        ...this.searchForm,
        ...this.info,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      }).catch(() => {});

      this.loading = false;
      if (!res) return;

      this.tableData = res.data.content;
      this.total = res.data.totalElements;
    },
    handleCurrentChange(val) {
      console.log(val);
      this.currentPage = val;
      this.search();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.search();
    },
    toCreate() {
      this.curRow = { ...this.info };
      this.$refs.ModifySourceDetail.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifySourceDetail.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(`确定要删除该题型吗?`, "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteSourceDetailApi([row.id]);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    toback() {
      window.history.go(-1);
    },
  },
};
</script>
