<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="getQuestionList" clearable standout="bg-primary text-white" v-model="msg.Title" label="关键字"
            @clear="getQuestionList" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="getQuestionList" 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="getQuestionList" clearable standout="bg-primary text-white" v-model="msg.TeacherName"
            label="知识点" @clear="getQuestionList" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="getQuestionList" 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)" />
          </div>
        </template>
        <template v-slot:body-cell-Title="props">
          <span v-html="props.row.Title">
          </span>
        </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="setQuestionStatus(props.row)"></q-btn>
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditQuestion(props.row)" />
          </q-td>
        </template>
      </q-table>
      <question-form v-if="isShowQuestionForm" :CourseId="msg.CourseId" :seting-obj="questionObj" @close="closeQuestion"
        @success="refreshQuestion">
      </question-form>
    </div>
  </div>
</template>
<script>
  import {
    queryQuestionPageList,
    queryQuestionTypeList,
    deleteQuestion,
    queryDifficultyType
  } from '../../api/question/question';
  import questionForm from '../../components/question/question-form';
  export default {
    meta: {
      title: "题库列表"
    },
    components: {
      questionForm
    },
    data() {
      return {
        columns: [{
            name: 'Title',
            label: '题目',
            field: 'Title',
            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: "",
          QuestionTypeId: [], //题型
          DifficultyType: [], //难易程度
        },
        pageCount: 0,
        questionTypeList: [], //问题类型列表
        questionDifficultyTypeList: [], //问题难易程度列表
        isShowQuestionForm: false,
        questionObj: null,
      }
    },
    created() {
      if (this.$route.query) {
        this.msg.CourseId = this.$route.query.CourseId
      }
      this.getQuestionType();
      this.getDifficultyType();
    },
    mounted() {
      this.getQuestionList();
    },
    methods: {
      //获取题型列表
      getQuestionType() {
        queryQuestionTypeList({}).then(res => {
          if (res.Code == 1) {
            this.questionTypeList = res.Data;
          }
        });
      },
      //获取问题难易程度列表
      getDifficultyType() {
        queryDifficultyType().then(res => {
          if (res.Code == 1) {
            this.questionDifficultyTypeList = res.Data;
          }
        });
      },
      //删除问题
      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.getQuestionList();
      },
      //新增修改问题
      EditQuestion(obj) {
        if (obj) {
          this.questionObj = obj
        } else {
          this.questionObj = null
        }
        this.isShowQuestionForm = true
      },
      //关闭弹窗
      closeQuestion() {
        this.isShowQuestionForm = false;
        this.questionObj = null;
      },
    }
  }

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

</style>
