<!--题目管理-->
<template>
  <div class="app-container qu">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch && pageType == 0"
      label-width="68px"
    >
      <el-form-item label="题目名称" prop="content" style="margin-left: 10px">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入题目名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="题目类型" prop="quType" style="margin-left: 10px">
        <el-select
          v-model="queryParams.quType"
          placeholder="请选择题目类型"
          clearable
          size="small">
          <el-option
            v-for="dict in quTypes"
            :key="dict.type"
            :label="dict.value"
            :value="dict.type">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目分类" prop="cIds" style="margin-left: 10px">
        <el-select
          v-model="queryParams.cIds"
          placeholder="请选择题目分类"
          clearable
          size="small">
          <el-option
            v-for="dict in optionList"
            :key="dict.id"
            :label="dict.title"
            :value="dict.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难度级别" prop="level" style="margin-left: 10px">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择难度级别"
          clearable
          size="small"
        >
                     <el-option
          v-for="dict in levels"
          :key="dict.type"
          :label="dict.value"
          :value="dict.type"
        ></el-option>
                 
        </el-select>
      </el-form-item>
      <el-form-item style="float: right;" v-hasPermi="['exam:qu:add']">
        <el-row :gutter="10" class="mb8" v-show="pageType == 0">
          <el-dropdown @command="addTopic">
            <p class="add-button-one-120"><i class="el-icon-plus"></i>新增题目</p>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                style="border-bottom: 1px solid #e0e0e0; margin: 0 10px"
                command="1"
              >单选题</el-dropdown-item
              >
              <el-dropdown-item
                style="border-bottom: 1px solid #e0e0e0; margin: 0 10px"
                command="2"
              >多选题</el-dropdown-item
              >
              <el-dropdown-item style="margin: 0 10px" command="3"
              >判断题</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-row>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>
    <el-table border
      v-loading="loading"
      :data="quList"
      @selection-change="handleSelectionChange"
      v-show="pageType == 0"
    >
      <el-table-column label="题目名称" align="left" prop="content" show-overflow-tooltip/>
      <el-table-column label="题目类型"  align="left" prop="quType" width="120">
        <template slot-scope="scope">
          <span
            v-for="(item, index) in quTypes"
            :key="index"
            v-if="scope.row.quType == item.type"
          >{{ item.value }}</span
          >
        </template>
      </el-table-column>
      <!--      <el-table-column label="归属题库"  align="left" prop="poId">
              <template slot-scope="scope">
                <span v-for="(bigItem,index1) in scope.row.repoIds" :key="index1" style="margin-right:10px;" v-if="scope.row.repoIds[0]">
                  <span v-for="(minItem,index2) in repoOption" :key="index2" v-if="bigItem==minItem.id">{{ minItem.title}}</span>
                </span>
                <span  v-if="!scope.row.repoIds[0]">暂无归属</span>
              </template>
            </el-table-column>-->
      <el-table-column label="题目分类"  align="left" prop="classifyNames" width="160"></el-table-column>
      <!--<el-table-column label="适用范围"  align="left" prop="scopeType" width="160">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.scopeType==1?'安全准入考试':(scope.row.scopeType==2?'负面清单考试':(scope.row.scopeType==3?'黑名单考试':scope.row.scopeType==4?'模拟考试':"未知")) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="难度级别"  align="left" prop="level" width="120">
        <template slot-scope="scope">
          <span
            v-for="(item, index) in levels"
            :key="index"
            v-if="scope.row.level == item.type"
          >{{ item.value }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="创建时间"  align="left" prop="createTime"  width="180"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
              style="margin-right: 10px"
              @click="editTopic(scope.row)"
              v-hasPermiAnd="['exam:qu:query','exam:qu:edit']">编辑</p>
            <p class="table-min-button"
              @click="handleDelete(scope.row)"
              v-hasPermi="['exam:qu:remove']">删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination :page-sizes="[20, 30, 40, 50]"
      v-show="total > 0 && pageType == 0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div v-if="pageType != 0">
      <add-topic :pageType="pageType" :quId="quId"></add-topic>
    </div>
  </div>
</template>

<script>
import { listQu, getQu, delQu, addQu, updateQu } from "@/api/exam/qu";
import addTopic from "./addTopic.vue";
import { optionRepo } from "@/api/exam/repo";
import { list_option } from "@/api/exam/el_classify";

export default {
  components: {
    addTopic,
  },
  name: "Qu",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['exam:qu:query','exam:qu:edit','exam:qu:remove']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题题目表格数据
      quList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        poId: null,
        quType: null,
        level: null,
        scopeType: null,
        content: null,
        analysis: null,
        userId: null,
        deptId: null,
        deptName: null,
      },
      scopeTypes: [
        {
          type: 1,
          value: "安全准入考试",
        },
        {
          type: 2,
          value: "负面清单考试",
        },
        {
          type: 3,
          value: "黑名单考试",
        },
        {
          type: 4,
          value: "模拟考试",
        },
      ],
      levels: [
        {
          type: 1,
          value: "中等",
        },
        {
          type: 2,
          value: "较难",
        },
        {
          type: 3,
          value: "难",
        },
      ],
      quTypes: [
        {
          type: 1,
          value: "单选题",
        },
        {
          type: 2,
          value: "多选题",
        },
        {
          type: 3,
          value: "判断题",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        quType: [
          { required: true, message: "题目类型不能为空", trigger: "change" },
        ],
        level: [
          { required: true, message: "1普通,2较难不能为空", trigger: "blur" },
        ],
        content: [
          { required: true, message: "题目内容不能为空", trigger: "blur" },
          { required: true, message: "题目内容不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
          { required: true, message: "创建时间不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
          { required: true, message: "更新时间不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        remark: [
          { required: true, message: "题目备注不能为空", trigger: "blur" },
          { required: true, message: "题目备注不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        analysis: [
          { required: true, message: "整题解析不能为空", trigger: "blur" },
          { required: true, message: "整题解析不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
      },
      //页面状态 默认0单选1多选2判断3
      pageType: 0,
      repoOption: [],
      quId:"",
      //题目分类
      optionList:[],
    };
  },
  created() {
    this.getList();
    this.getRepoOption();
    this.getList_option();
  },
  methods: {
    //刷新数据/用于修改或编辑后
    refreshData(){
      this.resetQuery();
      this.pageType = 0;
    },
    getList_option(){
      list_option({}).then(response => {
        this.optionList = response.data;
      });
    },
    /** 查询题库列表 */
    getRepoOption() {
      optionRepo({}).then(response => {
        this.repoOption = response.data;
      });
    },
    //创建题目
    addTopic(type) {
      if (this.pageType != type) {
        this.quId = "";
        this.pageType = type;
      }
    },
    //编辑题目
    editTopic(item) {
      if (item.quType) {
        this.quId = item.id;
        this.pageType = item.quType;
      }
    },
    //关闭创建页面
    offAddTopic() {
      this.pageType = 0;
    },
    /** 查询问题题目列表 */
    getList() {
      this.loading = true;
      listQu(this.queryParams).then((response) => {
        this.quList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        quType: null,
        level: null,
        scopeType: null,
        content: null,
        createTime: null,
        updateTime: null,
        remark: null,
        analysis: null,
        userId: null,
        createBy: null,
        updateBy: null,
        deptId: null,
        deptName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQu(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQu(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQu(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除该题目?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delQu(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "exam/qu/export",
        {
          ...this.queryParams,
        },
        `exam_qu.xlsx`
      );
    },
  },
};
</script>


<style scoped lang="scss">
.qu {
  display: flex !important;
  flex-direction: column;
  box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
  padding: 20px !important;
  .el-row {
    margin-bottom: 20px;
  }
  .button-box {
    width: 190px;
    display: flex;
  }
  .form-box {
    display: flex;
    .null-p {
      flex: 1;
    }
  }
}
</style>
