<!-- 试题管理 -->
<template>
  <div class="app-container questionBankManagement">
    <div class="page-container questionBankManagementPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="shareStatus">
            <el-select v-model="queryParams.shareStatus" placeholder="是否共享" style="width: 180px">
              <el-option
                v-for="dict in optionListA"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="knowledgePointId">
            <el-cascader
              v-model="queryParams.knowledgePointId"
              :options="optionListB"
              :props="{
                emitPath:false,
                checkStrictly: true,
                value: 'id',
                label: 'knowledgePointName',
                children: 'children',
              }"
              style="width: 180px"
              :show-all-levels="false"
              filterable
              placeholder="知识点"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="" prop="questionType">
            <el-select v-model="queryParams.questionType" placeholder="题型" style="width: 180px">
              <el-option
                v-for="dict in optionListC"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="difficulty">
            <el-select v-model="queryParams.difficulty" placeholder="难度" style="width: 180px">
              <el-option
                v-for="dict in optionListD"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="disciplineTypeId">
            <el-select v-model="queryParams.disciplineTypeId" placeholder="试题分类" style="width: 200px">
              <el-option
                v-for="dict in disciplineOptionList"
                :key="dict.id"
                :label="dict.disciplineName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="questionContent">
            <el-input
              maxLength="30"
              v-model="queryParams.questionContent"
              placeholder="搜索试题"
              style="width: 200px"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             v-hasPermiRouter="['exam:elQuestionBank:add']"
             @click="tableButton(1)"
          >添加试题</p>
          <el-form-item style="float: right;" v-hasPermiRouter="['exam:elQuestionBank:importData']">
            <import-component :importConfig="importConfig"></import-component>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box"  border :data="dataList">
          <el-table-column label="题干" prop="questionContent"  show-overflow-tooltip>
            <template slot-scope="scope">
              <span style="color:#0183fa;cursor: pointer;" @click="tableButton(2,scope.row)">{{ cleanRichText(scope.row.questionContent) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="知识点" prop="knowledgePointNames" width="200" show-overflow-tooltip/>
          <el-table-column label="题型" prop="questionType" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.questionType == 1?'单选题':(scope.row.questionType == 2?'多选题':(scope.row.questionType == 3?'判断题':'')) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="难度" prop="content" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.difficulty == 1?'简单':(scope.row.difficulty == 2?'一般':(scope.row.difficulty == 3?'较难':(scope.row.difficulty == 4?'极难':''))) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否共享" prop="content" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span style="padding:2px 10px;border:1px solid rgb(1, 131, 251);border-radius:4px;color: rgb(1, 131, 251);background-color:rgba(1, 131, 251,0.2)" v-if="scope.row.shareStatus == 1">共享</span>
              <span style="padding:2px 10px;border:1px solid rgb(245, 154, 35);border-radius:4px;color: rgb(245, 154, 35);background-color:rgba(245, 154, 35,0.2)" v-if="scope.row.shareStatus == 0">不共享</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   v-if="scope.row.canEditDelete"
                   v-hasPermiRouter="['exam:elQuestionBank:edit']"
                   @click="tableButton(3,scope.row)"
                >编辑</p>
                <p class="table-button-p"
                   v-if="scope.row.canEditDelete"
                   v-hasPermiRouter="['exam:elQuestionBank:del']"
                   @click="tableButton(4,scope.row)"
                >删除</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"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <add-page :propsData="propsData" v-if="pageType === 2"></add-page>
    <el-drawer
      size="40%"
      custom-class="questionBankManagement-drawer"
      :append-to-body="true"
      :title="drawerTitle"
      ref="drawer"
      :visible.sync="drawer"
      direction="rtl"
    >
      <answerKey v-if="drawer" :answerKeyPropsData="answerKeyPropsData"></answerKey>
    </el-drawer>
  </div>
</template>
<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  import { examElKnowledgePointTreeList,examElQuestionBankList,examElQuestionBankDel,examElDisciplineTypeList } from "@/api/safetyEducationExaminationNew/index";
  import importComponent from "@/components/importComponent/importComponent.vue";
  import answerKey from "@/views/safetyEducationExaminationNew/components/answerKey.vue";
  import addPage from "./addPage.vue";
  export default {
    name: 'index',
    components: {
      importComponent,
      addPage,
      answerKey,
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['exam:elQuestionBank:edit','exam:elQuestionBank:del',]),
        //页面状态
        pageType:1,
        importConfig:{
          upLoadApi:'/exam/elQuestionBank/importData',                //上传接口地址
          downloadApi:'/exam/elQuestionBank/importTemplate',          //下载模板接口地址
          downloadCustomKey:null,                                     //下载模板自定义参数KEY(非必填)
          downloadCustomValue:null,                                   //下载模板自定义参数VALUE(非必填)
          loseApi:'/exam/elQuestionBank/exportErrorData',             //失败报表接口地址
          loseCustomKey:null,                                         //失败报表自定义参数KEY(非必填)
          loseCustomValue:null,                                       //失败报表自定义参数VALUE(非必填)
          fileName:'导入模板-',                                       //下载模板命名
        },
        //下拉列表数据
        optionListA:[{value:1,label:'共享'},{value:0,label:'不共享'}],
        optionListB:[],
        optionListC:[{value:1,label:'单选'},{value:2,label:'多选'},{value:3,label:'判断'}],
        optionListD:[{value:1,label:'简单'},{value:2,label:'一般'},{value:3,label:'较难'},{value:4,label:'极难'}],
        disciplineOptionList:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          shareStatus:null,
          knowledgePointId :null,
          disciplineTypeId: '',
          questionType :null,
          difficulty :null,
          questionContent :'',
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
        answerKeyPropsData:{},
        //弹窗相关
        drawerTitle: '',
        drawerType: '',
        drawer: false
      }
    },
    created () {

    },
    mounted () {
      this.examElKnowledgePointTreeList();
      this.examElDisciplineTypeList();
      this.getList();
    },
    methods: {
      //清洗富文本
      cleanRichText(htmlStr, maxLength = 50) {
        if (!htmlStr) return '';
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = htmlStr;
        let text = tempDiv.textContent || tempDiv.innerText || '';
        text = text.replace(/\s+/g, ' ').trim();
        if (maxLength && text.length > maxLength) {
          return text.substring(0, maxLength) + '...';
        }
        return text;
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          shareStatus:null,
          knowledgePointId :null,
          disciplineTypeId: '',
          questionType :null,
          difficulty :null,
          questionContent :'',
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        examElQuestionBankList(obj).then(response => {
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'pageType',2);
          this.$set(this,'propsData',{});
        }else if(type == 2){
          //预览
          this.$set(this, 'drawerTitle', '试题预览')
          this.$set(this, 'drawerType', 2)
          this.$set(this, 'drawer', true)
          this.$set(this,'answerKeyPropsData',{
            id:row.id,
          });
        }else if(type == 3){
          //编辑
          this.$set(this,'pageType',2);
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'propsData',obj);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            examElQuestionBankDel({ids:[row.id]}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      examElKnowledgePointTreeList(){
        examElKnowledgePointTreeList({ dataScope:2 }).then(response => {
          const list = response.data || [];
          this.formatTreeData(list);
          this.$set(this, 'optionListB', list);
        });
      },
      //查询全部专业类别
      examElDisciplineTypeList(){
        examElDisciplineTypeList({}).then(response => {
          this.$set(this,'disciplineOptionList',response.data);
        });
      },
    },
  }
</script>
<style scoped lang="scss">
  .questionBankManagement{
    .questionBankManagementPage{

    }
  }
</style>
