import {Component, OnInit} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

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

  currentStep;
  currentJudge;

  constructor(bsModalRef: BsModalRef, sanitizer: DomSanitizer) {
    super(bsModalRef, sanitizer);
  }

  ngOnInit() {
  }

  initDefautEntity() {
    this.currentEntity.room_id = this.currentEntity.ly_room_id;
  }

  clearMac(judge) {
    if (judge.pad_mac === undefined)
      return;
    judge.ly_room_id = judge.room_id;
    MsgService.confirmAlert(`确定要清除评委【${judge.judge_name}】的绑定设备吗？`, () => {
      AjaxService.request('exam/interview/judge/clear/mac.htm', judge, () => {
        this.requestJudgeArray();
      });
    });
  }

  clearRoomMac() {
    MsgService.confirmAlert(`确定要清除考场【${this.currentEntity.room_addr}】的所有终端设备？`, () => {
      AjaxService.request('exam/interview/judge/clear/mac/all.htm', {ly_room_id: this.currentEntity.ly_room_id}, () => {
        this.requestJudgeArray();
      });
    });
  }


  requestJudgeArray() {
    AjaxService.requestMap('exam/interview/room/judge/list.htm', {ly_room_id: this.currentEntity.ly_room_id}, (map) => {
      this.ResponseMap = map;
    });
  }

  goChangeJudge(judge) {
    this.currentJudge = judge;
    this.currentStep = 'judge';
  }


  changeJudge(judge) {
    if (judge.judge_id === this.currentJudge.judge_id) {
      return;
    }
    for (const j of this.ResponseMap['JudgeArray']) {
      if (j.judge_id === judge.judge_id) {
        MsgService.errorToastr('该评委已经在此考场！');
        return;
      }
    }
    MsgService.confirmAlert(`确定要将评委【${this.currentJudge.judge_name}】替换成【${judge.judge_name}】？`, () => {
      AjaxService.request('exam/interview/judge/change.htm', {
        ly_room_id: this.currentEntity.ly_room_id,
        judge_id: this.currentJudge.judge_id,
        new_judge_id: judge.judge_id
      }, () => {
        this.currentJudge = undefined;
        this.currentStep = undefined;
        this.ResponseMap = undefined;
        this.requestJudgeArray();
      });
    });
  }

}
