import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {DomSanitizer} from '@angular/platform-browser';
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';
import { ModalService } from '../../../../../core/service/modal.service';

declare let laydate;

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

  groupArray;
  currStep = 'init';
  currGroupArray;
  exam_date;
  provinceArr = [];

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

  }

  ngOnInit() {
    // this.getDate();
    this.provinceArr.push({province_id : '', province_name: '不限'});
    this.provinceArr.push({province_id : 'Active', province_name: '省内'});
    this.provinceArr.push({province_id : 'InActive', province_name: '省外'});
    AjaxService.requestArray('exam/video/dist/total.htm', {ly_agent_id: this.ResponseEntity}, (array) => {
      // this.groupArray = array;
      for (const g of array) {
        if (g.exam_seconds > g.nolayout_total)
          g.exam_seconds = g.nolayout_total;
      }
      this.groupArray = array;
      for(const group of this.groupArray) {
        if(group.room_total === undefined) {
          group.room_total = 1;
        }
      }
    });
  }

  // 确认任务数量
  confirmTotal() {
    const array = [];
    for (const gx of this.groupArray) {
      if(gx.nolayout_total < 1) {
        continue;
      }
      if (gx.exam_seconds < 1 && gx.room_total < 1) {
        continue;
      }
      let room_std = Math.floor(gx.exam_seconds / 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.exam_seconds - layout_std;
        }
        layout_std += room_std;
        let rowspan = 1;
        if (i === 1) {
          rowspan = g.room_total;
        }
        g.rowspan = rowspan;
        g.room_seq = i;
        g.ly_std_total = room_std;
        g.room_addr = `第${i}考场`;
        // if(i % 2 == 0) {
        //   g.std_sex = '女';
        // } else {
        //   g.std_sex = '男';
        // }
        g.std_sex='不限';
        g.std_province = 'InActive';
        array.push(g);
      }
    }
    if (array.length === 0) {
      MsgService.errorToastr('至少需要一个面试项目！');
      return;
    }
    this.currGroupArray = array;
    this.currStep = 'room';
    this.getDate();
  }

  totalRoomStd(g) {
    let total = 0;
    for (const gx of this.currGroupArray) {
      if (gx.ly_group_id !== g.ly_group_id || gx.ly_std_total < 1)
        continue;
      if (gx.ly_group_id === g.ly_group_id)
        total += gx.ly_std_total;
    }
    // for (const gx of this.currGroupArray) {
    //   gx.exam_seconds = total;
    // }
  }


  confirmRoom() {
    if (this.exam_date === undefined) {
      MsgService.errorToastr('请输入视频评分日期！');
      return;
    }
    for (const gx of this.currGroupArray) {
      gx.ly_std_total = parseInt(gx.ly_std_total + '', 10);
      if (gx.ly_std_total < 1) {
        MsgService.errorToastr('考生考生数量错误，请检查！');
        return;
      }
    }
    MsgService.confirmAlert('确定保存考场配置并分发任务吗？', () => {
      AjaxService.requestEntity('exam/video/dist/save.htm', {
        ly_agent_id: this.ResponseEntity,
        exam_date: this.exam_date,
        room_json: JSON.stringify(this.currGroupArray)
      }, (entity) => {
        ModalService.showThreadWindow(this.framePageComponent.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
        this.closeWindow();
      }, {RequestWithLoading: true});
    });
  }

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

 
}
