import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FuncService } from '../../../../../core/service/func.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
import {LayoutPlanInterviewDetailComponent} from '../../plan/layout-plan-interview-detail/layout-plan-interview-detail.component';

declare let laydate;

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

  // 分页组件
  // @ViewChild('uploader', {static: false}) uploader: UploaderComponent;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'group_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'appointment_type': [null],
      'once_flag': [null],
      'province_flag': [null],
      'aspect_num_flag': [null],
      'focus_flag': [null],
      'room_limit_flag': [null],
      'focus_addr': [null],
      'ly_order': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(999)])],
      'conflict_type': [null],
    });
  }

  initDefautEntity() {
    if (this.currentEntity.ly_group_id === undefined) {
      this.currentEntity = LayoutPlanInterviewDetailComponent.makeGroup();
    } else {
      this.AjaxService.requestMap('layout/group/subject/list.htm', {ly_group_id: this.currentEntity.ly_group_id}, (map) => {
        this.currentEntity = map['group'];
        this.ResponseArray = map['subjectList'];
      });
    }
    this.currentEntity.group_type = 'Written';
  }

  ngOnInit() {
    setTimeout(() => {
      this.setExamTime();
      this.getDate();
    }, 200);
  }

  changeFocusFlag() {
    if (this.currentEntity.focus_flag === 'InActive') {
      this.currentEntity.focus_addr = undefined;
    }
  }
  saveGroup() {
    if (!this.validForm()) {
      return false;
    }

    for (const sb of this.ResponseArray) {
      if (sb.exam_start_time === undefined){
        MsgService.errorToastr(`请输入 ${sb.subject_name} 的考试开始时间，格式为 yyyy-MM-dd HH:mm:ss`);
        return  false;
      }
      if (sb.exam_minutes === undefined){
        MsgService.errorToastr(`请输入${sb.subject_name}的考试时长，单位为分钟`);
        return;
      }
    }


    if (this.currentEntity.focus_flag === 'Active') {
      if (this.currentEntity.focus_addr === undefined || this.currentEntity.focus_addr.length === 0 ) {
        MsgService.errorToastr(`设置了统一候考，必须输入统一候考地址！`);
        return  false;
      }
    }
    FuncService.sortArray(this.ResponseArray, 'exam_start_time');
    this.currentEntity.subject_json = JSON.stringify(this.ResponseArray);
    if(!this.currentEntity.conflict_type) {
      this.currentEntity.conflict_type = 1;
    }
    MsgService.confirmAlert('确定要将这些科目进行分组吗？', () => {
      AjaxService.request('layout/group/union.htm', this.currentEntity, () => {
        MsgService.successAlert('笔试科目分组操作成功！', () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }


  setExamTime() {
    for(const response of this.ResponseArray) {
      var nowDate = new Date();
      var year = nowDate.getFullYear();
      var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1): nowDate.getMonth() + 1;
      var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
      if(!response.exam_start_time) {
        response.exam_start_time = year +"-" + month +"-" + day  +" 09:00:00";
      }
    }
  }


  //初始化时间控件
  getDate() {
    let _this = this;
    setTimeout(() => {
      laydate.render({
        elem:'#exam_start_time1',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          debugger
          this.ResponseArray[0].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time2',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[1].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time3',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[2].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time4',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[3].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time5',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[4].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time6',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[5].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time7',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[6].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time8',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[7].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time9',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[8].exam_start_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_time10',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.ResponseArray[9].exam_start_time = value;
        }
      });
    }, 500);
  }

}
