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-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(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],
      'group_ids': [null],
      'appointment_order_flag': [null],
      'group_worker_explain': [null],
      'conflict_type': [null,Validators.required]

    });
  }

  initDefautEntity() {
    if (this.currentEntity.ly_group_id === undefined) {
      this.currentEntity = LayoutPlanInterviewDetailComponent.makeGroup();
    } else {
      //重新从数据库中读取
      this.AjaxService.requestEntity('layout/group/find.htm', {ly_group_id: this.currentEntity.ly_group_id}, (entity) => {
        this.currentEntity = entity;
      });
    }
    this.currentEntity.group_type = 'Interview';
    for (const sb of this.ResponseArray) {
      if (sb.score_status === 'Active') {
        sb.checked = true;
      } else {
        sb.checked = false;
      }
    }
    if(!this.currentEntity.room_judge_num) {
      this.currentEntity.room_judge_num = 5; //默认5个评委
    }
    if(!this.currentEntity.room_wait_num || this.currentEntity.room_wait_num == 0) {
      this.currentEntity.room_wait_num = 20; //场外候考人数，默认20
    }
    if(!this.currentEntity.appointment_order_flag) {
      this.currentEntity.appointment_order_flag = 'Active';
    }
    if(!this.currentEntity.interview_processor) {
      this.currentEntity.interview_processor = 'InActive';
    }
    if(!this.currentEntity.conflict_type) {
      this.currentEntity.conflict_type = 1;
    }
    //设置同序号考场分组
    if(this.currentEntity.group_worker_explain) {
      const ids = [];
      const group_ids =  this.currentEntity.group_worker_explain.split(',');
      for(const id of group_ids) {
        ids.push(parseInt(id));
      }
      this.currentEntity.group_worker_explain = ids;
    }
  }

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

  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;
      }
    }

     //同序号考场分组判断
     if(this.currentEntity.group_worker_explain) {
      const ids = this.currentEntity.group_worker_explain;
      for(const id of ids) {
        if(id === this.currentEntity.ly_group_id) {
          this.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;
    debugger
    MsgService.confirmAlert('确定要将这些科目进行分组合并吗？', () => {
      AjaxService.request('layout/group/union.htm', this.currentEntity, () => {
        MsgService.successAlert('面试科目分组合并操作成功！',  () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }

  //设置冲突类型
  setConflictType() {
    //多人分组考试，默认强冲突
    if(this.currentEntity.pack_flag == 'Active') {
      this.currentEntity.conflict_type = 1;
    } else {
      this.currentEntity.conflict_type = 2;
    }
  }


}
