<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">上传教案信息</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-uploader style="display: inline-block;max-height: 320px;max-width: 100%; background-repeat:no-repeat"
            max-files="1" hide-upload-btn label="教案" accept=".doc,.docx,.ppt,.pptx,.xls,.xlsx,.pdf" auto-upload
            :factory="uploadFile" no-thumbnails>
          </q-uploader>
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6">附件
        </div>
        <div class="text-caption q-my-md q-px-xs text-grey-6" v-if="objOption.SourceUrl!=''">
          <a :href="objOption.SourceUrl" :download="objOption.PlanName">{{objOption.PlanName}}</a>
          <div class="col-6">
            <template v-for="(item,index) in tempFileList">
              <q-img :src="item" style="height: 140px; max-width: 150px" :key="index"></q-img>
            </template>
          </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="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 {
    saveTeachPlanInfo,
    queryTeachPlanInfo,
  } from '../../api/course/index'
  import {
    UploadSelfFile,
    getFileExt
  } from '../../api/common/common'

  export default {
    components: {

    },
    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: 1, //教案类型(0-自动添加教案,1-上传教案)
          ExtUrl: "", //上传生成文件路径
          Remark: "", //备注
        },
        optionTitle: "",
        saveCourseLoading: false,
        tempFileList: [], //生成文件图片路径
      }
    },
    mounted() {
      this.initObj()
    },
    methods: {
      uploadFile(files) {
        UploadSelfFile('assistIcon', files[0], 1, res => {
          if (res.Code == 1) {
            this.objOption.PlanName = res.FileName;
            this.objOption.PlanFileExtension = getFileExt(res.FileName);
            this.objOption.SourceUrl = res.FileUrl;
            if (res.ExtFile && res.ExtFile.length > 0) {
              this.objOption.ExtUrl = res.ExtFile.toString();
            }
          }
        })
      },
      //初始化表单
      initObj() {
        this.optionTitle = "";
        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.tempFileList = res.Data.FileList;
          })
          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>