<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.CateId==0?'新增系列':'修改系列'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">系列信息</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="30" :dense="false" v-model="objOption.CateName" ref="CateName"
            class="col-6 q-pr-lg q-pb-lg" label="系列名称" :rules="[val => !!val || '系列名称']" />
          <q-select filled class="col-6 q-pr-lg q-pb-lg" v-model="objOption.CourseSubject" :options="CourseSubjectList"
            option-label="SubjectName" option-value="Id" emit-value map-options label="所属科目" clearable />
        </div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="5" :dense="false" v-model="objOption.SortNum" ref="SortNum"
            class="col-6 q-pr-lg q-pb-lg" label="排序" @keyup.native="checkInteger(objOption,'SortNum')" />
        </div>
        <div class="col-12 q-pr-lg q-pb-lg q-pt-lg">
          <q-toggle size="md" label="状态" color="primary" :false-value="1" :true-value="0" v-model="objOption.Status"
            title="注意：关闭后，分类将无法正常使用." />
        </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 q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveCategory" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    queryCourseCategoryTree,
    queryCourseCategoryInfo,
    saveCourseCategoryInfo,
    getCourseSubject
  } from '../../api/course/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        isShow: false,
        persistent: true,
        objOption: {
          CateId: 0, //分类编号
          ParentId: 0, //父级编号
          CateName: "", //系列名称
          SortNum: 0, //排序
          Status: 0, //状态(0-正常,1-禁用)},
          CourseSubject: ''
        },
        optionTitle: "",
        //树形课程分类列表
        TreeCategoryList: [],
        saveLoading: false,
        chooseCategroyArray: [],
        CourseSubjectList: []
      }
    },
    mounted() {
      this.getCategoryTreeList();
      this.initObj();
      this.queryCourseSubject();
    },
    methods: {
      queryCourseSubject() {
        getCourseSubject({}).then(res => {
          if (res.Code == 1) {
            this.CourseSubjectList = res.Data;
          }
        })
      },
      //获取树形课程分类列表
      getCategoryTreeList() {
        this.TreeCategoryList = [];
        var qMsg = {}
        queryCourseCategoryTree(qMsg).then(res => {
          this.TreeCategoryList = res.Data;
          this.isShow = true;
        })
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.CateId > 0) {
          queryCourseCategoryInfo({
            CateId: this.saveObj.CateId
          }).then(res => {
            this.objOption.CateId = res.Data.CateId;
            this.objOption.ParentId = res.Data.ParentId;
            this.objOption.CateName = res.Data.CateName;
            this.objOption.SortNum = res.Data.SortNum;
            this.objOption.Status = res.Data.Status;
            this.objOption.CourseSubject = res.Data.CourseSubject;
            if (res.Data.ParentId && res.Data.ParentId > 0) {
              this.chooseCategroyArray.push(res.Data.ParentId);
            }
          })
          this.optionTitle = "修改系列信息"
        } else {
          this.optionTitle = "新增系列"
          this.objOption.CateId = 0;
          this.objOption.ParentId = 0;
          this.objOption.CateName = "";
          this.objOption.SortNum = 0;
          this.objOption.Status = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveCategory() {
        var tempKeys = "";
        if (this.chooseCategroyArray && this.chooseCategroyArray.length > 0) {
          this.chooseCategroyArray.forEach(item => {
            this.objOption.ParentId = item;
          })
        }
        this.saveLoading = true
        saveCourseCategoryInfo(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
        })
      }
    },
    watch: {
      chooseCategroyArray: function (val, oldval) {
        if (this.chooseCategroyArray.length > 1) {
          var end = this.chooseCategroyArray[this.chooseCategroyArray.length - 1];
          var tempArray = [];
          tempArray.push(end);
          this.chooseCategroyArray = tempArray;
        }
      }
    }
  }
</script>