import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { RoomInterviewJudgeHandComponent } from '../room-interview-judge-hand/room-interview-judge-hand.component';
import { RoomInterviewJudgeScoreComponent } from '../room-interview-judge-score/room-interview-judge-score.component';
import { RoomInterviewJudgeComponent } from '../room-interview-judge/room-interview-judge.component';
import { RoomInterviewStdComponent } from '../room-interview-std/room-interview-std.component';
import { RoomInterviewTaskAppendComponent } from '../room-interview-task-append/room-interview-task-append.component';
import { RoomInterviewVideoStdComponent } from '../room-interview-video-std/room-interview-video-std.component';
import { RoomInterviewTaskAppendNewComponent } from '../room-interview-task-append-new/room-interview-task-append-new.component';

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

  schoolArr = [];
  batchArr = [];
  dateArr = [];
  timeArr = [];
  roomArr = [];
  room: any;
  private intervalId: any;

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

  //数据初始化
  goInit() {
    this.schoolArr = [];
    this.batchArr = [];
    this.dateArr = [];
    this.timeArr = [];
    this.room = {};
    clearTimeout(this.intervalId);
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.room.school_id = this.schoolArr[0].school_id;
        this.listBatch();
      }
    });
    this.listTimeType();
  }

  //时段查询
  listTimeType() {
    let array = this.FrameService.getFrameDictArray('ExamTimeType');
    this.timeArr = array;
    if(this.timeArr.length>0 && this.timeArr[0].dict_text !== '全天') {
      this.timeArr.splice(0, 0, {dict_text: '全天', dict_value: undefined});
    }
  }

  //通过学校-查找批次
  listBatch() {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : this.room.school_id
    }, (array) => {
      this.batchArr = array;
      this.cdr.detectChanges();
      if(this.batchArr && this.batchArr.length == 1) {
        this.room.batch_id = this.batchArr[0].batch_id;
        this.listExamDate();
      }
    });
  }

  //通过批次-查找考试日期
  listExamDate() {
    this.AjaxService.requestArray('ly/checkin/date/list.htm', {
      batch_id : this.room.batch_id
    }, (array) => {
      this.dateArr = array;
      this.cdr.detectChanges();
      if(this.dateArr && this.dateArr.length == 1) {
        this.room.exam_date = this.dateArr[0].exam_date;
        this.changeDate();
      }
    });
  }

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

  //请求考场
  queryRoom() {
    this.AjaxService.requestArray('ly/room/interview/list.htm', this.room, (array) => {
      this.room.refresh_time = this.FuncService.dateToLongString(new Date());
      this.roomArr = array;
    });
  }

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


  ngOnInit(): void {
  }

  //考生
  showStd(room) {
    room.time_type = this.room.time_type;
    this.showComponentModal(RoomInterviewStdComponent, {
      ResponseEntity: room
    });
  }

  //评委
  showJudge(room) {
    this.showComponentModal(RoomInterviewJudgeComponent, {
      ResponseEntity: room
    });
  }

  //任务追加
  showStdTask(room) {
    //RoomInterviewTaskAppendComponent
    this.showComponentModal(RoomInterviewTaskAppendNewComponent, {
      ResponseEntity: room
    }, {class: 'modal-6p'});
  }

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

  //视频审核考生
  showVideoStd(room) {
    this.showComponentModal(RoomInterviewVideoStdComponent, {
      ResponseEntity: room
    }, {class:'modal-5p'});
  }

  //评委手账
  showPrintHand(room) {
    this.showComponentModal(RoomInterviewJudgeScoreComponent, {
      ResponseEntity: room
    }, {class:'modal-5p'});
  }

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

  showAllStd(room: any) {
    this.FuncService.ajaxDownload('ex/task/dist/export/stds.htm', {room_id: room.ly_room_id});
  }
}
