import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from 'src/app/core/service/msg.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { FaceCheckZhongxDisciplineComponent } from '../face-check-zhongx-discipline/face-check-zhongx-discipline.component';
declare var aliPlayerUtil :any;

@Component({
  selector: 'app-face-check-zhongx',
  templateUrl: './face-check-zhongx.component.html',
  styleUrls: ['./face-check-zhongx.component.scss']
})
export class FaceCheckZhongxComponent  extends FramePageComponent implements OnInit {

  responseEntity : any;
  searchText: any;
  checkArray : any;
  actionArray : any;
  checkOptr : any;//审核员
  action : any;//操作类型
  currId = 0;
  aliPlayer: any;
  aliOtherPlayer : any;
  tpl: any;
  bsModalWin: any;
  firstLoad = true;
  aspectArray = [];
  subjectArray = [];
  curAspectId = -1;
  curSubjectId = -1;
  submitFlag = false;
  cert_url = "http://172.16.5.84/zxcert/";
  photo_url = "http://172.16.5.84/zxphoto/";
  // cert_url = "https://10048.hmsoft.cn/image/";
  // photo_url = "https://10048.hmsoft.cn/image/";

  viewerOptions: any = {
    navbar: false,
    toolbar: {
      zoomIn: 4,
      zoomOut: 4,
      oneToOne: 4,
      reset: 4,
      prev: false,
      play: {
        show: 4,
        size: 'small'
      },
      next: false,
      rotateLeft: 4,
      rotateRight: 4,
      flipHorizontal: 4,
      flipVertical: 4,
    }
  };
  subject_id: any;


  
  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit() {
    AjaxService.requestEntity('zhongx/std/face/check/submit/flag.htm', {}, (entity) => {
      if(entity && entity.optr_desc == 'Active') {
        this.submitFlag = true;
      }
      this.searchStd('first');
    });
    this.getSubject();
   }

  //刷新科目
  getSubject() {
    AjaxService.requestArray('zhongx/std/room/subject.htm', {}, (array) => {
      this.subjectArray = [];
      this.subjectArray = array;
      if(this.subjectArray && this.subjectArray.length>0) {
        this.subject_id = this.subjectArray[0].subject_id;
      }
    });
  }

  nextStd() {
    let param = {query: this.searchText, subject_id: this.subject_id};
    AjaxService.requestEntity('zhongx/std/face/check/next.htm', param, (entity) => {
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("审核完成!");
      } else {
        if(this.firstLoad) {
          this.initVideoPlayer(entity.video_file);
          this.initOtherVideoPlayer(entity.video_file_related);
          this.firstLoad = false;
        } else {
          aliPlayerUtil.changeSource(this.aliPlayer, entity.video_file);
          aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video_file_related);
        }
      }
    });
  }


  nextStdFy() {
    let param = {exam_seq: this.responseEntity.exam_seq+1, flag: 'Active', subject_id: this.subject_id };
    AjaxService.requestEntity('zhongx/std/face/check/last.htm', param , (entity) => {
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("已无下一个考生!");
      } else {
        if(this.firstLoad) {
          this.initVideoPlayer(entity.video_file);
          this.initOtherVideoPlayer(entity.video_file_related);
          this.firstLoad = false;
        } else {
          aliPlayerUtil.changeSource(this.aliPlayer, entity.video_file);
          aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video_file_related);
        }
      }
    });
  }


  lastStd() {
    AjaxService.requestEntity('zhongx/std/face/check/last.htm', {exam_seq: this.responseEntity.exam_seq-1, subject_id: this.subject_id }, (entity) => {
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("已经是第一个考生!");
      } else {
        if(this.firstLoad) {
          this.initVideoPlayer(entity.video_file);
          this.initOtherVideoPlayer(entity.video_file_related);
          this.firstLoad = false;
        } else {
          aliPlayerUtil.changeSource(this.aliPlayer, entity.video_file);
          aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video_file_related);
        }
      }
    });
  }
 
  //审核提交
  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,
      subject_id : this.subject_id
    };
    if(op == 2) {
      MsgService.confirmAlert('确定不是同一个考生吗？', () => {
        AjaxService.request('zx/std/face/check/update.htm', param, () => {
          this.action = "";
          MsgService.successToastr("审核成功!");
          this.nextStd();
        });
      });
    } else {
      AjaxService.request('zx/std/face/check/update.htm', param, () => {
        this.action = "";
        MsgService.successToastr("审核成功!");
        this.nextStd();
      });
    }

  }



  searchStd(first?) {
    this.responseEntity = undefined;
    let param = { query: this.searchText, flag : 'Active', subject_id: this.subject_id };
    if(first === 'first') {
      param.flag = 'InActive';
    } else {
      param.flag = 'Active';
    }
    AjaxService.requestEntity('zhongx/std/face/check/next.htm', param, (entity) => {
      this.responseEntity = entity;
      if (!entity) {
        MsgService.warningToastr("没有找到待审核的考生!");
        return ;
      }
      if(entity.last_flag == 'Active' && !this.submitFlag) {
        // MsgService.successAlert('已经完成所有考生的审核,请及时提交！');
        MsgService.successAlert('已经完成科目【'+ entity.subject_name +"】的所有考生的审核！" );
      }
      if(this.firstLoad) {
        this.initVideoPlayer(entity.video_file);
        this.initOtherVideoPlayer(entity.video_file_related);
        this.firstLoad = false;
      } else {
        aliPlayerUtil.changeSource(this.aliPlayer, entity.video_file);
        aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video_file_related);
      }
    
    });
  }




  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?) {
    let _this = this;
    this.aliPlayer = aliPlayerUtil.init('aliPlayer', url);
    // 技术播放自动下一个
    this.aliPlayer.on("ended", function() {
    });
  }

  initOtherVideoPlayer(url?) {
    let _this = this;
    this.aliOtherPlayer = aliPlayerUtil.init('aliOtherPlayer', url);
    // 技术播放自动下一个
    this.aliOtherPlayer.on("ended", function() {
    });
  }




  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("已切换下一张照片!");
      }
    });
  }

  //审核名单提交
  submitStd() {
    MsgService.confirmAlert('确认提交全部审核吗？提交后，不可在更改！', () => {
      AjaxService.request('zhongx/std/face/check/submit.htm', {}, () => {
        MsgService.successToastr('提交成功！');
        this.submitFlag = true;
      });
    });
  }

  openDispline() {
    this.responseEntity.subject_id = this.subject_id;
    this.showComponentModal(FaceCheckZhongxDisciplineComponent, {
      ResponseEntity : this.responseEntity
    }, {class: 'modal-5p'});
  }

  //改变科目-重新获取判定的考生数据
  changeSubject() {
    this.searchStd('first');
  }

}
