<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" 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="row wrap">
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.JobName" ref="JobName"
            class="col-6 q-pr-lg q-pb-lg" label="作业名称" :rules="[val => !!val || '请填写作业名称']" />
        </div>
        <div class="row wrap">
          <span>
            作业类型
            <q-radio v-model="objOption.JobType" :val="0" label="题目型作业" />
            <q-radio v-model="objOption.JobType" :val="1" label="答题卡作业" />
          </span>
        </div>
        <div class="row wrap">
          <span>
            评分机制
            <q-radio v-model="objOption.ScoreType" :val="1" label="百分制（平均分配每道题的分值）" />
            <q-radio v-model="objOption.ScoreType" :val="2" label="百分制（平均分配每道题的分值）" />
          </span>
        </div>
        <div class="row wrap">
          <span>
            题型设置
            <q-radio v-model="objOption.TopicType" :val="1" label="按题型归类" />
            <q-radio v-model="objOption.TopicType" :val="2" label="不按题型归类" />
          </span>
        </div>
        <el-upload class="avatar-uploader" action="" :show-file-list="false" :http-request="UploadAttachment">
          <div class="app-upload" flex="main:center cross:center" style="width: 100px; height: 100px;">
            <i class="el-icon-upload"></i>
          </div>
        </el-upload>
      </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 {
    queryCourseCategoryTree,
    saveCourseInfo,
    queryCourseInfo,
    querySaleplatList,
  } from '../../api/course/index'
  import {
    getTeacherDropDownList,
  } from '../../api/school/index'
  import {
    UploadSelfFile,
    UploadLocalSystem
  } 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
      },
      CourseId: {
        type: String,
        default: ""
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          JobId: 0, //作业编号
          CourseId: 0, //课程编号
          JobName: "", //作业名称
          JobType: 0, //作业类型(0-题目型作业，1-答题卡作业)
          ScoreType: 1, //评分机制(1-百分制，2-自定义)
          TopicType: 1, //题型设置(1-按题型归类，2-不按题型归类)
        },
        optionTitle: "",
        defaultArray: [],
        ladderPriceList: [], //阶梯定价数组
        saveCourseLoading: false,
        TreeCategoryList: [], //课程分类树形列表
        TeacherList: [], //关联教师下拉数据
        SaleplatList: [], //销售端口
        choosePlat: [], //选择的平台
        chooseTeacher: [], //选择老师
      }
    },
    created() {
      this.GetTeacherList();
      this.getSaleplat();
    },
    mounted() {
      this.getCategorytree();
      this.initObj()
    },
    methods: {
      UploadAttachment(files) {
        var fileParams = {
          Word: 1,
          Analysis: 1,
          CourseId: this.CourseId,
          Uid:this.getLocalStorage().Id
        }
        UploadLocalSystem(JSON.stringify(fileParams), files.file, res => {
          console.log("res", res);
        })
      },
      //获取教师下拉
      GetTeacherList() {
        getTeacherDropDownList({}).then(res => {
          if (res.Code == 1) {
            this.TeacherList = res.Data;
            var obj = {
              TeacherName: '请选择',
              TId: 0
            }
            this.TeacherList.unshift(obj);
          }
        })
      },
      //获取销售端口列表
      getSaleplat() {
        querySaleplatList().then(res => {
          this.SaleplatList = res.Data;
        })
      },
      //获取编辑器值
      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.Code == 1) {
            this.objOption.CoverImg = res.FileUrl;
          }
        })
      },
      getCategorytree() {
        this.TreeCategoryList = [];
        var qMsg = {}
        queryCourseCategoryTree(qMsg).then(res => {
          this.TreeCategoryList = res.Data;
        })
      },
      //新增阶梯定价
      addStepPrice() {
        var obj = {
          ClassStepPriceId: 0,
          PersionNum: 2,
          PersionPrice: 0
        }
        if (this.ladderPriceList && this.ladderPriceList.length > 0) {
          var _index = this.ladderPriceList.length - 1;
          obj.PersionNum = parseInt(this.ladderPriceList[_index].PersionNum) + 1;
        }
        this.ladderPriceList.push(obj);
      },
      //删除阶梯定价
      delStepPrice(index) {
        this.ladderPriceList.splice(index, 1);
      },
      //初始化表单
      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.objOption.School_Id = res.Data.School_Id;
            this.objOption.Teacher_Ids = res.Data.Teacher_Ids;
            this.objOption.CourseFeature = res.Data.CourseFeature;
            this.objOption.OriginalPrice = res.Data.OriginalPrice;
            this.objOption.SellPrice = res.Data.SellPrice;
            this.objOption.IsOpenStepPrice = res.Data.IsOpenStepPrice;
            this.objOption.IsRenew = res.Data.IsRenew;
            this.objOption.RenewOgPrice = res.Data.RenewOgPrice;
            this.objOption.RenewSlPrice = res.Data.RenewSlPrice;
            this.objOption.Saleplat = res.Data.Saleplat;
            this.objOption.IsKCourse = res.Data.IsKCourse;
            if (res.Data.StepPriceList && res.Data.StepPriceList.length > 0) {
              this.ladderPriceList = res.Data.StepPriceList;
            }
            if (res.Data.TeacherIdList && res.Data.TeacherIdList.length > 0) {
              this.chooseTeacher = res.Data.TeacherIdList;
            }
            if (res.Data.SalePlatList && res.Data.SalePlatList.length > 0) {
              this.choosePlat = res.Data.SalePlatList;
            }
            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;
          this.objOption.School_Id = 0;
          this.objOption.Teacher_Ids = "";
          this.objOption.CourseFeature = "";
          this.objOption.OriginalPrice = 0;
          this.objOption.SellPrice = 0;
          this.objOption.IsOpenStepPrice = 0;
          this.objOption.IsRenew = 0;
          this.objOption.RenewOgPrice = 0;
          this.objOption.RenewSlPrice = 0;
          this.objOption.Saleplat = "";
          this.objOption.IsKCourse = 0;
        }
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveCourse() {
        this.saveCourseLoading = true;
        var Teacher_Ids = "";
        if (this.chooseTeacher && this.chooseTeacher.length > 0) {
          this.chooseTeacher.forEach(item => {
            Teacher_Ids += "," + item;
          })
        }
        if (Teacher_Ids != '') {
          Teacher_Ids = Teacher_Ids.substring(1);
        }
        var salePlat = "";
        if (this.choosePlat && this.choosePlat.length > 0) {
          this.choosePlat.forEach(item => {
            salePlat += "," + item;
          });
        }
        if (salePlat != '') {
          salePlat = salePlat.substring(1);
        }
        this.objOption.StepPriceList = this.ladderPriceList;
        this.objOption.Teacher_Ids = Teacher_Ids;
        this.objOption.Saleplat = salePlat;
        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>
