<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-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, //保存图库
  } from '../../api/question/question'
  export default {
    props: {
      questionBankObj: {
        type: Object,
        default: null
      }
    },
    components: {

    },
    data() {
      return {
        msg: {
          BankId: 0,
          BankNo: "", //题库序号
          BankName: '', //题库名称
        },
        persistent: true
      }
    },
    computed: {},
    created() {},
    mounted() {
      this.initQuestionBank();
    },
    methods: {
      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;
            }
          })
        } else {
          this.msg.BankId = 0;
          this.msg.BankNo = "";
          this.msg.BankName = "";
        }
      },
      //创建
      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>
