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

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

  .QuestionisVisible {
    visibility: hidden;
  }

</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="Id" option-label="Name"
            v-model="msg.QCategoryId" :options="questionCategoryList" emit-value map-options label="分类" multiple
            clearable />
        </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 sticky-right-column-table"
        separator="none" :data="data" :columns="columns" row-key="QuestionId" :selection="editStatus?'multiple':'none'"
        :selected.sync="selectedQuestion">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">问题列表</div>
          <q-space />
          <div class="page-option" v-if="editStatus">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增题目" @click="EditQuestion(null)" />
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="批量设置" title="批量设置考级程度"
              @click="isShowEdit=true">
              <q-popup-proxy>
                <q-banner v-if="isShowEdit">
                  <div class="calenderDialog">
                    <div style="margin:10px 0 15px 0;">设置考级程度</div>
                    <q-select standout="bg-primary text-white" option-value="Id" option-label="Name"
                      v-model="levelTypeMsg.LevelType" :options="questionLevalTypeList" emit-value map-options
                      label="考级程度" />
                    <q-card-actions align="right" class="bg-white">
                      <q-btn label="取消" flat color="grey-10" @click="isShowEdit=false"
                        style="font-weight:400 !important" />
                      <q-btn label="确认" color="accent q-px-md" style="font-weight:400 !important"
                        @click="SetBatchLevelType()" />
                    </q-card-actions>
                  </div>
                </q-banner>
              </q-popup-proxy>
            </q-btn>
            <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">
          <td style="width:400px;cursor:pointer;">
            <div @click="goDetail(props.row)" v-html="props.row.Title" class="quetion_Title">
            </div>
          </td>
        </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 v-if="editStatus" flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditQuestion(props.row)" />
            <q-btn v-if="editStatus" flat size="xs" icon="edit" color="negative" style="font-weight:400" label="删除"
              @click="setQuestionStatus(props.row)"></q-btn>
            <q-btn v-if="editStatus" flat size="xs" icon="iconfont icon-shangyi2" :class="{'QuestionisVisible':!props.row.isShowFirst}"
              color="accent" title="上移" style="font-weight:400;" label="" @click="getUpStatus(props.row,1)"></q-btn>
            <q-btn v-if="editStatus" flat size="xs" icon="iconfont icon-xiayi" :class="{'QuestionisVisible':!props.row.isShowLast}"
              color="accent" title="下移" style="font-weight:400;" label="" @click="getUpStatus(props.row,2)"></q-btn>
            <q-btn v-if="editStatus" flat size="xs" icon="iconfont icon-fuzhi" color="accent" style="font-weight:400;" label=""
              @click="getCopyItem(props.row)"></q-btn>
            <q-btn v-if="!editStatus" flat size="xs" color="primary" style="font-weight:400" label="查看"
              @click="goDetail(props.row)"/>
          </q-td>
        </template>
      </q-table>
      <question-form v-if="isShowQuestionForm" :CourseId="msg.CourseId" :BankId="msg.BankId" :seting-obj="questionObj"
        @close="closeQuestion" @success="refreshQuestion">
      </question-form>
      <question-upload v-if="isShowImportUpload" :CourseId="msg.CourseId" :BankId="msg.BankId" :importType="importType"
        @close="closeQuestion" @success="refreshQuestion">
      </question-upload>
    </div>
  </div>
