import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

@Component({
  selector: 'app-room-interview-task-append-new',
  templateUrl: './room-interview-task-append-new.component.html',
  styleUrls: ['./room-interview-task-append-new.component.scss']
})
export class RoomInterviewTaskAppendNewComponent extends FrameDetailComponent implements OnInit {

  provinceArr = [];
  subjectArr = [];
  varietyArr = [];
  room_name: any;
  remain_total: any;
  currStep = 'init';
  remain_num : any;

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

  ngOnInit(): void {
    this.initProvince();
    this.ResponseEntity.std_province_flag='';
    this.ResponseEntity.std_sex = '不限';
    // this.requestSubject();
    this.getRemainTotal();
    this.requestVariety();
  }

  initDefautEntity(): void {
    this.room_name = this.ResponseEntity.group.group_name + '第' + this.ResponseEntity.room_seq +'考场';
  }

  requestVariety() {
    this.AjaxService.requestArray('ex/task/dist/variety/stdnum.htm', {ly_group_id: this.ResponseEntity.group.ly_group_id }, (array) => {
      this.varietyArr = array;
    });
  }

  //待分发总量
  getRemainTotal() {
    this.AjaxService.requestMap('ex/task/dist/remain/total.htm', {ly_room_id: this.ResponseEntity.ly_room_id}, (entity) => {
      this.remain_total = entity.std_total +"(省内 " + entity.in_std_total+"，省外 " + 
                          entity.out_std_total +" | 男 " + entity.male_total +",女 " + entity.female_total+")";
      this.remain_num = entity.std_total;
    })
  }

   //初始化省份数组
   initProvince() {
    this.provinceArr.push({province_id : '', province_name: '不限'});
    this.provinceArr.push({province_id : 'Active', province_name: '省内'});
    this.provinceArr.push({province_id : 'InActive', province_name: '省外'});
  }

  //设置品种
  addVariety() {
    this.AjaxService.requestArray('ex/task/dist/variety/stdnum.htm', {ly_group_id:  this.ResponseEntity.group.ly_group_id}, (array) => {
      this.varietyArr = array;
      let order_num = 1;
      for(let variety of this.varietyArr) {
        if(!variety.std_sex) {
          variety.std_sex = '不限';
        }
        if(!variety.std_province_flag){
          variety.std_province_flag = '';
        }
      }
      this.currStep = 'variety';
    });
  }

  //确认品种
  confirmVariety() {
    let arr = [];
    for(let variety of this.varietyArr) {
        if(variety.real_std_total == 0 || !variety.real_std_total) continue;
        arr.push(variety);
    }
    if(arr.length == 0) {
      this.MsgService.errorToastr('请至少设置一个品种！');
      return;
    }
    let std_num = 0;
    //判断考生的数量
    for(let v of arr) {
      //无限制
      if(v.std_sex == '不限' && !v.std_province_flag) {
        if(v.real_std_total > v.std_total) {
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
      }
      //只限制性别
      if(v.std_sex !== '不限' && !v.std_province_flag) {
        if(v.std_sex == '男' && v.real_std_total > v.male_total){
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
        if(v.std_sex == '女' && v.real_std_total > v.female_total){
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
      }
      //只限制省份
      if(v.std_sex == '不限' && v.std_province_flag) {
        if(v.std_province_flag == 'Active' && v.real_std_total > v.in_province_total) {
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
        if(v.std_province_flag == 'InActive' && v.real_std_total > v.out_province_total) {
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
      }
      std_num += v.real_std_total;
    }
    this.ResponseEntity.std_num = std_num;
    this.currStep = 'init' ;
    this.ResponseEntity.varietyArr = this.varietyArr;
  }

  //查看
  viewVariety() {
    this.currStep = 'variety';
  }

   //任务追加
   addTask() {
    if(!this.ResponseEntity.std_num) {
      this.MsgService.errorToastr('请填写要追加的考生数量！');
      return;
    }
    if(parseInt(this.ResponseEntity.std_num,10) > parseInt(this.remain_num,10)) {
      this.MsgService.errorToastr('追加的考生数量大于剩余的考生总数，无法追加！');
      return;
    }
    if(this.ResponseEntity.varietyArr && this.ResponseEntity.varietyArr.length > 0) {
      this.ResponseEntity.variety_json = JSON.stringify(this.ResponseEntity.varietyArr);
    }
    this.MsgService.confirmAlert('确定要追加考生到' + this.room_name +"吗?", () => {
      this.AjaxService.request('ex/task/dist/append.htm', this.ResponseEntity, () => {
        this.MsgService.successToastr('追加成功!');
        this.closeWindow();
      });
    });
  }

}
