import { Component, 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';


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

  checkin: any;
  room_key: any;
  roomList = [];
  private intervalId: any;
  examShowInterview: any;
  currentStep:any;

  constructor(modalService: BsModalService) { 
    super(modalService);
    this.currentStep = 'Init';
    this.checkin = {};
  }

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

  //返回输入考场秘钥主窗口
  goInit() {
    this.checkin = {};
    clearTimeout(this.intervalId);
    this.currentStep = 'Init';
  }
  

  changePassword() {
    this.intervalId = setInterval(this.queryRoom.bind(this), 30000);
    if (this.room_key=== undefined || $.trim(this.room_key).length === 0)
      return;
    AjaxService.requestEntity('exam/interview/room/online/find.htm', {room_key: this.room_key}, (entity) => {
      this.currentStep = 'Room';
      if(entity) {
        this.roomList = [];
        this.roomList.push(entity);
      }
    }, {
      MsgType: 'Swal'
    });
  }

  queryRoom() {
    AjaxService.requestEntity('exam/interview/room/online/find.htm', {room_key: this.room_key}, (entity) => {
      this.currentStep = 'Room';
      this.checkin.refresh_time = this.FuncService.dateToLongString(new Date());
      if(entity) {
        this.roomList = [];
        this.roomList.push(entity);
      }
    });
  }
  
  //
/**************
   * 显示评委
   */
  showJudge(room) {
    this.showComponentModal(ExamInterviewRoomJudgeComponent, {
      ResponseEntity: room,
      mapUrl: 'exam/interview/room/judge/list.htm',
      mapParam: {ly_room_id: room.ly_room_id}
    });
  }
  /**************
   * 显示考生
   */
  showStd(room) {
    this.showComponentModal(ExamInterviewRoomStdComponent, {
      ResponseEntity: room
    }, {class: 'modal-95p'});
  }

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

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

  // 会议链接绑定
  bindMeetingUrl(room) {
    this.onModalClose = () => {
      this.changePassword();
    };
    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('考场成功关闭!');
      });
    });
  }


}
