import {Component, OnInit} from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

declare let laydate;

@Component({
  selector: 'app-layout-plan-time-detail',
  templateUrl: './layout-plan-time-detail.component.html',
  styleUrls: ['./layout-plan-time-detail.component.scss']
})
export class LayoutPlanTimeDetailComponent extends FrameDetailComponent implements OnInit {

  constructor(bsModalRef: BsModalRef) {
    super(bsModalRef);

  }

  ngOnInit() {
    this.getDate();
  }

  addTime() {
    this.ResponseArray.push({time_order: this.ResponseArray.length + 1,exam_start_time: '09:00', exam_end_time: '12:00'});
    this.getDate();
  }

  initDefautEntity() {
    if (this.ResponseArray === undefined) {
      this.ResponseArray = [];
    }
    for (let i = 0; i < this.ResponseArray.length; i++) {
      this.ResponseArray[i].time_order = i + 1;
    }
  }

  deleteTime(time) {
    const array = [];
    for (const t of this.ResponseArray) {
      if (t.time_order !== time.time_order) {
        array.push(t);
      }
      this.ResponseArray = array;
    }
  }

  saveTime() {
    if (this.ResponseArray.length === 0) {
      MsgService.errorToastr(`没有定义有效的考试时段！`);
      return;
    }
    for (let i = 0; i < this.ResponseArray.length; i++) {
      const time = this.ResponseArray[i];
      if (time.time_name) {
        time.time_name = time.time_name.trim();
      }
      if (time.exam_start_time) {
        time.exam_start_time = time.exam_start_time.trim();
      }
      if (time.exam_end_time) {
        time.exam_end_time = time.exam_end_time.trim();
      }

      if (!time.time_name || time.time_name.length === 0) {
        MsgService.errorToastr(`请填写第${i + 1}行时段名称！`);
        return;
      }
      if (!time.exam_start_time || time.exam_start_time.length === 0) {
        MsgService.errorToastr(`请填写第${i + 1}行考试开始时间！`);
        return;
      }
      if (!time.exam_end_time || time.exam_end_time.length === 0) {
        MsgService.errorToastr(`请填写第${i + 1}行考试结束时间！`);
        return;
      }
      if (!time.time_type) {
        MsgService.errorToastr(`请选择第${i + 1}行考试时间类型！`);
        return;
      }
    }

    // 保存
    AjaxService.requestArray('layout/plan/time/save.htm', {
      time_group: this.currentEntity.time_group,
      time_json: JSON.stringify(this.ResponseArray)
    }, () => {
      MsgService.successToastr('预编排时段定义已保存');
      this.closeWindow();
    });
  }

  //初始化时间控件
  getDate() {
    setTimeout(() => {
       laydate.render({
        elem:'#exam_start_time1',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[0].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time2',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[1].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time3',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[2].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time4',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[3].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time5',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[4].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_end_time1',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[0].exam_end_time = value;
        }
      });
      laydate.render({
        elem:'#exam_end_time2',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[1].exam_end_time = value;
        }
      });
      laydate.render({
        elem:'#exam_end_time3',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[2].exam_end_time = value;
        }
      });
      laydate.render({
        elem:'#exam_end_time4',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[3].exam_end_time = value;
        }
      });
      laydate.render({
        elem:'#exam_end_time5',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          this.ResponseArray[4].exam_end_time = value;
        }
      });

    }, 500);
    
  }

}
