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',
  templateUrl: './face-check.component.html',
  styleUrls: ['./face-check.component.scss']
})
export class FaceCheckComponent extends FramePageComponent implements OnInit {
  responseEntity : any;
  searchText: any;
  checkArray : any;
  actionArray : any;
  checkOptr : any;//审核员
  action : any;//操作类型
  currId = 0;
  aliPlayer: any;
  tpl: any;
  bsModalWin: any;
  firstLoad = true;
  
  constructor(private modalService: BsModalService) {
    super(modalService);
    this.nextStd();
  }

  ngOnInit() {
    this.checkArray = [{check_optr:"check1", check_name: "check1"},
                        {check_optr:"check2", check_name: "check2"}];
    this.actionArray = [{action_id:"是", action_name: "是"},
                        {action_id:"疑是", action_name: "疑是"},
                        {action_id:"作弊", action_name: "作弊"}];
  }

  nextStd() {
    debugger;
    this.responseEntity = undefined;
    let param = {};
    if (this.currId) {
      param = {id: this.currId, query: this.searchText};
    }
    AjaxService.requestEntity('std/face/check/next.htm', param, (entity) => {

      this.responseEntity = entity;
      this.currId =  entity.id;
      
      setTimeout(() => {
        this.startVideo();
      }, 200);
      console.log(FrameService.getParamValue('EnrolMaterialWebPath') + entity.std_image);
    });
  }
 
  //审核提交
  udpateFaceCheck(op) {
    // if (!this.checkOptr) {
    //   MsgService.errorToastr('请选择审核员！');
    //   return;
    // }
    // if (!this.action) {
    //   MsgService.errorToastr('请选确认类型！');
    //   return;
    // }
    let action = "";
    if (op == 1) {//同一个人
      action = "同一个人"
    } else if (op == 2) {
      action = "不是同一个人"
    }
    let param = {
      id : this.responseEntity.id,
      check_val : action
    };

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

  searchStd() {
    this.responseEntity = undefined;
    AjaxService.requestEntity('std/face/check/next.htm', {query: this.searchText}, (entity) => {
      debugger;
      this.responseEntity = entity;
    });
  }

  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) {
    debugger;
    if (video_file && video_file.length > 0) {
      this.tpl = tpl;
      this.nextGroupVideo(true, video_file);
    } else {
      MsgService.errorToastr('当前考生无视频！');
    }
  }*/

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

  initVideoPlayer(url?) {
    debugger;
    let _this = this;
    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("已切换下一张照片!");
      }
    });
  }
}
