<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 500px;max-width:500px;">
      <q-card-section>
        <div class="text-h6">{{objOption.PaperId==0?'新增文件夹':'修改文件夹名称'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="row wrap">
          <q-input filled stack-label maxlength="50" :dense="false" v-model="objOption.PaperName" ref="PaperName"
            class="col-12 q-pb-lg" label="文件夹名称" :rules="[val => !!val || '请填写文件夹名称']" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent" class="q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="setPaperFolderInfo" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    GetPaper,
    savePaperFolderInfo
  } from '../../api/teacher/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      FaParentId: {
        type: Number,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          PaperId: 0,
          PaperName: '', //文件夹名称
          PaperType: 1, //试卷类型(1-文件夹，2-试卷)
          ParentId: 0, //上级文件夹编号
        },
        saveLoading: false
      }
    },
    created() {

    },
    mounted() {

      this.initObj()
    },
    methods: {
      initObj() {
        if (this.saveObj) {
          this.optionTitle = "修改文件夹名称"
          this.objOption.PaperId = this.saveObj.PaperId;
          this.objOption.PaperName = this.saveObj.PaperName;
          this.objOption.PaperType = this.saveObj.PaperType;
          this.objOption.ParentId = this.saveObj.ParentId;
        } else {
          this.optionTitle = "新增文件夹"
        }
      },
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      setPaperFolderInfo() {
        this.$refs.PaperName.validate();
        if (!this.$refs.PaperName.hasError) {
          this.saveLoading = true;
          if (this.FaParentId) {
            this.objOption.ParentId = this.FaParentId;
          }
          savePaperFolderInfo(this.objOption).then(res => {
            this.saveLoading = false
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeSaveForm()
          }).catch(() => {
            this.saveLoading = false
          })
        }
      },
    },
  }

</script>
<style>
</style>
