<!--新建课程-->
<template>
  <div class="page-container courseManagement-addPage">
    <div class="page-container-top-max-big-box-shadow">
      <p class="top-1-p">{{newData.id?'编辑课程':'新建课程'}}</p>
      <p class="top-2-p" @click="backPage()">返回</p>
      <p class="top-3-p" v-if="newData.id&&!showType" @click="resetButton()">重置</p>
      <p class="top-4-p" v-if="!showType" @click="submitButton()">保存</p>
    </div>
    <div class="bottom-max-big-box">
      <div class="left-max-big-box">
        <p class="left-for-p" :class="item.value == buttonType?'check-button-p':''"
           @click="tableButton(item.value)"
           v-for="(item,index) in buttonList" :key="index">{{item.name}}</p>
      </div>
      <basicSettings ref="basicSettings" class="right-max-big-box" v-show="buttonType == 1"></basicSettings>
      <studyCourses ref="studyCourses" class="right-max-big-box" v-show="buttonType == 2"></studyCourses>
      <afterClassEvaluation class="right-max-big-box" v-show="buttonType == 3"></afterClassEvaluation>
    </div>
  </div>
</template>

<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  //import { getInfo } from "@/api/basicsModules/index";
  import { examElCourseAdd,examElCourseEdit,examElCourseDetail } from "@/api/safetyEducationExaminationNew/index";
  import basicSettings from "./basicSettings.vue";
  import studyCourses from "./studyCourses.vue";
  import afterClassEvaluation from "./afterClassEvaluation.vue";
  export default {
    name: 'addPage',
    components: {
      basicSettings,
      studyCourses,
      afterClassEvaluation,
    },
    props:{
      propsData:{},
    },
    data(){
      return{
        //页面状态
        buttonType:1,
        //菜单按钮
        buttonList:[
          {name:'基本设置',value:'1'},
          {name:'学习课件',value:'2'},
          // {name:'课后考核',value:'3'},
        ],
        newData:{},
        showType:false,
      }
    },
    created(){

    },
    mounted(){
      if (this.propsData.id){
        this.$set(this,'newData',{
          id:this.propsData.id
        });
        this.$nextTick(()=>{
          this.examElCourseDetail();
        })
      }
    },
    methods:{
      //课程详情
      examElCourseDetail(){
        examElCourseDetail({id:this.newData.id}).then(response => {
          this.$refs.basicSettings.setDataInfo({
            disciplineTypeId:response.data.disciplineTypeId,
            shelfStatus:response.data.shelfStatus,
            shareStatus:response.data.shareStatus,
            courseName:response.data.courseName,
            coverAttachmentPath:response.data.coverAttachmentPath,
            popupVerifyEnabled:response.data.popupVerifyEnabled?response.data.popupVerifyEnabled:0,
            popupIntervalMinutes:response.data.popupIntervalMinutes?response.data.popupIntervalMinutes:0,
          })
          let list = [];
          for(let i=0;i<response.data.coursewares.length;i++){
            let index = null
            for(let x=0;x<list.length;x++){
              if(list[x].knowledgePointId == response.data.coursewares[i].knowledgePointId){
                index = x;
              }
            }
            if(index != null){
              for(let o=0;o<response.data.coursewares[i].coursewares.length;o++){
                list[index].minList.push({
                  id:response.data.coursewares[i].coursewares[o].coursewareId,
                  minData1:response.data.coursewares[i].coursewares[o].coursewareName,
                  minData2:response.data.coursewares[i].coursewares[o].deptName,
                  minData3:response.data.coursewares[i].coursewares[o].materialType,
                  minData4:response.data.coursewares[i].coursewares[o].minLearningDuration,
                  minData5:response.data.coursewares[i].coursewares[o].creditHours,
                })
              }
            }else{
              let obj = {
                showType:true,
                data1:response.data.coursewares[i].knowledgePointName,
                data2:0,
                data3:0,
                data4:0,
                knowledgePointId:response.data.coursewares[i].knowledgePointId,
                minList:[],
              };
              for(let o=0;o<response.data.coursewares[i].coursewares.length;o++){
                obj.minList.push({
                  id:response.data.coursewares[i].coursewares[o].coursewareId,
                  minData1:response.data.coursewares[i].coursewares[o].coursewareName,
                  minData2:response.data.coursewares[i].coursewares[o].deptName,
                  minData3:response.data.coursewares[i].coursewares[o].materialType,
                  minData4:response.data.coursewares[i].coursewares[o].minLearningDuration,
                  minData5:response.data.coursewares[i].coursewares[o].creditHours,
                })
              }
              list.push(obj)
            }
          }
          this.$refs.studyCourses.setDataInfo(list);
          if(this.propsData.showType){
            this.$set(this,'showType',true);
            this.$refs.studyCourses.setShowType();
            this.$refs.basicSettings.setShowType();
          }
        });
      },
      //切换按钮
      tableButton(val){
        if(this.buttonType != val){
          this.$set(this,'buttonType',val);
        }
      },
      //重置
      resetButton(){
        let self = this;
        this.$confirm('是否确认重置?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        }).then(() => {
          self.examElCourseDetail();
        }).catch(() => {});
      },
      // 返回按钮
      backPage(){
        this.$parent.tableButton(6);
      },
      //提交
      submitButton(){
        if(this.buttonType == 1){
          this.$refs.basicSettings.submitButton()
        }else{
          this.$refs.basicSettings.checkTheData()
        }
      },
      pageDataA(){
        this.$refs.studyCourses.checkTheData()
      },
      pageDataB(){
        let self = this;
        this.$confirm('是否确认保存?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        }).then(() => {
          let basicData = JSON.parse(JSON.stringify(this.$refs.basicSettings.newData))
          let studyData = JSON.parse(JSON.stringify(this.$refs.studyCourses.dataList))
          let obj = {
            disciplineTypeId:basicData.disciplineTypeId,
            shelfStatus:basicData.shelfStatus,
            shareStatus:basicData.shareStatus,
            courseName:basicData.courseName,
            coverAttachmentPath:basicData.coverAttachmentPath,
            popupVerifyEnabled:basicData.popupVerifyEnabled,
            popupIntervalMinutes:basicData.popupVerifyEnabled == 1?basicData.popupIntervalMinutes:0,
            assessmentStatus:0,
            assessmentTotalScore:0,
            assessmentPassScore:0,
            assessmentAnswerDuration:0,
            coursewareIds:[],
            assessmentConfigs:[],
          }
          for(let i=0;i<studyData.length;i++){
            for(let o=0;o<studyData[i].minList.length;o++){
              obj.coursewareIds.push(studyData[i].minList[o].id)
            }
          }
          if(self.newData.id){
            self.examElCourseEdit(obj);
          }else{
            self.examElCourseAdd(obj);
          }
        }).catch(() => {});
      },
      examElCourseAdd(obj){
        examElCourseAdd(obj).then(response => {
          this.msgSuccess(response.message);
          this.$parent.tableButton(6);
        });
      },
      examElCourseEdit(obj){
        obj.id = this.newData.id
        examElCourseEdit(obj).then(response => {
          this.msgSuccess(response.message);
          this.$parent.tableButton(6);
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .courseManagement-addPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .bottom-max-big-box{
      flex:1;
      display: flex;
      overflow: hidden;
      .left-max-big-box{
        padding:20px 0;
        width:240px;
        overflow: hidden;
        border-radius:10px;
        margin:5px 10px 20px 10px;
        box-shadow: 0 0 8px 1px rgba(0, 0, 0, 0.1);
        .left-for-p{
          padding:0 30px;
          line-height:40px;
          cursor: pointer;
          margin-bottom:5px;
        }
        .left-for-p:hover{
          color:#fff;
          background-color: #0183fa;
        }
        .check-button-p{
          color:#0183FA;
        }
      }
      .right-max-big-box{
        flex:1;
        overflow: hidden;
        padding:0;
        border-radius:10px;
        margin:5px 20px 20px 10px;
        box-shadow: 0 0 8px 1px rgba(0, 0, 0, 0.1);
      }
    }
  }
</style>
