<style>
  .page-content p {
    margin: 16px 0 0 0 !important;
  }

  .quetion_Title {
    background: transparent !important;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    width: 280px;

  }

</style>
<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="research" clearable standout="bg-primary text-white" v-model="msg.Title" label="关键字"
            @clear="research" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="research" standout="bg-primary text-white" option-value="QId" option-label="Name"
            v-model="msg.QuestionTypeId" :options="questionTypeList" emit-value map-options label="题型" multiple
            clearable />
        </div>
        <div class="col-3">
          <q-input @change="research" clearable standout="bg-primary text-white" v-model="msg.PointName" label="知识点"
            @clear="research" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="research" standout="bg-primary text-white" option-value="Id" option-label="Name"
            v-model="msg.DifficultyType" :options="questionDifficultyTypeList" emit-value map-options label="难度"
            multiple clearable />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">题库列表</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增题目" @click="EditQuestion(null)" />
            <q-btn-dropdown color="accent" label="批量导入" size="sm" class="q-mr-md" style="margin-left:5px;">
              <q-list>
                <q-item clickable v-close-popup>
                  <q-item-section @click="importQuestion(1)">
                    <q-item-label>模板导入(EXCEL)</q-item-label>
                  </q-item-section>
                </q-item>
                <q-item clickable v-close-popup>
                  <q-item-section @click="importQuestion(2)">
                    <q-item-label>智能导入(WORD)</q-item-label>
                  </q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </div>
        </template>
        <template v-slot:body-cell-Title="props">
          <div v-html="props.row.Title" class="quetion_Title">
          </div>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditQuestion(props.row)" />
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="删除"
              @click="setQuestionStatus(props.row)"></q-btn>
          </q-td>
        </template>
      </q-table>
      <question-form v-if="isShowQuestionForm" :CourseId="msg.CourseId" :seting-obj="questionObj" @close="closeQuestion"
        @success="refreshQuestion">
      </question-form>
      <question-upload v-if="isShowImportUpload" :CourseId="msg.CourseId" :importType="importType"
        @close="closeQuestion" @success="refreshQuestion">
      </question-upload>
    </div>
  </div>
</template>
<script>
  import {
    queryQuestionPageList,
    queryQuestionTypeList,
    deleteQuestion,
    queryDifficultyType
  } from '../../api/question/question';
  import questionForm from '../../components/question/question-form';
  import questionUpload from '../../components/question/question-upload';
  export default {
    meta: {
      title: "题库列表"
    },
    components: {
      questionForm,
      questionUpload
    },
    data() {
      return {
        columns: [{
            name: 'Title',
            label: '题目',
            field: 'Title',
            align: 'left'
          },
          {
            name: 'CategoryName',
            label: '大类',
            field: 'CategoryName',
            align: 'left',
          },
          {
            name: 'QuestionTypeName',
            label: '题型',
            field: 'QuestionTypeName',
            align: 'left',
          },
          {
            name: 'DifficultyTypeName',
            label: '难易',
            field: 'DifficultyTypeName',
            align: 'left'
          },
          {
            name: 'CreateByName',
            label: '创建者',
            field: 'CreateByName',
            align: 'left'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            field: 'CreateTimeStr',
            align: 'left'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'QuestionId'
          }
        ],
        data: [],
        loading: false,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          CourseId: 0,
          Title: "",
          PointName: "", //知识点
          QuestionTypeId: [], //题型
          DifficultyType: [], //难易程度
        },
        pageCount: 0,
        questionTypeList: [], //问题类型列表
        questionDifficultyTypeList: [], //问题难易程度列表
        isShowQuestionForm: false,
        questionObj: null,
        importType: 0, //导入类型(1-模板导入;2-智能导入)
        isShowImportUpload: false, //是否显示导入模板
      }
    },
    created() {
      if (this.$route.query) {
        this.msg.CourseId = this.$route.query.CourseId
      }
      this.getQuestionType();
      this.getDifficultyType();
    },
    mounted() {
      this.getQuestionList();
    },
    methods: {
      research() {
        this.msg.pageIndex = 1;
        this.getQuestionList();
      },
      importQuestion(type) {
        this.importType = type;
        this.isShowImportUpload = true;
      },
      //获取题型列表
      getQuestionType() {
        queryQuestionTypeList({}).then(res => {
          if (res.Code == 1) {
            this.questionTypeList = res.Data;
          }
        });
      },
      //获取问题难易程度列表
      getDifficultyType() {
        queryDifficultyType().then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            tempArray.splice(0, 1);
            this.questionDifficultyTypeList = tempArray;
          }
        });
      },
      //删除问题
      setQuestionStatus(item) {
        this.$q.dialog({
          title: "删除问题",
          message: "你正在进行删除问题行为，一旦执行无法找回，是否确认执行？",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          deleteQuestion({
            QuestionId: item.QuestionId
          }).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '删除成功!',
                position: 'top'
              })
              this.getQuestionList();
            } else {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: res.Message,
                position: 'top'
              })
            }
          });
        });
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getQuestionList()
      },
      //获取问题分页列表
      getQuestionList() {
        this.loading = true;
        queryQuestionPageList(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshQuestion() {
        this.isShowQuestionForm = false;
        this.isShowImportUpload = false;
        this.getQuestionList();
      },
      //新增修改问题
      EditQuestion(obj) {
        if (obj) {
          this.questionObj = obj
        } else {
          this.questionObj = null
        }
        this.isShowQuestionForm = true
      },
      //关闭弹窗
      closeQuestion() {
        this.isShowQuestionForm = false;
        this.questionObj = null;
        this.isShowImportUpload = false;
        this.importType = 0;
      },
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
