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

@Component({
  selector: 'app-exam-interview-judge-score',
  templateUrl: './exam-interview-judge-score.component.html',
  styleUrls: ['./exam-interview-judge-score.component.scss']
})
export class ExamInterviewJudgeScoreComponent extends FramePageComponent implements OnInit {

  checkin: any;
  agentArray = [];
  dateArray = [];
  roomArray = [];
  roomMap = {};
  private intervalId: any;
  currentStep:any;
  constructor(modalService: BsModalService) {
    super(modalService);
    this.goInit();
   }

  ngOnInit() {
  }

  goInit() {
    clearTimeout(this.intervalId);
    this.agentArray = [];
    this.dateArray = [];
    this.checkin = {start_time: '06:00', end_time: '23:00'};

    // 获取所有考点
    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';
    });
  }

   // 获取考试日期
   requestDateArray() {
    AjaxService.requestArray('exam/checkin/init/date.htm', this.checkin, (array) => {
      this.dateArray = [];
      // 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;
      }
    });
  }

  ngOnDestroy(): void {
    clearTimeout(this.intervalId);
  }

  judgeScore() {
    if(!this.checkin.exam_date) {
      MsgService.errorAlert('请选择考试日期！');
      return;
    }
    if(!this.checkin.start_time || !this.checkin.end_time) {
      MsgService.errorAlert('请填写开考时间范围！');
      return;
    }
    this.currentStep = 'Scoreing';
    this.queryJudgeScore();
    this.intervalId = setInterval(this.queryJudgeScore.bind(this), 10000);
  }

  queryJudgeScore() {
    AjaxService.requestMap('exam/judge/grade/total.htm', this.checkin, (map) => {
      this.roomMap = map;
      this.roomArray = map['roomArray'];
      this.checkin.refresh_time = this.FuncService.dateToLongString(new Date());
    });
  }

}
