import { Component, OnInit, ViewChild } from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import { BsModalService, ModalOptions, BsModalRef } from 'ngx-bootstrap/modal';
import { FramePager } from '../../../../../core/frame/pager/frame-pager/frame-pager';
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-std',
  templateUrl: './exam-interview-room-std.component.html',
  styleUrls: ['./exam-interview-room-std.component.scss']
})
export class ExamInterviewRoomStdComponent extends FrameDetailComponent implements OnInit {

  // @ViewChild('uploader', {static: false}) uploader: UploaderComponent;
  @ViewChild('inroomPager', {static: false}) inroomPager: FramePager;
  @ViewChild('layoutPager', {static: false}) layoutPager: FramePager;
  std_id : any;
  std_num : any;
  handflag : any;
  faceFlag : any;
  topicRandFlag : any;
  // 弹框服务
  bsService: BsModalService;
  // 弹窗参数
  bsOptions: ModalOptions;
  // 弹出框类型
  modalComponent: any;
  topicModesRef: BsModalRef;
  inroom_num : any;

  constructor(bsModalRef: BsModalRef, topicModesRef : BsModalRef, sanitizer: DomSanitizer, modalService: BsModalService) {
    super(bsModalRef, sanitizer);
    this.bsService = modalService;

    this.bsOptions = {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-75p'
    };
  }

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

  }

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

  getStdNum() {
    AjaxService.requestEntity('exam/interview/room/std/num.htm', {
      real_room_id : this.currentEntity.ly_room_id
    }, (entity) => {
      this.std_num = entity;
      this.inroomPager.reload({ly_room_id: this.currentEntity.ly_room_id});
    });
  }
  
  //上传截图
  uploderPic(std: any) {
    if(std) {
      this.std_id = std.std_id;
    //  this.uploader.uploadFiles({std_id : std.std_id,  ly_room_id: this.currentEntity.ly_room_id});
    }
  }

  afterUpload(event) {
    this.inroomPager.reload({ly_room_id: this.currentEntity.ly_room_id});
  }

  //结束考试
  endStdExam(item: any) {
    MsgService.confirmAlert('确定下一个考生入场吗？', () => {
      AjaxService.request('exam/interview/room/std/update.htm', {
        real_room_id: item.real_room_id,
        std_id : item.std_id
      }, () => {
        MsgService.successToastr('下一个考生入场成功！');
        this.getStdNum();
        this.inroomPager.reload({ly_room_id: this.currentEntity.ly_room_id});
      });
    });
  }

  //抽取考题
  showExtractTopics(v) {
    // this.showComponentModal(ExtractTopicComponent, {
    // 'ResponseEntity': v,
    // 'arrayUrl': 'conf/aspecttopic/list.htm',
    //   'arrayParam': {
    //     aspect_id: v.aspect_id
    //   }
    // });
  }

  // 弹框, 弹窗之前做好数据获取准备，为防止弹出窗口后报错
  showComponentModal(component: any, param?: any, options?: any) {
    if (param === undefined) {
      param = {};
    }
    const modal = Object.assign({}, this.bsOptions, options);
    modal.initialState = param;
    modal.initialState['FramePageComponent'] = this;
    // 如果需要去服务器请求数据
    if (param['entityUrl']) {
      AjaxService.requestEntity(param['entityUrl'], param['entityParam'], (entity) => {
        modal.initialState['ResponseEntity'] = entity;
        this.topicModesRef = this.bsService.show(component, modal);
      });
    } else if (param['mapUrl']) {
      AjaxService.requestMap(param['mapUrl'], param['mapParam'], (map) => {
        modal.initialState['ResponseMap'] = map;
        this.topicModesRef = this.bsService.show(component, modal);
      }, {RequestWithLoading: true});
    } else if (param['arrayUrl']) {
      AjaxService.requestArray(param['arrayUrl'], param['arrayParam'], (array) => {
        modal.initialState['ResponseArray'] = array;
        this.topicModesRef = this.bsService.show(component, modal);
      }, {RequestWithLoading: true});
    } else {
      this.topicModesRef = this.bsService.show(component, modal);
    }
  }

  //取消进场
  cancelInRoom(std?) {
    MsgService.confirmAlert('确定要取消该考生进场吗？', () => {
      AjaxService.request('exam/extract/std/cancel/inroom.htm', {
        room_id : this.currentEntity.ly_room_id,
        std_id : std.std_id,
        real_exam_seq: std.real_exam_seq
      }, () => {
        MsgService.successAlert('取消成功，请提醒评委重新登录！');
        this.getMaxInroomNum();
        this.layoutPager.reload({ly_room_id: this.currentEntity.ly_room_id});
      });
    });
  }

  getMaxInroomNum() {
    AjaxService.requestEntity('exam/extract/get/max/inroom.htm', {room_id: this.currentEntity.ly_room_id}, (entity) => {
      this.inroom_num = entity;
      this.inroomPager.reload({ly_room_id: this.currentEntity.ly_room_id});
    })
  }

  refresh() {
    this.inroomPager.reload({ly_room_id: this.currentEntity.ly_room_id});
  }
}
