import {Component, OnInit} 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';

@Component({
  selector: 'app-std-interview-modify',
  templateUrl: './std-interview-modify.component.html',
  styleUrls: ['./std-interview-modify.component.scss']
})
export class StdInterviewModifyComponent extends FrameDetailComponent implements OnInit {

  dateArray;
  initMap;
  roomArray;
  appointmentArray;

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

  ngOnInit() {
  }

  initDefautEntity() {
    AjaxService.requestMap('std/layout/group/appointment.htm', this.currentEntity, (map) => {
      const dArray = [];
      for (const d of map.DateArray) {
        dArray.push({value: FuncService.dateToShortString(d.ly_exam_date)});
      }
      this.dateArray = dArray;
      this.initMap = map;
    });
  }

  changeExamDate() {
    if (this.currentEntity.new_ly_exam_date !== undefined) {
      const array = [];
      for (const s of this.initMap['AppArray_' + this.currentEntity.new_ly_exam_date]) {
        let appointment_value = s.group_time.time_name;
        if (s.country_total > 0) {
          appointment_value = appointment_value + ' (' + s.arrange_country_total + ' / ' + s.country_total + ')';
        }
        if (s.province_total > 0) {
          appointment_value = appointment_value + ' (省内：' + s.arrange_province_total + ' / ' + s.province_total + ')';
        }
        if (s.outside_total > 0) {
          appointment_value = appointment_value + ' (省外：' + s.arrange_outside_total + ' / ' + s.outside_total + ')';
        }
        array.push({
          appointment_id: s.appointment_id,
          appointment_value: appointment_value
        });
      }
      this.appointmentArray = array;
      this.roomArray = this.initMap['RoomArray_' + this.currentEntity.new_ly_exam_date];
    }
  }

  modifyGroup() {
    if (this.currentEntity.new_ly_exam_date === undefined) {
      MsgService.errorToastr('必须指定考试日期！');
      return;
    }
    if (this.currentEntity.new_appointment_id === undefined) {
      MsgService.errorToastr('必须指定考试时段！');
      return;
    }

    if (this.currentEntity.ly_room_id !== undefined) {
      if (this.currentEntity.new_ly_room_id === undefined) {
        MsgService.errorToastr('必须指定考场信息！');
        return;
      }
    }
    MsgService.confirmAlert('确定要更改该项目的考试安排吗？', () => {
        AjaxService.request('std/layout/group/change.htm',this.currentEntity, () => {
          MsgService.successToastr('修改成功！');
          this.closeWindow();
        })
    });
  }

}
