import {Component, OnInit} from '@angular/core';
import {StdRegModifyComponent} from '../../reg/std-reg-modify/std-reg-modify.component';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FrameService } from 'src/app/core/service/frame.service';
import { MsgService } from 'src/app/core/service/msg.service';
declare var aliPlayerUtil :any;

@Component({
  selector: 'app-face-check-zx',
  templateUrl: './face-check-zx.component.html',
  styleUrls: ['./face-check-zx.component.scss']
})
export class FaceCheckZXComponent extends FramePageComponent implements OnInit {
  responseEntity : any;
  searchText: any;
  checkArray : any;
  actionArray : any;
  checkOptr : any;//审核员
  action : any;//操作类型
  currId = 1;
  aliPlayer: any;
  tpl: any;
  bsModalWin: any;
  firstLoad = true;
  aspectArray = [];
  subjectArray = [];
  curAspectId = -1;
  curSubjectId = -1;
  
  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit() {
    AjaxService.requestArray('zx/std/aspect/list.htm', {}, (array) => {
      debugger;
      this.aspectArray = array;
      this.freshSubject();
      this.searchStd();
    });
  }

  //刷新科目
  freshSubject() {
    AjaxService.requestArray('conf/aspect/subject/list.htm', {aspect_id : this.curAspectId}, (array) => {
      this.subjectArray = [];
      this.subjectArray = array;
    });
  }

  nextStd() {
    debugger;
    let param = {id: this.currId, aspect_id : this.curAspectId, subject_id : this.curSubjectId, query: this.searchText};
    AjaxService.requestEntity('zx/std/face/check/next.htm', param, (entity) => {
      debugger;
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("审核完成!");
      } else {
        this.currId = entity.id;
      }
    });
  }
 
  //审核提交
  udpateFaceCheck(op) {
    let action = "";
    if (op == 1) {//同一个人
      action = "同一个人"
    } else if (op == 2) {
      action = "不是同一个人"
    }
    let param = {
      std_id : this.responseEntity.std_id,
      video_id : this.responseEntity.video_id,
      check_val : action
    };

    AjaxService.request('zx/std/face/check/update.htm', param, () => {
      this.action = "";
      MsgService.successToastr("审核成功!");
      this.nextStd();
    });
  }

  searchStd() {
    debugger;
    this.responseEntity = undefined;
    let param = {id: this.currId, aspect_id: this.curAspectId, subject_id: this.curSubjectId, query: this.searchText};
    AjaxService.requestEntity('zx/std/search.htm', param, (entity) => {
      debugger;
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("没有找到待审核的考生!");
      }
    });
  }

  /*startVideo() {
    debugger;
    if (this.responseEntity.video_file && this.responseEntity.video_file.length > 0) {
      this.nextGroupVideo(true, this.responseEntity.video_file);
    } else {
      MsgService.errorToastr('当前考生无视频！');
    }
  }*/

  startVideo(tpl, video_file, type) {
    debugger;
    if (video_file && video_file.length > 0) {
      this.tpl = tpl;
      this.nextGroupVideo(true, video_file);
    } else {
      if (type == "1") {
        MsgService.errorToastr('当前考生无考试视频！');
      } else if (type == "2") {
        MsgService.errorToastr('当前考生无监控视频！');
      }
      
    }
  }

  nextGroupVideo(firstLoad, video_file) {
    debugger;
    if (firstLoad) {
      this.initVideoPlayer();
    }
    aliPlayerUtil.changeSource(this.aliPlayer, video_file);
  }

  initVideoPlayer(url?) {
    debugger;
    let _this = this;
    this.bsModalWin = this.modalService.show(this.tpl, { class: 'modal-9p' , backdrop: 'static', keyboard: false});
    this.aliPlayer = aliPlayerUtil.init('aliPlayer', url);
    // 技术播放自动下一个
    this.aliPlayer.on("ended", function() {
      debugger;
    });
  }

  hideModal(){
    if(this.bsModalWin) this.bsModalWin.hide();
  }

  pauseVideo(){
    aliPlayerUtil.pause(this.aliPlayer);
  }

  playVideo(){
    aliPlayerUtil.play(this.aliPlayer);
  }

  //app上传照片随机切换
  reqNextPhoto() {
    AjaxService.requestMap('std/face/snapshot/change.htm', {cert_id: this.responseEntity.cert_id}, (map) => {
      debugger;
      if (map) {
        debugger;
        this.responseEntity.snapshot_file = map.snapshot_file;
        MsgService.successToastr("已切换下一张照片!");
      }
    });
  }
}
