<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.PlanId==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.PlanName" ref="PlanName"
            class="col-6 q-pr-lg q-pb-lg" label="教案名称" :rules="[val => !!val || '请填写教案名称']" />
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6">课前
        </div>
        <ext-editor :defaultMsg="objOption.ClassBefore" classStr="col-12" @getEditValue="getEditValue1">
        </ext-editor>
        <div class="text-caption q-my-md q-px-xs text-grey-6">课中
        </div>
        <ext-editor :defaultMsg="objOption.ClassIn" classStr="col-12" @getEditValue="getEditValue2">
        </ext-editor>
        <div class="text-caption q-my-md q-px-xs text-grey-6">课后
        </div>
        <ext-editor :defaultMsg="objOption.ClassAfter" classStr="col-12" @getEditValue="getEditValue3">
        </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 {
    queryTeachPlanInfo,
    saveTeachPlanInfo
  } from '../../api/course/index'

  import extEditor from '../common/ext-editor'
  export default {
    components: {
      extEditor
    },
    props: {
      CourseId: {
        type: String,
        default: "0"
      },
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          PlanId: 0, //教案编号
          CourseId: 0, //课程编辑
          PlanName: "", //教案名称
          ClassBefore: "", //课前
          ClassIn: "", //课中
          ClassAfter: "", //课后
          ParentPlanId: 0, //父级节点编号
          SourceUrl: "", //源文件路径
          PlanFileExtension: "", //文件后缀名
          PlanNodeType: 0, //教案类型(0-自动添加教案,1-上传教案)
          ExtUrl: "", //上传生成文件路径
          Remark: "", //备注
        },
        optionTitle: "",
        saveCourseLoading: false,
      }
    },
    mounted() {
      this.initObj()
    },
    methods: {
      getEditValue1(obj) {
        this.objOption.ClassBefore = obj;
      },
      getEditValue2(obj) {
        this.objOption.ClassIn = obj;
      },
      getEditValue3(obj) {
        this.objOption.ClassAfter = obj;
      },
      uploadFile(files) {
        UploadSelfFile('course', files[0], res => {
          if (res.resultCode == 1) {
            this.objOption.CoverImg = res.FileUrl;
          }
        })
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.PlanId > 0) {
          queryTeachPlanInfo({
            PlanId: this.saveObj.PlanId
          }).then(res => {
            this.objOption.PlanName = res.Data.PlanName;
            this.objOption.PlanId = res.Data.PlanId;
            this.objOption.CourseId = res.Data.CourseId;
            this.objOption.ClassBefore = res.Data.ClassBefore;
            this.objOption.ClassIn = res.Data.ClassIn;
            this.objOption.ClassAfter = res.Data.ClassAfter;
            this.objOption.ParentPlanId = res.Data.ParentPlanId;
            this.objOption.SourceUrl = res.Data.SourceUrl;
            this.objOption.PlanFileExtension = res.Data.PlanFileExtension;
            this.objOption.PlanNodeType = res.Data.PlanNodeType;
            this.objOption.ExtUrl = res.Data.ExtUrl;
            this.objOption.Remark = res.Data.Remark;
          })
          this.optionTitle = "修改教案信息"
        } else {
          this.optionTitle = "新增教案"
        }
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveCourse() {
        this.objOption.CourseId = this.CourseId;
        this.saveCourseLoading = true;
        saveTeachPlanInfo(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>
