import { Component, OnInit } from '@angular/core';
import { ExamVideoRoomStdComponent } from '../exam-video-room-std/exam-video-room-std.component';
import { ExamVideoRoomInComponent } from '../exam-video-room-in/exam-video-room-in.component';
import { ExamVideoRoomJudgeComponent } from '../exam-video-room-judge/exam-video-room-judge.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';
import { ExamVideoManualPrintComponent } from '../exam-video-manual-print/exam-video-manual-print.component';
import { ExamInterviewJudgeScorePrintComponent } from '../../interview/exam-interview-judge-score-print/exam-interview-judge-score-print.component';
import { ExamVideoRoomHandComponent } from '../exam-video-room-hand/exam-video-room-hand.component';
import { ExamVideoRoomScoreComponent } from '../exam-video-room-score/exam-video-room-score.component';
import { FuncService } from '../../../../../core/service/func.service';
import { ExamVideoRoomScoreBeforeComponent } from '../exam-video-room-score-before/exam-video-room-score-before.component';
import { ExamVideoRoomVerifyComponent } from '../exam-video-room-verify/exam-video-room-verify.component';
import { ExamVideoReviewInitComponent } from '../exam-video-review-init/exam-video-review-init.component';
import { ExamVideoRoomTaskNewComponent } from '../exam-video-room-task-new/exam-video-room-task-new.component';

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

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

  ngOnInit() {
  }

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

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

  // 获取考试日期
  requestDateArray() {
    AjaxService.requestArray('exam/video/room/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.queryVideoRoom();
        this.changeDate();
      }
    });
  }

  changeDate() {
    this.queryVideoRoom();
    this.intervalId = setInterval(this.queryVideoRoom.bind(this), 10000);
  }

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


  //任务追加 
  addTask(room) {
    this.showComponentModal(ExamVideoRoomTaskNewComponent, {ResponseEntity: room}, {class: 'modal-8p'});
  }

  //显示评委
  showJudge(room) {
    this.showComponentModal(ExamVideoRoomJudgeComponent, {
      ResponseEntity: room,
      mapUrl: 'exam/interview/room/judge/list.htm',
      mapParam: {ly_room_id: room.ly_room_id}
    });
  }

  //显示播放视频
  showVideo(room) {
    this.showComponentModal(ExamVideoRoomStdComponent, {
      ResponseEntity : room
    })
  }

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

  //批量入场
  batchInroom(room) {
    this.showComponentModal(ExamVideoRoomInComponent, {
      ResponseEntity : room
    });
  }

  //开启考场
  startRoom(room) {
    MsgService.confirmAlert('确定要开启考场吗？', () => {
      AjaxService.requestEntity('exam/interview/room/start.htm', {ly_room_id: room.ly_room_id}, (entity) => {
        this.queryVideoRoom();
      }, {
        RequestWithLoading: true
      });
    });
  }

 //评委登录
 judgeLogin(room) {
  MsgService.confirmAlert('确定要对考场【'+room.room_addr+'】进行所有评委登录操作吗？', () => {
    AjaxService.requestEntity('exam/interview/room/judge/login.htm', room, () => {
      this.queryVideoRoom();
      MsgService.successAlert('已模拟所有评委登录，请10秒检查处理结果');
    }, {
      RequestWithLoading: true
    });
  });
 } 

  //下载手账
  downScorePdf(room) {
    this.showComponentModal(ExamVideoRoomHandComponent, {
      ResponseEntity : room
    }, {class: 'modal-5p'});
  }
  
  //下载评委评分表-空表
  downManualPdf(room) {
    this.showComponentModal(ExamVideoManualPrintComponent, {
      ResponseEntity : room
    }, {class: 'modal-5p'});
  }

  

  //下载评委分数表-带分数
  downScoreManualPdf(room) {
    this.showComponentModal(ExamVideoRoomScoreComponent, {
      ResponseEntity : room
    }, {class : 'modal-5p'});
  }

  downScoreDetailPdf(room) {
    room.score_detail = 'Active';
    this.showComponentModal(ExamVideoRoomHandComponent, {
      ResponseEntity : room
    }, {class: 'modal-5p'});
  }

  exportVideoStd(room) {
    this.showComponentModal(ExamVideoRoomVerifyComponent, {
      ResponseEntity : room
    }, {class: 'modal-5p'});
    
  }

    //评委提交之前打印pdf
    downScoreBeforePdf(room) {
      this.showComponentModal(ExamVideoRoomScoreBeforeComponent, {
        ResponseEntity : room
      }, {class: 'modal-5p'});
    }

    exportVideoStdScore(room) {
      FuncService.ajaxDownload('frame/report/download.htm', {report_name:'ZXGatScoreExport', report_title :room.group.group_name,room_id:room.ly_room_id});
    }

    exportStdRegInroom(room) {
      FuncService.ajaxDownload('frame/report/download.htm', {report_name:'StdRegInRoomZxReport', report_title :room.group.group_name,real_room_id:room.ly_room_id});
    }

    videoDataInit(room) {
      this.showComponentModal(ExamVideoReviewInitComponent, {
        ResponseEntity: room
      }, {class: 'modal-5p'});
    }

}
