import {Component, OnInit} from '@angular/core';
import {FrameEditComponent} from '../../../../frame/core/FrameEditComponent';
import {BsModalRef} from 'ngx-bootstrap';
import {FormBuilder, Validators} from '@angular/forms';
import {FrameService} from '../../../../../core/frame/frame.service';
import {AjaxService} from '../../../../../core/ajax/ajax.service';

@Component({
  selector: 'app-std-interview-modify',
  templateUrl: './std-interview-modify.component.html',
  styleUrls: ['./std-interview-modify.component.scss']
})
export class StdInterviewModifyComponent extends FrameEditComponent implements OnInit {
  groupArray: any;
  dateArray: any;
  timeArray: any;
  currentGroup:any;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.valForm = fb.group({
      'ly_group_id': [null, Validators.compose([Validators.required])],
      'business_note': [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
  }

  initDefautEntity() {
    const array = [];
    for (const s of this.ResponseArray) {
      if (s.real_room_id === undefined) {
        // 没有考试，可以更换考场
        let flag = true;
        for (const v of array) {
          if (v.ly_group_id === s.ly_group_id) {
            flag = false;
            break;
          }
        }
        if (flag) {
          s.view_text = s.group_name + ' [' + FrameService.shortDateString(s.ly_exam_date) + '] [' + (FrameService.shortMinuteString(s.exam_start_time) + ' - ' + FrameService.shortMinuteString(s.exam_end_time)) + ']';
          array.push(s);
        }
      }
    }
    this.groupArray = array;
  }

  changeGroup() {
    let flag = true;
    for (const s of this.groupArray) {
      if (s.ly_group_id === this.currentEntity.ly_group_id && s.ly_room_addr !== undefined) {
        this.currentEntity.ly_room_id = s.ly_group_id;
        this.currentEntity.ly_room_addr = s.ly_room_addr;
        flag = false;
      }
    }
    if (flag) {
      this.currentEntity.ly_room_id = undefined;
      this.currentEntity.ly_room_addr = undefined;
    }
    this.currentGroup = undefined;
    AjaxService.requestEntity('layout/group/find.htm', {ly_group_id: this.currentEntity.ly_group_id}, (entity) => {
      this.currentGroup = entity;
      AjaxService.requestArray('enrol/std/appointment/date.htm', {ly_group_id: this.currentEntity.ly_group_id}, (array) => {
        for(let d of array) {
          d.date_text = FrameService.shortDateString(d.ly_exam_date);
          if(this.currentGroup.province_flag === 'Active') {
            // 分省内外

          } else {
            // 全国
            d.date_text = d.date_text + `[${d.arrange_country_total} / ${d.country_total}]`;
          }

        }

        this.dateArray = array;
      });
    })


  }
}
