import {Component, OnDestroy, OnInit} from '@angular/core';
import {ExamInterviewRoomJudgeComponent} from '../exam-interview-room-judge/exam-interview-room-judge.component';
import {ExamInterviewRoomStdComponent} from '../exam-interview-room-std/exam-interview-room-std.component';
import { ExamInterviewJudgeScorePrintComponent } from '../exam-interview-judge-score-print/exam-interview-judge-score-print.component';
import { ExamInterviewRoomBindComponent } from '../exam-interview-room-bind/exam-interview-room-bind.component';
import { ExamInterviewRoomBatchComponent } from '../exam-interview-room-batch/exam-interview-room-batch.component';
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-room',
  templateUrl: './exam-interview-room.component.html',
  styleUrls: ['./exam-interview-room.component.scss']
})
export class ExamInterviewRoomComponent extends FramePageComponent implements OnInit, OnDestroy {

  checkin: any;
  agentArray = [];
  dateArray = [];
  roomArray = [];
  private intervalId: any;
  examShowInterview: any;
  timeArray = [];

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

  // 获取考试日期
  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 = new Array();
            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.changeDate();
      }
    });
  }


  goInit() {
    clearTimeout(this.intervalId);
    this.agentArray = [];
    this.dateArray = [];
    this.checkin = {};
    this.timeArray = [];

    // 获取所有考点
    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.checkin.ly_agent_id = this.agentArray[0].ly_agent_id;
        this.requestDateArray();
      }
    });
    let array = FrameService.getFrameDictArray('ExamTimeType');
    this.timeArray = array;
    if(this.timeArray.length>0 && this.timeArray[0].dict_text !== '全天') {
      this.timeArray.splice(0, 0, {dict_text: '全天', dict_value: undefined});
    }
  }

  changeDate() {
    clearTimeout(this.intervalId);
    this.queryRoom();
    this.intervalId = setInterval(this.queryRoom.bind(this), 15000);
  }

  queryRoom() {
    AjaxService.requestArray('exam/interview/room/list.htm', this.checkin, (array) => {
      this.checkin.refresh_time = this.FuncService.dateToLongString(new Date());
      this.roomArray = array;
    });
  }

  ngOnInit() {
    // AjaxService.requestMap('exam/interview/room/std/param.htm', {}, (map) => {
    //   this.examShowInterview = map['examShowInterview'];
    // });
  }

  /**************
   * 显示评委
   */
  showJudge(room) {
    this.showComponentModal(ExamInterviewRoomJudgeComponent, {
      ResponseEntity: room,
      mapUrl: 'exam/interview/room/judge/list.htm',
      mapParam: {ly_room_id: room.ly_room_id}
    });
  }
  /**************
   * 显示评委
   */
  showStd(room) {
    if(this.checkin.time_type) {
      room.time_type = this.checkin.time_type;
    }
    this.showComponentModal(ExamInterviewRoomStdComponent, {
      ResponseEntity: room
    }, {class: 'modal-95p'});
  }

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

  //显示打印评委评分
  showPrintScore(room) {
    room.time_type = this.checkin.time_type;
    this.showComponentModal(ExamInterviewJudgeScorePrintComponent, {
      ResponseEntity : room
    }, {class: 'modal-5p'});
  }

  // 会议链接绑定
  bindMeetingUrl(room) {
    this.onModalClose = () => {
      this.queryRoom();
    };
    this.showComponentModal(ExamInterviewRoomBindComponent, {
      ResponseEntity: room
    }, {class: 'modal-5p'});
  }

  //批量入场
  batchInRoom(room) {
    this.showComponentModal(ExamInterviewRoomBatchComponent, {
      ResponseEntity: room
    })
  }

  //关闭考场
  colseRoom(room) {
    MsgService.confirmAlert('确定要关闭考场吗？,一旦关闭，考生将无法入场！', () => {
      AjaxService.request('exam/interview/room/close.htm', room , () => {
        MsgService.successToastr('考场成功关闭!');
      });
    });
  }

    //下载考场
    downloadRoom() {
      this.FuncService.ajaxDownload('ly/room/interview/export.htm', {agent_id: this.checkin.agent_id, exam_date: this.checkin.exam_date});
    }


}

