import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import {FrameDetailComponent} from '../../../frame/core/detail/frame.detail';
import { MsgService } from 'src/app/core/service/msg.service';
import {AjaxService} from '../../../../core/service/ajax.service';
declare let laydate;
@Component({
  selector: 'app-ly-group-page-detail',
  templateUrl: './ly-group-page-detail.component.html',
  styleUrls: ['./ly-group-page-detail.component.scss']
})
export class LyGroupPageDetailComponent extends FrameDetailComponent implements OnInit {

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      group_name: [null, Validators.compose([Validators.required])],
      provence_flag: ['InActive', Validators.compose([Validators.required])],
      aspect_order: ['InActive', Validators.compose([Validators.required])],
      ly_order: [null, Validators.compose([Validators.required])],
      cfSubjectList: [null],
    });
  }

  ngOnInit(): void {
    setTimeout(() => {
      this.setExamTime();
      this.getDate();
    }, 500);
  }
  initDefautEntity() {
    super.initDefautEntity();
    if(!this.currentEntity.provence_flag){
      this.currentEntity.provence_flag='InActive'
      this.currentEntity.aspect_order='InActive'
      this.detailMode = 'add';
    }

  }

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


  // 初始化时间控件
  getDate() {
    const _this = this;
    setTimeout(() => {
      for (let i = 0; i < this.ResponseArray.length; i++) {
        laydate.render({
          elem: '#exam_start_time'+(i+1),
          type: 'datetime',
          theme: '#0c6acf',
          trigger: 'click',
          format: 'yyyy-MM-dd HH:mm',
          done: (value, date, endDate) => {
            _this.ResponseArray[i].exam_start_time = value;
          }
        });
        laydate.render({
          elem: '#exam_end_time'+(i+1),
          type: 'datetime',
          theme: '#0c6acf',
          trigger: 'click',
          format: 'yyyy-MM-dd HH:mm',
          done: (value, date, endDate) => {
            _this.ResponseArray[i].exam_start_time = value;
          }
        });
      }
    }, 500);
  }

  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`);
        return  false;
      }
      if (sb.exam_end_time === undefined){
        MsgService.errorToastr(`请输入 ${sb.subject_name} 的考试结束时间，格式为 yyyy-MM-dd HH:mm`);
        return  false;
      }
    }
    this.currentEntity.subject_json = JSON.stringify(this.ResponseArray);
    MsgService.confirmAlert('确定要将这些科目进行分组吗？', () => {
      AjaxService.request('ly/lyGroup/add.htm', this.currentEntity, () => {
        MsgService.successAlert('编排分组设置添加成功！', () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }
}
