import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDeptDetailComponent } from 'src/app/routes/frame/dept/frame-dept-detail/frame-dept-detail.component';
import { AjaxService } from '../../../../../core/service/ajax.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';


@Component({
  selector: 'app-ump-group-interview-detail',
  templateUrl: './ump-group-interview-detail.component.html',
  styleUrls: ['./ump-group-interview-detail.component.scss']
})
export class UmpGroupInterviewDetailComponent extends FrameDetailComponent  implements OnInit {
 
  deptArray = [];
 
  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);

    this.detailForm = fb.group({
      'group_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'ly_order': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(999)])],
      'appointment_type': [null],
      'exam_seconds': [null, Validators.compose([Validators.required, Validators.min(60), Validators.max(999)])],
      'std_sign_minutes': [null, Validators.compose([Validators.required, Validators.min(0), Validators.max(180)])],
      'province_flag': [null],
      'once_flag': [null],
      'pagerless_flag': [null],
      'pack_flag': [null],
      'room_extract_time': [null],
      'focus_flag': [null],
      'focus_addr': [null],
      'room_judge_num': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(50)])],
      'pack_std_num': [null, Validators.compose([Validators.min(1), Validators.max(99)])],
      'pack_std_max': [null, Validators.compose([Validators.min(1), Validators.max(99)])],
      'video_flag': [null],
      'optr_dept': [null]

    });
  }

  initDefautEntity() {
    if (this.currentEntity.ly_group_id === undefined) {
      this.currentEntity = LayoutPlanInterviewDetailComponent.makeGroup();
    }
    this.currentEntity.group_type = 'Interview';
    for (const sb of this.ResponseArray) {
      if (sb.score_status === 'Active') {
        sb.checked = true;
      } else {
        sb.checked = false;
      }
    }
  }

  ngOnInit() {
    //所有的学院，考场管控-控制权限使用
    AjaxService.requestArray('frame/dept/all.htm', {}, (ResponseArray) => {
      this.deptArray = FrameDeptDetailComponent.makeOptrDeptArray(ResponseArray);
    });
  }

  saveGroup() {
    if (!this.validForm()) {
      return;
    }

    for (const sb of this.ResponseArray) {
      if (sb.group_order === undefined || sb.group_order < 0) {
        sb.group_order = 0;
      }
      if (sb.checked === true) {
        sb.score_status = 'Active';
      } else {
        sb.score_status = 'InActive';
      }
    }
    // 分组考试
    if (this.currentEntity.pack_flag === 'Active') {
      if (this.currentEntity.pack_std_num < 2) {
        MsgService.errorToastr('分组面试模式下，单次考生数量必须大于1！');
        return;
      }
      if (this.currentEntity.pack_std_max < this.currentEntity.pack_std_num) {
        MsgService.errorToastr('分组面试模式下，单次考生数量最大考生数不能小于标准考生数！');
        return;
      }
      if (this.currentEntity.room_wait_num < this.currentEntity.pack_std_max) {
        MsgService.errorToastr('分组面试模式下，场外候考人数不能小于分组最大人数！');
        return;
      }
    } else {
      this.currentEntity.pack_std_num = 1;
      this.currentEntity.pack_std_max = 1;
    }
    // 判断无纸化平评参数
    if (this.currentEntity.pagerless_flag === 'Active') {
      if (this.currentEntity.room_judge_num < 1) {
        MsgService.errorToastr('评委数量不能小于1，请检查！');
        return;
      }
      if (this.currentEntity.judge_cache_num < 1) {
        MsgService.errorToastr('评委评分缓存数量不能小于1，请检查！');
        return;
      }


      if (this.currentEntity.room_print_num < 0) {
        this.currentEntity.room_print_num = 0;
      }

    }

    if (this.currentEntity.focus_flag === 'Active') {
      if (this.currentEntity.focus_addr === undefined || this.currentEntity.focus_addr.length === 0) {
        MsgService.errorToastr('请输入集中候考的地址！');
        return;
      }
    }

    this.currentEntity.subject_json = JSON.stringify(this.ResponseArray);
    this.currentEntity.group_start_date = undefined;
    this.currentEntity.group_end_date = undefined;
    this.currentEntity.gropup_std_total = undefined;
    MsgService.confirmAlert('确定要将这些科目进行分组合并吗？', () => {
      AjaxService.request('ump/group/union.htm', this.currentEntity, () => {
        MsgService.successAlert('面试科目分组合并操作成功！',  () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }

}
