<template>
  <a-modal title="安排试听课" ok-text="确认" cancel-text="取消" :visible="auditionClassShow" :confirm-loading="dialogloading"
    :width="600" @ok="auditionOk" @cancel="auditionCancel">
    <a-row >
      <a-col :md="24" :xs="24">
        <span style="color:#000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span style="color:tomato;">*</span>校区:</span>
        <a-select style="width: 176px;margin-left: 8px;" v-model="addPara.schoolDistrictId" @change="handlerChange">
          <a-select-option v-for="(item, key)  in schoolOptions" :key="key" :value="item.value">
            {{item.text}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span style="color:tomato;">*</span>课程:</span>
        <a-select style="width: 176px;margin-left: 8px;" v-model="addPara.courseId" >
          <a-select-option v-for="(item, key)  in courseOptions" :key="key" :value="item.value">
            {{item.text}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;"><span style="color:tomato;">*</span>上课日期:</span>
        <j-date  date-format="YYYY-MM-DD" v-model="addPara.curriculaDate" style="margin-left: 10px;" >
        </j-date>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col>
        <span style="color:#000;"><span style="color:tomato;">*</span>上课时间:</span>
        <a-select style="width: 60px;margin-left: 10px;" v-model="time1" @change="timeChange">
          <a-select-option v-for="(val,index) in 23" :key="index" :value="index">{{ convertToBinary(index) }}
          </a-select-option>
        </a-select>
        &nbsp;:&nbsp;
        <a-select style="width: 60px" v-model="time2" @change="timeChange">
          <template v-for="(val,index) in 60">
            <a-select-option v-if="Number(index)%5===0" :value="index">{{ convertToBinary(index)}}</a-select-option>
          </template>
        </a-select>
        &nbsp;&nbsp;~&nbsp;&nbsp;
        <a-select style="width: 60px" v-model="time3" @change="timeChange">
          <a-select-option v-for="(val,index) in 23" :key="index" :value="index">{{ convertToBinary(index) }}
          </a-select-option>
        </a-select>
        &nbsp;:&nbsp;
        <a-select style="width: 60px" v-model="time4" @change="timeChange">
          <template v-for="(val,index) in 60">
            <a-select-option v-if="Number(index)%5===0" :value="index">{{ convertToBinary(index)}}</a-select-option>
          </template>
        </a-select>
        (上课时长:{{duration}}分钟)
      </a-col>
    </a-row>

    <a-row >
      <a-col>
        <a-button style="margin-left: 54px;"  @click="handlerTimeShow" type="link">使用预设时间</a-button>
      </a-col>
    </a-row>

    <a-row style="margin-top: 10px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;">上课教室:</span>
        <a-select style="width: 176px;margin-left: 8px;" placeholder="请选择教室"  v-model="classRoomId" >
          <a-select-option v-for="(item, key)  in classRoomOptions" :key="key" :value="item.id">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;">主教老师:</span>
        <a-select style="width: 176px;margin-left: 8px;" placeholder="请选择老师" v-model="addPara.lecturerId" >
          <a-select-option v-for="(item, key)  in lecturerOptions" :key="key" :value="item.value">
            {{item.text}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;">助教老师:</span>
        <a-select style="width: 176px;margin-left: 8px;" placeholder="请选择助教老师" v-model="addPara.assistantId" >
          <a-select-option v-for="(item, key)  in lecturerOptions" :key="key" :value="item.value">
            {{item.text}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24">
        <span style="color:#000;">上课主题:</span>
        <a-select style="width: 176px;margin-left: 8px;" placeholder="请选择上课主题" v-model="addPara.themeId" >
          <a-select-option v-for="(item, key)  in themeOptions" :key="key" :value="item.value">
            {{item.text}}
          </a-select-option>
        </a-select>
      </a-col>
    </a-row>
    <a-row style="margin-top: 15px;">
      <a-col :md="24" :xs="24" style="display: flex;">
        <div style="color:#000; flex:11;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;备注:</div>
        <div style="flex: 89;">
          <a-textarea v-model="addPara.remark" rows="4" placeholder="请输入备注" style="max-width: 86%; margin-left: 6px;" />
        </div>
      </a-col>
    </a-row>
    <PrefabricationTime @child-event='timeReturn' :timeShow="timeShow"></PrefabricationTime>
    <AuditionConfirm @confirm-event='confirmReturn' :confirmShow="confirmShow" 
    :addPara="addPara" :addStatus="'addNew'" ></AuditionConfirm>
  </a-modal>
</template>
<script>
import { getAction,postAction } from '@/api/manage'
import PrefabricationTime from '../../modules/PrefabricationTime.vue'
import AuditionConfirm from '../../modules/AuditionConfirm.vue'
import store from '@/store'
import moment from 'moment'
import { message } from 'ant-design-vue';
import {ajaxGetDictItems} from '@/api/api'
export default {
  name: 'AuditionClassAdd',
  components: {
    PrefabricationTime,AuditionConfirm
  },
  data() {
    return {
      dialogloading: false,
      addPara: {},
      courseList: undefined,
      classRoomList: undefined,
      lectureList: undefined,
      assistantList: undefined,
      themeList: undefined,
      time1: undefined,
      time2: undefined,
      time3: undefined,
      time4: undefined,
      classRoomId: undefined,
      timeShow: false,
      duration: 0,
      confirmShow: false,
      followTypeList: [],
      courseOptions: undefined,
      classRoomOptions: undefined,
      lecturerOptions: undefined,
      schoolOptions: undefined,
      themeOptions: undefined,
    }
  },
  props: ['auditionClassShow','cpId','cpPhone','classType'],
  watch: {
    auditionClassShow: {
      handler() {
        this.addPara = {
          curriculaDate: moment(new Date()).format("YYYY-MM-DD"),
          cpId: this.cpId,
          cpPhone: this.cpPhone,
        };
        var date = new Date();
        var hour = date.getHours();
        if(hour === Number(0)) {
          this.time1 = Number(hour);
          this.time2 = Number(0);
          this.time3 = Number(hour)+1;
          this.time4 = Number(0);
          this.timeChange();
        } else {
          this.time1 = Number(hour)-1;
          this.time2 = Number(0);
          this.time3 = Number(hour);
          this.time4 = Number(0);
          this.timeChange();
        }
        
        ajaxGetDictItems('education_course,name,id', null).then((res) => {
          if (res.success) {
            this.courseOptions = res.result;
          }
        });
        getAction("/education/educationClassroom/getAll").then((res) => {
          this.classRoomOptions = res;
          this.classRoomOptionsBack = res;
        });

        ajaxGetDictItems('sys_user,realname,id', null).then((res) => {
          if (res.success) {
            this.lecturerOptions = res.result;
          }
        });
        ajaxGetDictItems('education_theme,name,id', null).then((res) => {
          if (res.success) {
            this.themeOptions = res.result;
          }
        });
        ajaxGetDictItems('school_organization,name,id', null).then((res) => {
          if (res.success) {
            this.schoolOptions = res.result;
          }
        });
      }
    }
  },
  methods: {
    auditionOk() {
      if(this.addPara.schoolDistrictId === null || this.addPara.schoolDistrictId === undefined || this.addPara.schoolDistrictId==="") {
        message.warning('请选择校区');
        return;
      }
      if(this.addPara.courseId === null || this.addPara.courseId === undefined || this.addPara.courseId==="") {
        message.warning('请选择课程');
        return;
      }
      if(this.addPara.curriculaDate === null || this.addPara.curriculaDate === undefined || this.addPara.curriculaDate==="") {
        message.warning('请选择上课日期');
        return; 
      }
      
      if(this.time1 === null || this.time1 === undefined || this.time1===""
      ||this.time2 === null || this.time2 === undefined || this.time2===""
      ||this.time3 === null || this.time3 === undefined || this.time3===""
      ||this.time4 === null || this.time4 === undefined || this.time4==="") {
        message.warning('请选择上课时间');
        return; 
      }
      if(this.duration === 0 ) {
        message.warning('时间选择错误!');
        return; 
      }
      this.addPara.type = 2;
      this.addPara.createBy = store.getters.userInfo.id;
      this.addPara.createTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
      this.addPara.status = 2;
      this.addPara.classRoomId = this.classRoomId;
      this.addPara.period = this.duration;
      this.addPara.courseText = this.courseOptions.find(item => {return item.value == this.addPara.courseId }).text;
      if (this.addPara.classRoomId !== undefined) {
        this.addPara.classRoomText = this.classRoomOptionsBack.find(item => {return item.id == this.addPara.classRoomId }).name;
      }
      if (this.addPara.lecturerId !== undefined) {
        this.addPara.lecturerText = this.lecturerOptions.find(item => {return item.value == this.addPara.lecturerId }).text;
      }
      if (this.addPara.assistantId !== undefined) {
        this.addPara.assistantText = this.lecturerOptions.find(item => {return item.value == this.addPara.assistantId }).text;
      }
      if (this.addPara.themeId !== undefined) {
        this.addPara.themeText = this.themeOptions.find(item => {return item.value == this.addPara.themeId }).text;
      }
      let checkPara = {
        class_room_id: this.addPara.classRoomId,
        lecturer_id: this.addPara.lecturerId,
        assistant_id:this.assistantId,
        class_id:this.addPara.class_id,
        curricula_date: this.addPara.curriculaDate,
        curricula_time: this.addPara.curriculaTime
      }
      // 判断老师档期、教室档期 、助教档期
      postAction("/education/educationSchedule/timeCheck", checkPara).then((res) => {
        if (res.success) {
          if(this.classType ==='classAdd') {
            postAction("/education/educationSchedule/add", this.addPara).then((res) => {
              message.success('添加成功！');
              this.$emit('child-event', false);
            });
          } else {
            //居然又弹窗  服了~~~！！！
            this.confirmShow = true;
          }
        } else {
          message.warning(res.message)
        }        
      });
      
    },
    auditionCancel() {
      this.$emit('child-event', false);
    },
    convertToBinary(num) {
      var str = num.toString();
      while (str.length < 2) {
        str = "0" + str;
      }
      return str;
    },
    timeChange(){
      var time2temp = Number(this.time3)*60 + Number(this.time4);
      var time1temp = Number(this.time1)*60 + Number(this.time2);
      if (time2temp - time1temp > 0) {
        this.duration = time2temp - time1temp ;
      } else {
        this.duration = 0 ;
      }
      this.addPara.curriculaTime = this.convertToBinary(this.time1)+":"+this.convertToBinary(this.time2)
      +"-"+this.convertToBinary(this.time3)+":"+this.convertToBinary(this.time4);
    },
    handlerTimeShow() {
      this.timeShow = true;
    },
    timeReturn(data) {
      this.timeShow = false;
      if(data !== null) {
        let timeTemp = data[0].value.split('-');
        let timeTemp1 = timeTemp[0].split(':');
        let timeTemp2 = timeTemp[1].split(':');
        this.time1 = this.convertToBinary(timeTemp1[0]);
        this.time2 = timeTemp1[1];
        this.time3 = this.convertToBinary(timeTemp2[0]);
        this.time4 = timeTemp2[1];
        this.duration = data[0].text;
        this.addPara.curriculaTime = this.convertToBinary(this.time1)+":"+this.convertToBinary(this.time2)
      +"-"+this.convertToBinary(this.time3)+":"+this.convertToBinary(this.time4);
      }
    },
    confirmReturn(data) {
      this.confirmShow = false;
      if(!data) {
        this.$emit('child-event', false);
      }
    },
    handlerChange(data) {
      this.classRoomOptions = this.classRoomOptionsBack.filter(item => item.schoolDistrictId === Number(data));
      this.classRoomId = undefined;
    }
  }
}
</script>
