import {Component, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
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';

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

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);

  }

  ngOnInit() {

  }

  deleteAppointment(v) {
    AjaxService.requestEntity('layout/group/appointment/delete.htm', {appointment_id: v.appointment_id}, (flag) => {
      if (flag) {
        MsgService.successToastr(`时段已删除`);
        // 还有时段
        AjaxService.requestArray('layout/group/interview/date/view.htm', {ly_group_id: this.currentEntity.ly_group_id, exam_date: v.ly_exam_date}, (array) => {
          this.ResponseMap['TimeMap'][ v.ly_exam_date] = array;
        }, {RequestWithLoading: true});
      } else {
        MsgService.successToastr(`时段已删除，本日无可用时段，自动删除当前日期`);
        this.refresh();
      }
    });
  }

  addDate() {
    AjaxService.requestEntity('layout/group/appointment/date/add.htm', {ly_group_id: this.currentEntity.ly_group_id}, (entity) => {
      MsgService.successToastr(`已经添加了考试日期【${entity}】，并自动为该日期增加了所有时段！`);
      this.refresh();
    }, {RequestWithLoading: true});
  }

  fullTime(ly_group_id, date) {
    AjaxService.requestEntity('layout/group/appointment/time/full.htm', {ly_group_id: ly_group_id, exam_date: date}, () => {
      MsgService.successToastr(`已经将考试日期【${date}】的所有时段填充完毕！`);
      AjaxService.requestArray('layout/group/interview/date/view.htm', {ly_group_id: this.currentEntity.ly_group_id, exam_date: date}, (array) => {
        this.ResponseMap['TimeMap'][ date] = array;
      }, {RequestWithLoading: true});
    }, {RequestWithLoading: true});
  }


  refresh() {
    AjaxService.requestMap('layout/group/interview/view.htm', {ly_group_id:  this.currentEntity.ly_group_id}, (map) => {
     this.ResponseMap = map;
    }, {RequestWithLoading: true});
  }


  /*******************
   * 计算已编排的数量
   */
  calArrangeTotal() {
    let country_total = 0, province_total = 0, outside_total = 0;
    for (const date of this.ResponseMap['DateArray']) {
      for (const time of this.ResponseMap['TimeMap'][date.ly_exam_date]) {
        if (time.country_total !== undefined) {
          country_total += time.country_total;
        }
        if (time.province_total !== undefined) {
          province_total += time.province_total;
        }
        if (time.outside_total !== undefined) {
          outside_total += time.outside_total;
        }
      }
    }
    this.ResponseMap['GroupTotal'].country_total = country_total;
    this.ResponseMap['GroupTotal'].province_total = province_total;
    this.ResponseMap['GroupTotal'].outside_total = outside_total;
  }
  // 编排
  saveGroup() {
    // 判断
    if (this.currentEntity.province_flag === 'Active') {
      if (this.currentEntity.province_std_total > 0 && (this.ResponseMap['GroupTotal'].province_total === undefined || this.ResponseMap['GroupTotal'].province_total < this.currentEntity.province_std_total)) {
        MsgService.errorToastr('省内编排容量小于参加考试人数，请检查！');
        return;
      }
      if (this.currentEntity.outside_std_total > 0 && (this.ResponseMap['GroupTotal'].outside_total === undefined || this.ResponseMap['GroupTotal'].outside_total < this.currentEntity.outside_std_total)) {
        MsgService.errorToastr('外省编排容量小于参加考试人数，请检查！');
        return;
      }
    } else {
      if (this.ResponseMap['GroupTotal'].country_total === undefined || this.ResponseMap['GroupTotal'].country_total < this.currentEntity.group_std_total) {
        MsgService.errorToastr('编排容量小于参加考试人数，请检查！');
        return;
      }
    }

    // 设置属性,
    const tArray = [];
    for (const date  of this.ResponseMap['DateArray']) {
      for (const t of this.ResponseMap['TimeMap'][date.ly_exam_date]) {
        tArray.push(t);
      }
    }
    this.currentEntity.app_json = JSON.stringify(tArray);

    MsgService.confirmAlert('确定要保存编排信息吗？', () => {
      AjaxService.request('layout/group/interview/modify.htm', {
        ly_group_id: this.currentEntity.ly_group_id,
        app_json: JSON.stringify(tArray)
      }, () => {
        MsgService.successAlert('编排信息已保存！', () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }

}