</template>
<script>
  import {
    queryQuestionPageList,
    queryQuestionTypeList,
    deleteQuestion,
    queryDifficultyType,
    queryQuestionCategory,
    setQuestionSort,
    CopyQuestion,
    queryQuestionLevelType,
    saveBatchQuestionLevel
  } from '../../api/question/question';
  import questionForm from '../question/question-form';
  import questionUpload from '../question/question-upload';
  export default {
    props: {
      queryMsg: {
        type: Object,
      },
      editStatus:{
        type: Boolean,
        default: true
      }
    },
    meta: {
      title: "问题列表"
    },
    components: {
      questionForm,
      questionUpload
    },
    data() {
      return {
        columns: [{
            name: 'Number',
            label: '编号',
            field: 'Number',
            align: 'left'
          },
          {
            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: 'LevelTypeName',
            label: '考级程度',
            field: 'LevelTypeName',
            align: 'left'
          },
          {
            name: 'CreateByName',
            label: '创建者',
            field: 'CreateByName',
            align: 'left'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            field: 'CreateTimeStr',
            align: 'left'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'QuestionId',
            align: 'center'
          }
        ],
        data: [],
        loading: false,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          CourseId: 0,
          BankId: 0,
          Title: "",
          PointName: "", //知识点
          QuestionTypeId: [], //题型
          DifficultyType: [], //难易程度
          QCategoryId: [], //分类
        },
        pageCount: 0,
        questionTypeList: [], //问题类型列表
        questionDifficultyTypeList: [], //问题难易程度列表
        isShowQuestionForm: false,
        questionObj: null,
        importType: 0, //导入类型(1-模板导入;2-智能导入)
        isShowImportUpload: false, //是否显示导入模板
        questionCategoryList: [], //问题分类
        selectedQuestion: [], //选中的数组
        isShowEdit: false, //是否显示弹窗
        questionLevalTypeList: [], //问题考级程度列表
        levelTypeMsg: {
          LevelType: 0,
        }
      }
    },
    created() {
      if (this.queryMsg.BankId) {
        this.msg.BankId = this.queryMsg.BankId;
      }
      if (this.queryMsg.CourseId) {
        this.msg.CourseId = this.queryMsg.CourseId;
      }
      this.getQuestionType();
      this.getDifficultyType();
      this.getQuestionCategory();
      this.getQuestionLevelType();
    },
    mounted() {
      this.getQuestionList();
    },
    methods: {
      //获取问题考级程度
      getQuestionLevelType() {
        queryQuestionLevelType().then(res => {
          var tempData = res.Data;
          var obj = {
            Name: '请选择',
            Id: 0
          }
          tempData.unshift(obj);
          this.questionLevalTypeList = tempData;
        })
      },
      //获取问题大类
      getQuestionCategory() {
        queryQuestionCategory().then(res => {
          this.questionCategoryList = res.Data;
        })
      },
      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;
          }
        });
      },
      //批量设置考级程度
      SetBatchLevelType() {
        if (this.selectedQuestion && this.selectedQuestion.length > 0) {
          var levelMsg = {
            LevelType: this.levelTypeMsg.LevelType,
            questionIds: [],
          };
          this.selectedQuestion.forEach(item => {
            levelMsg.questionIds.push(item.QuestionId)
          })
          saveBatchQuestionLevel(levelMsg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '设置成功!',
                position: 'top'
              })
              this.getQuestionList();
              this.isShowEdit = false;
              this.selectedQuestion = [];
              this.levelTypeMsg.LevelType = 0;
            }
          })
        } else {
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: "请选择问题",
            position: 'top'
          })
        }
      },
      //删除问题
      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;
          this.data.forEach((x, index) => {
            if (index == 0) {
              x.isShowFirst = false
            } else {
              x.isShowFirst = true
            }
            if (index == this.data.length - 1) {
              x.isShowLast = false
            } else {
              x.isShowLast = true
            }
          })
        }).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;
      },
      //上移
      getUpStatus(item, num) {
        var QuestionId = item.QuestionId;
        var index = -1;
        for (var i = 0; i < this.data.length; i++) {
          if (this.data[i].QuestionId == QuestionId) {
            index = i;
            break;
          }
        }
        if (num == 1) { //上移
          var targetQId = this.data[index - 1].QuestionId;
        }
        if (num == 2) { //下移
          var targetQId = this.data[index + 1].QuestionId;
        }
        var msg = {
          curQId: QuestionId,
          targetQId: targetQId
        }
        setQuestionSort(msg).then(res => {
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '操作成功!',
              position: 'top'
            })
            this.getQuestionList();
          }
        });
      },
      //复制
      getCopyItem(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) {
              CopyQuestion({
                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'
              })
            }
          });
        });
      },
      //点击跳转
      goDetail(item){
        this.OpenNewUrl('/course/questionInfo',{
          'QuestionId':item.QuestionId
        })
      }
    }
  }

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

</style>
