import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from 'src/app/routes/frame/core/page/frame.page';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FrameService } from '../../../../../core/service/frame.service';

declare let laydate;


@Component({
  selector: 'app-exam-room-extract-search',
  templateUrl: './exam-room-extract-search.component.html',
  styleUrls: ['./exam-room-extract-search.component.scss']
})
export class ExamRoomExtractSearchComponent extends FramePageComponent implements OnInit {
  
  agentArray = []; //考点列表
  groupArray = []; //考试的分组
  dateArray = []; //分组对应的考试日期
  batchArr = []; //抽取批次
  currentStep: any; //抽取步骤
  checkin : any;
  groupName: any;
  stdArr = []; //抽取到的考生


  constructor(moalService : BsModalService) {
    super(moalService);
    this.goInit();
   }

  ngOnInit(): void {
  }

  //初始化信息
  goInit() {
    this.currentStep === 'Init';
    this.agentArray = [];
    this.dateArray = [];
    this.groupArray = [];
    this.checkin = {start_time: '06:00', end_time: '23:00'};
    this.groupName = "";
    this.getDate();
    // 获取所有考点
    AjaxService.requestArray('exam/checkin/init/agent.htm', {}, (array) => {
      this.agentArray = array;
      if (this.agentArray.length === 1) {
        this.checkin.agent_id = this.agentArray[0].ly_agent_id;
        this.requestDateArray();
      }
      this.currentStep = 'Init';
    });
  }

  //初始化时间控件
  getDate() {
    let _this = this;
    setTimeout(() => {
      laydate.render({
        elem:'#startTime',
        type:'time',
        theme:'#0c6acf',
        trigger: 'click',
        format: 'HH:mm',
        done:(value, date, endDate)=>{
          _this.checkin.start_time = value;
        }
      });
      laydate.render({
        elem:'#endTime',
        type:'time',
        theme:'#0c6acf',
        format: 'HH:mm',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.checkin.end_time = value;
        }
      });  
    }, 500);
  }

  // 获取考试日期
  requestDateArray() {
    AjaxService.requestArray('exam/checkin/init/date.htm', this.checkin, (array) => {
      if (FrameService.FrameOptr.optr_id !== 0) {
        for (const d of array) {
          if (d.ly_room_id === 0) {
            const s = [];
            s.push(d);
            this.dateArray = s;
            break;
          }
        }
        if(this.dateArray ===undefined || this.dateArray.length ==0) {
          this.dateArray = array;
        }
      } else {
        this.dateArray = array;
      }
      if (this.dateArray.length === 1) {
        this.checkin.exam_date = this.dateArray[0].exam_date;
        this.requestGroupArray();
      }
    });
  }

    // 获取分组
    requestGroupArray() {
      AjaxService.requestArray('exam/checkin/init/group.htm', this.checkin, (array) => {
        this.groupArray = array;
      });
    }

  //选择的分组名称
  getGroupName() {
    for(const group of this.groupArray) {
      if(group.ly_group_id === this.checkin.ly_group_id) {
        this.groupName = group.group_name;
      }
    }
  } 

  //获取抽取批次
  getExtractBatch() {
    AjaxService.requestArray('exam/extract/get/batch.htm', this.checkin, (array) => {
      this.batchArr = array;
      for(const batch of this.batchArr) {
        batch.batch_name = '第【' + batch.extract_batch + '】批次';
      }
    });
  }

  startExtract() {
    this.currentStep = 'Extracting';
    this.getExtractBatch();
    this.requestExtractStd();

  }

  //获取抽到的考生
  requestExtractStd() {
    AjaxService.requestArray('exam/extract/get/std.htm', this.checkin, (array) => {
      this.stdArr = array;
    }, {RequestWithLoading: true});
  }

   // 一键抽取
    allExtract() {
      let msg = "一键抽取会将该分组内已经检录的考生抽取，确认要抽取吗？";
      this.requestWithThread('./exam/extract/stdAll.htm', this.checkin, {confirmMsg:msg}, () => {
        this.startExtract()
      } );
    }

    //导出
    export() {
      this.FuncService.ajaxDownload('exam/extract/export.htm', this.checkin);
    }

}
