<!--题目管理-->
<template>
  <div class="app-container qu">
    <div class="page-container">
      <div class="page-form-title-box">
        <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="请输入题目名称"

              size="small"
            />
          </el-form-item>
          <el-form-item label="" prop="quType" style="margin-left: 10px">
            <el-select
              v-model="queryParams.quType"
              placeholder="请选择题目类型"

              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="classifyIds" style="margin-left: 10px">
            <el-select
              v-model="queryParams.classifyIds"
              placeholder="请选择题目分类"

              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="请选择难度级别"

              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-hasPermiRouter="['exam:qu:add']">
            <el-row :gutter="10" class="mb8" v-show="pageType == 0">
              <el-dropdown @command="addTopic">
                <p class="page-submit-common-style-button" style="width:100px;">新增题目</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="page-inquire-common-style-button" @click="handleQuery">查询</p>
            <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table border class="table-box" 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="classifyNames" width="160"></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="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="editTopic(scope.row)"
                   v-hasPermiRouter="['exam:qu:add','exam:qu:edit']">编辑</p>
                <p class="table-button-p"
                   @click="handleDelete(scope.row)"
                   v-hasPermiRouter="['exam:qu:del']">删除</p>
                <p class="table-button-null"></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.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <add-topic v-if="pageType != 0" :pageType="pageType" :quId="quId"></add-topic>
  </div>
</template>

<script>
import { examQuList, examQu, examQuDel, examQuAdd, examQuEdit,
  examRepoQueryOption,examElClassifyQueryOption} from "@/api/safetyEducationExam/index";
import addTopic from "./addTopic.vue";

export default {
  components: {
    addTopic,
  },
  name: "Qu",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['exam:qu:detail','exam:qu:edit','exam:qu:del']),
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题题目表格数据
      quList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 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(){
      examElClassifyQueryOption({}).then(response => {
        this.optionList = response.data.records;
      });
    },
    /** 查询题库列表 */
    getRepoOption() {
      examRepoQueryOption({}).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() {
      let self=this;
      self.loading = true;
      examQuList(this.queryParams).then((response) => {
        this.quList = response.data.records;
        this.total = response.data.total;
        self.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.page = 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;
      examQu(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) {
            examQuEdit(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            examQuAdd(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let self=this;
      this.$confirm('是否确认删除该题目?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
      }).then(() => {
        examQuDel({id:ids}).then(response => {
          self.msgSuccess(response.message)
          self.getList();
        });
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "exam/qu/export",
        {
          ...this.queryParams,
        },
        `exam_qu.xlsx`
      );
    },
  },
};
</script>


<style scoped lang="scss">
.qu {
  .el-row {
    margin-bottom: 20px;
  }
  .button-box {
    width: 190px;
    display: flex;
  }
  .form-box {
    display: flex;
    .null-p {
      flex: 1;
    }
  }
}
</style>
