<template>
  <div class="app-container">
    <!--搜索模块-->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="题目" prop="issue">
        <el-input
          v-model="queryParams.issue"
          placeholder="请输入题目"
          size="small"
        />
      </el-form-item>
      <el-form-item label="科目" prop="subject">
        <el-select v-model="queryParams.subject" placeholder="请选择科目">
          <el-option label="科目一" :value="1" />
          <el-option label="科目四" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="专题类型" prop="issueTypeValue">
        <el-select
          v-model="queryParams.issueTypeValue"
          placeholder="请选择类型"
        >
          <el-option label="分类专题" :value="1" />
          <el-option label="精选专题" :value="2" />
          <el-option label="地方专题" :value="3" />
          <el-option label="顺序练习" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="issueValue">
        <el-select v-model="queryParams.issueValue" placeholder="请选择分类">
          <el-option
            v-for="item in issueTypeList"
            :key="item.typeId"
            :label="item.title"
            :value="item.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--列表数据模块-->
    <div class="table-box">
      <el-table v-loading="loading" :data="list" height="100%">
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="题目" align="center" width="300px">
          <template slot-scope="scope">
            <el-card :body-style="{ padding: '5px' }">
              <div class="issue-card">
                <span>{{ scope.row.issue }}</span>
                <img
                  v-if="scope.row.image"
                  :src="scope.row.image"
                  @click="imagePreview(scope.row.image)"
                />
              </div>
            </el-card>
          </template>
        </el-table-column>
        <el-table-column label="指定顺序" align="center" width="130px">
          <template slot-scope="scope">
            <el-card :body-style="{ padding: '5px' }">
              <div class="sort-card">
                <el-input
                  v-model="scope.row.sort"
                  placeholder="题目顺序"
                  type="number"
                  size="small"
                />
                <el-button
                  type="primary"
                  size="mini"
                  @click="setSort(scope.row)"
                  >修改顺序</el-button
                >
              </div>
            </el-card>
          </template>
        </el-table-column>
        <el-table-column
          label="分类模块名称"
          align="center"
          prop="classIssueName"
        />
        <el-table-column
          label="精选模块名称"
          align="center"
          prop="excellIssueName"
        />
        <el-table-column
          label="顺序模块名称"
          align="center"
          prop="sequeIssueName"
        />
        <el-table-column
          label="地方模块名称"
          align="center"
          prop="placeIssueName"
        />
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 50, 300, 1000, 10000]"
      @pagination="getList"
    />
  </div>
</template>

<script>
import subjectApi from "@/api/subject";
import imagePreview from "@/components/ImagePreview";

export default {
  name: "Subject",
  data() {
    return {
      //表格加载状态
      loading: false,
      //表格数据
      list: null,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueTypeValue: 1,
        issueValue: "1",
        subject: 1,
      },
      //具体分类列表
      issueTypeList: null,
    };
  },
  created() {
    this.getList();
    this.getIssueTypeList();
  },
  watch: {
    "queryParams.subject": {
      handler() {
        this.getIssueTypeList();
      },
    },
    "queryParams.issueTypeValue": {
      handler() {
        this.getIssueTypeList();
      },
    },
  },
  methods: {
    /**预览图片 */
    imagePreview: imagePreview,
    /**查询用户列表 */
    async getList() {
      this.loading = true;
      const res = await subjectApi.getList(this.queryParams);
      this.list = res.rows;
      this.total = res.total;
      this.loading = false;
    },
    /**获取具体分类列表 */
    async getIssueTypeList() {
      const res = await subjectApi.getIssueType({
        issueTypeValue: this.queryParams.issueTypeValue,
        subject: this.queryParams.subject,
      });
      this.issueTypeList = res.data;
      this.$set(this.queryParams, "issueValue", this.issueTypeList[0].typeId);
    },
    /**修改题目顺序 */
    async setSort(row) {
      try {
        // await this.$confirm(
        //   `确认修改题目ID为${row.id}的顺序为${row.sort}吗？`,
        //   "修改题目顺序",
        //   {
        //     distinguishCancelAndClose: true,
        //     confirmButtonText: "保存",
        //     cancelButtonText: "放弃修改",
        //   }
        // );
        await subjectApi.setSubjectSort({
          id: row.id,
          sort: row.sort,
          issueTypeValue: this.queryParams.issueTypeValue,
        });
        this.$message({
          type: "info",
          message: "保存修改",
        });
        this.getList();
      } catch (error) {
        this.$message({
          type: "info",
          message: "放弃修改",
        });
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 150px);
  padding: 20px;
  .table-box {
    flex-grow: 1;
  }
}
.issue-card {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  img {
    width: 50%;
    cursor: pointer;
  }
}
.sort-card {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  height: 70px;
}
</style>
