<template>
  <q-dialog v-model="persistent" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.CourseId==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="20" :dense="false" v-model="objOption.CourseName" ref="CourseName"
            class="col-6 q-pr-lg q-pb-lg" label="课程名称" :rules="[val => !!val || '请填写课程名称']" />
          <div class="col-6 q-pr-lg q-pb-lg">
            <selectTree v-if="TreeCategoryList&&TreeCategoryList.length>0" :treeData='TreeCategoryList'
              :defaultArray="defaultArray" nodeKey="CateId" :multiple="false" labelKey="CateName"
              childrenKey="ChildList" tipText="课程分类" @getChild="getChild"></selectTree>
          </div>
          <div class="col-6 q-pb-lg upload-assiatant-box">
            <q-uploader :style="{backgroundImage:'url(' + objOption.CoverImg + ')'}" flat hide-upload-btn max-files="1"
              label="课程封面" :max-file-size="512*1024" accept=".jpg, image/*" :factory="uploadFile" auto-upload>
            </q-uploader>
          </div>
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6">课程介绍
        </div>
        <ext-editor :defaultMsg="objOption.CourseIntro" classStr="col-12" @getEditValue="getEditValue"></ext-editor>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" color="dark" style="font-weight:400 !important" @click="closeCourseForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveCourseLoading"
          @click="saveCourse" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    queryCourseCategoryTree,
    saveCourseInfo,
    queryCourseInfo,
  } from '../../api/course/index'
  import {
    UploadSelfFile
  } from '../../api/common/common'
  import selectTree from '../common/select-tree'
  import extEditor from '../common/ext-editor'
  export default {
    components: {
      selectTree,
      extEditor,
    },
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          CourseId: 0, //课程编号
          CoverImg: "", //课程封面图
          CourseName: "", //课程名称
          CourseIntro: "", //课程介绍
          CateId: 0, //课程编号
        },
        optionTitle: "",
        defaultArray: [],
        saveCourseLoading: false,
        TreeCategoryList: [], //课程分类树形列表
      }
    },
    mounted() {
      this.getCategorytree();
      this.initObj()
    },
    methods: {
      //获取编辑器值
      getEditValue(obj) {
        this.objOption.CourseIntro = obj;
      },
      getChild(obj) {
        if (obj == "") {
          this.objOption.CateId = 0;
        } else {
          this.objOption.CateId = obj;
        }
      },
      uploadFile(files) {
        UploadSelfFile('course', files[0], res => {
          if (res.resultCode == 1) {
            this.objOption.CoverImg = res.FileUrl;
          }
        })
      },
      getCategorytree() {
        this.TreeCategoryList = [];
        var qMsg = {}
        queryCourseCategoryTree(qMsg).then(res => {
          this.TreeCategoryList = res.Data;
        })
      },
      //初始化表单
      initObj() {
        this.defaultArray = [];
        if (this.saveObj && this.saveObj.CourseId > 0) {
          queryCourseInfo({
            CourseId: this.saveObj.CourseId
          }).then(res => {
            this.objOption.CourseId = res.Data.CourseId;
            this.objOption.CoverImg = res.Data.CoverImg;
            this.objOption.CourseName = res.Data.CourseName;
            this.objOption.CourseIntro = res.Data.CourseIntro;
            this.objOption.CateId = res.Data.CateId;
            this.defaultArray.push(res.Data.CateId);
          })
          this.optionTitle = "修改课程信息"
        } else {
          this.optionTitle = "新增课程"
          this.objOption.CourseId = 0;
          this.objOption.CoverImg = "";
          this.objOption.CourseName = "";
          this.objOption.CourseIntro = "";
          this.objOption.CateId = 0;
        }
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveCourse() {
        this.saveCourseLoading = true
        saveCourseInfo(this.objOption).then(res => {
          this.saveCourseLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveCourseLoading = false
        })
      }
    },
  }

</script>
