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 { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

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

  deptArray = [];
  groupArr = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'group_name': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(128)])],
      'room_judge_num': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(50)])],
      'dept_id': [null, null],
      'room_extract_time': [null, Validators.compose([Validators.required])],
      'res_play_mode': [null, Validators.compose([Validators.required])],
      'pack_flag': [null, null],
      'pack_std_num': [null, null],
      'pack_std_max': [null, null],
      'judge_cache_num': [null, Validators.compose([Validators.required])],
	    'room_wait_num': [null, Validators.compose([Validators.required])],
	    'room_extract_type': [null, Validators.compose([Validators.required])],
	    'checkin_flag': [null, Validators.compose([Validators.required])],
	    'time_limit_type': [null, Validators.compose([Validators.required])],
	    'room_limit_flag': [null, Validators.compose([Validators.required])],
      'sync_group_ids': [null, null]
    });
   }

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

  initDefautEntity() {
    if (this.currentEntity.ly_group_id === undefined) {
      this.currentEntity = this.makeGroup();
    }
    for (const sb of this.ResponseArray) {
      if (sb.score_status === 'Active') {
        sb.checked = true;
      } else {
        sb.checked = false;
      }
    }
    //设置同序号考场分组
    if(this.currentEntity.sync_group_ids) {
      const ids = [];
      const group_ids =  this.currentEntity.sync_group_ids.split(',');
      for(const id of group_ids) {
        ids.push(parseInt(id));
      }
      this.currentEntity.sync_group_ids = ids;
    }
  }

  saveGroup() {
    //评分科目设置
    for(const sb of this.ResponseArray) {
      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) {
        this.MsgService.errorToastr('分组面试模式下，单次考生数量必须大于1！');
        return;
      }
      if (this.currentEntity.pack_std_max < this.currentEntity.pack_std_num) {
        this.MsgService.errorToastr('分组面试模式下，单次考生数量最大考生数不能小于标准考生数！');
        return;
      }
      if (this.currentEntity.room_wait_num < this.currentEntity.pack_std_max) {
        this.MsgService.errorToastr('分组面试模式下，场外候考人数不能小于分组最大人数！');
        return;
      }
    } else {
      this.currentEntity.pack_std_num = 1;
      this.currentEntity.pack_std_max = 1;
    }
    //同序号考场分组判断
    if(this.currentEntity.sync_group_ids) {
      const ids = this.currentEntity.sync_group_ids;
      for(const id of ids) {
        if(id === this.currentEntity.ly_group_id) {
          this.MsgService.errorToastr('同序号考场分组不能选择当前分组！');
          return;
        }
      }
    }
    var msg = '确定要保存面试分组吗？';
    if(this.ResponseArray && this.ResponseArray.length > 1) {
      msg = '确定要将这些科目进行分组合并吗？';
    }
    this.currentEntity.subject_json = JSON.stringify(this.ResponseArray);
    this.currentEntity.group_start_date = undefined;
    this.currentEntity.group_end_date = undefined;
    this.MsgService.confirmAlert(msg, () => {
      this.AjaxService.request('ly/group/union.htm', this.currentEntity, () => {
        this.MsgService.successAlert('面试分组操作成功！', () => {
          this.closeWindow();
        });
      });
    });

  }

  //分组
  requestGroup() {
    this.AjaxService.requestArray('ly/group/list/by/group.htm', {ly_group_id: this.ResponseEntity.ly_group_id}, (array) => {
      this.groupArr = array;
    });
  }

  makeGroup() {
    return {
      // 默认不需要检录
      checkin_flag: 'InActive',
      // 默认评委待提交评分考生允许20个考生
      judge_cache_num: 20,
      // 编排状态，都是未编排状态
      ly_status: 'Init',
      // 是否按组考试，默认不是
      pack_flag: 'InActive',
      // 默认分组最大人数 15人
      pack_std_max: 15,
      // 默认分组标准人数10人
      pack_std_num: 10,
      // 是否需要将省内外考生分开编排
      province_flag: 'InActive',
      // 抽取考场的方式:默认平均，随机
      room_extract_type: 'AverageRandom',
      // 抽取考场的时间点
      room_extract_time: 'Checkin',
      // 默认评委为5，需要每个确认
      room_judge_num: 5,
      // 默认严格限制考生考场
      room_limit_flag: 'Active',
      // 考场时间限制，默认为天
      time_limit_type: 'Date',
      // 允许门外候考的人数，默认1
      room_wait_num: 20,
      res_play_mode: 'No'
    };
  }

}
