<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
    <q-card style="width: 500px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{(questionBankObj&&questionBankObj.BankId>0)?"修改题库":"创建题库"}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <q-input filled stack-label :dense="false" class="col-6 q-pb-lg" ref="BankNo" v-model="msg.BankNo"
          label="请输入题库序号" maxlength="20" :rules="[val => !!val || '请输入题库序号']" />

        <q-input filled stack-label :dense="false" class="col-6 q-pb-lg" ref="BankName" v-model="msg.BankName"
          label="请输入题库名称" maxlength="20" :rules="[val => !!val || '请输入题库名称']" />

        <q-select stack-label color="primary" filled clearable label="等级" option-value="Id" option-label="Name"
          :options="BankTypeList" v-model="msg.BankType" ref="ActivityType" emit-value map-options
          :rules="[val => !!val || '请选择题库等级']" />
      </q-card-section>
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeQuestionForm" />
        <q-btn label="创建" color="accent q-px-md" style="font-weight:400 !important" @click="saveQuestion" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getQuestionBankInfo, //获取题库详情
    SetQuestionBank, //保存图库
    queryBankTypeList
  } from '../../api/question/question'
  export default {
    props: {
      questionBankObj: {
        type: Object,
        default: null
      }
    },
    components: {

    },
    data() {
      return {
        msg: {
          BankId: 0,
          BankNo: "", //题库序号
          BankName: '', //题库名称
          BankType: "", //题库类型
        },
        BankTypeList: [], //题库等级列表
        persistent: true
      }
    },
    computed: {},
    created() {
      this.getBankTypeList();
    },
    mounted() {
      this.initQuestionBank();
    },
    methods: {
      //获取题库等级列表
      getBankTypeList() {
        queryBankTypeList({}).then(res => {
          if (res.Code == 1) {
            this.BankTypeList = res.Data;
          }
        })
      },
      initQuestionBank() {
        if (this.questionBankObj && this.questionBankObj.BankId > 0) {
          getQuestionBankInfo({
            BankId: this.questionBankObj.BankId
          }).then(res => {
            if (res.Code == 1) {
              this.msg.BankId = res.Data.BankId;
              this.msg.BankNo = res.Data.BankNo;
              this.msg.BankName = res.Data.BankName;
              this.msg.BankType=res.Data.BankType;
            }
          })
        } else {
          this.msg.BankId = 0;
          this.msg.BankNo = "";
          this.msg.BankName = "";
          this.msg.BankType="";
        }
      },
      //创建
      saveQuestion() {
        this.$refs.BankName.validate()
        if (!this.$refs.BankName.hasError) {
          SetQuestionBank(this.msg).then(res => {
            if (res.Code == 1) {
              this.$emit('success');
              this.closeQuestionForm();
            }
          });
        }
      },
      //取消
      closeQuestionForm() {
        this.$emit('close');
        this.persistent = false
      }
    },
  }
</script>