import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';

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

  score: any;
  currentStep: any;
  schoolArr = [];
  batchArr = [];
  dateArr = [];
  roomArr = [];
  roomMap = {};
  private intervalId : any;

  constructor(modalService: BsModalService, private cdr : ChangeDetectorRef) { 
    super(modalService);
    this.goInit();
  }

  ngOnInit(): void {
  }

  goInit() {
    clearTimeout(this.intervalId);
    this.schoolArr = [];
    this.batchArr = [];
    this.dateArr = [];
    this.roomArr = [];
    this.score = {};
    this.AjaxService.requestArray('cf/school/list.htm', {}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.score.school_id = this.schoolArr[0].school_id;
        this.requestBatch();
      }
      this.currentStep = 'Init';
    });
  }

  //批次
  requestBatch() {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : this.score.school_id
    }, (array) => {
      this.batchArr = array;
      this.cdr.detectChanges();
      if(this.batchArr && this.batchArr.length == 1) {
        this.score.batch_id = this.batchArr[0].batch_id;
        this.requestDate();
      }
    });
  }

  //考试日期
  requestDate() {
    this.AjaxService.requestArray('ly/checkin/date/list.htm', {batch_id: this.score.batch_id}, (array) => {
      this.dateArr = array;
    });
  }

  //评委评分统计查看
  judgeScore() {
    if(!this.score.exam_date) {
      this.MsgService.errorToastr('请选择考试日期');
      return;
    }
    this.currentStep = 'Scoreing';
    this.JudgeScoreStat();
    //15秒刷新
    this.intervalId = setInterval(this.JudgeScoreStat.bind(this), 15000);
  }

  JudgeScoreStat() {
    this.AjaxService.requestMap('ex/judge/score/report/total.htm', this.score, (map) => {
      this.roomMap = map;
      this.roomArr = map['roomArr'];
      this.score.refresh_time = this.FuncService.dateToLongString(new Date());
    });
  }



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



}
