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

  provinceArr = [];
  subjectArr = [];
  varietyArr = [];
  room_name: any;
  remain_total: 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();
  }

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

  //查找该考场-可评分的科目
  requestSubject() {
    this.AjaxService.requestArray('ly/subject/list/by/group.htm', {ly_group_id: this.ResponseEntity.group.ly_group_id}, (array) => {
      this.subjectArr = array;
      for(const subject of this.subjectArr) {
        subject.subject_full_name = subject.subject_code + '|' + subject.subject_name;
      }
      if(this.subjectArr && this.subjectArr.length == 1) {
        this.ResponseEntity.subjct_id = this.subjectArr[0].subject_id;
        this.requestVariety();
      }
    });
  }

  //查找-品种
  requestVariety() {
    this.AjaxService.requestArray('ly/subject/variety/list.htm', {ly_room_id: this.ResponseEntity.ly_room_id}, (array) => {
      this.varietyArr = array;
    });
  }

  //待分发总量
  getRemainTotal() {
    this.AjaxService.requestEntity('ex/task/dist/remain/total.htm', {ly_room_id: this.ResponseEntity.ly_room_id}, (entity) => {
      this.remain_total = entity;
    })
  }

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

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



}
