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

declare let laydate;

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


  currStep = 'init';
  exam_date: any;
  groupArr = [];
  currGroupArr = [];
  provinceArr = [];
  std_province_flag : any;

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

  ngOnInit(): void {
    this.initProvince();
    this.AjaxService.requestArray('ex/task/dist/total.htm', {batch_id: this.ResponseEntity.batch_id}, (array) => {
      for(const g of array) {
        if(g.nolayout_total < 200) {
          g.std_num = g.nolayout_total;
        }
      }
      this.groupArr = array;
      for(const group of this.groupArr) {
        if(group.room_total == undefined) {
          group.room_total = 1;
        }
      }
    });
  }

  //确认任务数量
  confirmTotal() {
    const array = [];
    for(const gx of this.groupArr) {
      if(gx.nolayout_total < 1)  continue;
      if(gx.std_num < 1 && gx.room_total < 1) continue;
      let room_std = Math.floor(gx.std_num / gx.room_total);
      let layout_std = 0;
      for(let i =1 ;i<=gx.room_total; i++) {
        const g = Object.assign({},gx);
        if(i == g.room_total) {
          room_std = g.std_num - layout_std;
        }
        layout_std += room_std;
        let rowspan = 1;
        if(i == 1) {
          rowspan = g.room_total;
        }
        g.rowspan = rowspan;
        g.room_seq = i;
        g.real_std_total = room_std;
        g.room_addr = `第${i}考场`;
        g.std_sex = '不限';
        g.std_province_flag = 'InActive';
        g.varietyArr = gx.varietyList;
        array.push(g);
      }
    }

     if(array.length == 0) {
      this.MsgService.errorToastr('至少需要一个任务！');
      return;
     }
     this.currGroupArr = array;
     this.currStep = 'room';
  }


  //确认分发
  confirmRoom() {
    if(!this.exam_date) {
      this.MsgService.errorToastr('请选择评分日期！');
      return;
    }
    for(const gx of this.currGroupArr) {
      gx.real_std_total = parseInt(gx.real_std_total+'',10);
      if(gx.real_std_total < 1) {
        this.MsgService.errorToastr('考生数量需要大于等于1，请检查！');
        return ;
      }
    }
    //判断是否存在考场
    this.AjaxService.requestEntity('ex/task/dist/room/exists.htm', {
      exam_date: this.exam_date, 
      room_json: JSON.stringify(this.currGroupArr)}, (entity) => {
        let remark = '定要分发任务吗？';
        if(entity && entity.group_name) {
          remark = '分组【' + entity.group_name +'】已经存在考场，确定还要分发吗？';
        }
        //确定分发
        this.MsgService.confirmAlert(remark, () => {
          this.AjaxService.requestEntity('ex/task/dist/save.htm', {
            school_id: this.ResponseEntity.school_id,
            batch_id : this.ResponseEntity.batch_id,
            exam_date: this.exam_date,
            room_json: JSON.stringify(this.currGroupArr)
          }, (entity) => {
            ModalService.showThreadWindow(this.framePageComponent.bsService, entity, {threadTitle: '正在分发任务，请稍等'});
              this.closeWindow();
          }, {RequestWithLoading: true});
        });
      });
  }

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

  //省份-批量选择操作
  checkAll() {
    if(this.std_province_flag== undefined) {
      return;
    }
    for(const group of this.currGroupArr) {
        group.std_province_flag = this.std_province_flag;
    }
  }


  //初始化时间控件
  getDate() {
    setTimeout(() => {
      laydate.render({
        elem:'#exam_dates',
        type:'date',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.exam_date = value;
        }
      });
    }, 10);
  }

}
