<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="50" :dense="false" v-model="objOption.CateName" ref="CateName"
            class="col-6 q-pr-lg q-pb-lg" label="分类名称" :rules="[val => !!val || '请填写分类名称']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.SortNum" ref="SortNum"
            class="col-6 q-pr-lg q-pb-lg" label="排序" />
          <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>
          <div class="col-12 q-pr-lg q-pb-lg q-pt-lg">
            上级分类:
            <div class="q-pa-md q-gutter-sm">
              <q-tree v-if="isShow" style="max-width:300px;" :nodes="TreeCategoryList" node-key="CateId"
                label-key="CateName" children-key="ChildList" tick-strategy="strict" :default-expand-all="true"
                no-connectors :ticked.sync="chooseCategroyArray">
              </q-tree>
            </div>
          </div>
        </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
  } 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-禁用)},
        },
        optionTitle: "",
        //树形课程分类列表
        TreeCategoryList: [],
        saveLoading: false,
        chooseCategroyArray: [],
      }
    },
    mounted() {
      this.getCategoryTreeList();
      this.initObj()
    },
    methods: {
      //获取树形课程分类列表
      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;
            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>
