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-exam-video-room-task-new',
  templateUrl: './exam-video-room-task-new.component.html',
  styleUrls: ['./exam-video-room-task-new.component.scss']
})
export class ExamVideoRoomTaskNewComponent extends FrameDetailComponent implements OnInit {

  room_name: any;
  provinceArr = [];
  remainStd: any;
  remainInfo: any;
  varietyArr = [];
  currStep = 'init';

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

  ngOnInit() {
    this.initProvince();
    this.getRemainStd();
    this.getVarietyStdNum();
  }

  initDefautEntity() {
    this.room_name = this.ResponseEntity.group.group_name + '第' + this.ResponseEntity.room_seq + '考场';
    this.ResponseEntity.std_sex = '不限';
    this.ResponseEntity.std_province = '';
  }

  initProvince() {
    this.provinceArr.push({province_id : '', province_name: '不限'});
    this.provinceArr.push({province_id : 'Active', province_name: '省内'});
    this.provinceArr.push({province_id : 'InActive', province_name: '省外'});
  }

  getRemainStd() {
    AjaxService.requestMap('exam/video/dist/remain/stdnum.htm', {ly_group_id : this.ResponseEntity.ly_group_id}, (map) => {
      this.remainStd = map;
      if(map) {
        this.remainInfo = map.std_total + '(省内:' + (!map.in_std_total ? 0:map.in_std_total) +",省外:" + (!map.out_std_total ? 0: map.out_std_total)
        +" | 男:" + (!map.male_total ? 0 : map.male_total) +",女:" + (!map.female_total ? 0 : map.female_total)+")";
      }
    });
  }

  getVarietyStdNum() {
    AjaxService.requestArray('exam/video/dist/variety/stdnum.htm', {ly_group_id : this.ResponseEntity.ly_group_id}, (array) => {
      this.varietyArr = array;
    });
  }

  addVariety() {
    for(let variety of this.varietyArr) {
      if(!variety.std_sex) {
        variety.std_sex = '不限';
      }
      if(!variety.std_province){
        variety.std_province = '';
      }
    }
    this.currStep = 'variety';
  }

    //查看
    viewVariety() {
      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) {
        if(v.real_std_total > v.std_total) {
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
      }
      //只限制性别
      if(v.std_sex !== '不限' && !v.std_province) {
        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) {
        if(v.std_province == 'Active' && v.real_std_total > v.in_province_total) {
          this.MsgService.errorToastr(v.variety_name +'的考生数量大于剩余未分发的考生数量！');
          return;
        }
        if(v.std_province == '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;
  }

  addTask() {
    if(!this.ResponseEntity.std_num) {
      MsgService.errorToastr('请填写需要分发的考生数量！');
      return;
    }
    if(parseInt(this.ResponseEntity.std_num,10) > parseInt(this.remainStd.std_total,10)) {
      this.MsgService.errorToastr('追加的考生数量大于剩余的考生总数，无法追加！');
      return;
    }
    if(this.ResponseEntity.varietyArr && this.ResponseEntity.varietyArr.length > 0) {
      this.ResponseEntity.variety_json = JSON.stringify(this.ResponseEntity.varietyArr);
    }
    MsgService.confirmAlert('确认要追加考生到' + this.room_name + "吗？", () => {
      AjaxService.request('exam/video/room/task/new/save.htm', this.ResponseEntity , () => {
        MsgService.successToastr('追加考生成功！');
        this.closeWindow();
      }, {RequestWithLoading: true});
    });
    
  }

}
