import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { StdMaterialReviewDisciplineComponent } from '../std-material-review-discipline/std-material-review-discipline.component';

declare var aliPlayerUtil :any;

@Component({
  selector: 'app-std-material-review',
  templateUrl: './std-material-review.component.html',
  styleUrls: ['./std-material-review.component.scss']
})
export class StdMaterialReviewComponent extends FramePageComponent implements OnInit {

  submitFlag = false;
  subjectArr = [];
  subject_id: any;
  searchText: any;
  responseEntity: any;
  firstLoad = true;
  aliPlayer: any;
  aliOtherPlayer : any;
  bsModalWin: any;


  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,
    }
  };

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

  ngOnInit(): void {
    this.AjaxService.requestEntity('std/material/review/flag.htm', {}, (entity) => {
      this.submitFlag = entity;
      if(!this.submitFlag) {
        this.searchStd('first');
      }
    });
    this.getSubject();
  }

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

  //科目列表
  getSubject() {
    this.AjaxService.requestArray('std/material/review/subject/list.htm', {}, (array) => {
      this.subjectArr = array;
      if(this.subjectArr && this.subjectArr.length > 0) {
        this.subject_id = this.subjectArr[0].subject_id;
      }
    });
  }

  //同一考生和非同一考生
  reviewStd(flag?) {
    let remark = '';
    if(flag == 1) remark = '同一考生';
    if(flag == 2) remark = '非同一考生';
    this.responseEntity.check_remark = remark;
    if(flag == 1) {
      this.AjaxService.request('std/material/review.htm', this.responseEntity, () => {
        this.MsgService.successToastr("审核成功!");
        this.nextStd();
      });
    } else {
      this.MsgService.confirmAlert('确定不是同一个考生吗？', () => {
        this.AjaxService.request('std/material/review.htm', this.responseEntity, () => {
          this.MsgService.successToastr("审核成功!");
          this.nextStd();
        });
      });
    }
  }

  //下一个考生
  nextStd() {
    let param = {real_exam_seq: this.searchText, subject_id: this.subject_id};
    this.AjaxService.requestEntity('std/material/review/std/next.htm', param, (entity) => {
      this.responseEntity = entity;
      if (!entity) {
        this.MsgService.warningToastr("审核完成!");
      } else {
        if(this.firstLoad) {
          this.initVideoPlayer(entity.video.video_file_url);
          this.initOtherVideoPlayer(entity.video.video_file_related);
          this.firstLoad = false;
        } else {
          aliPlayerUtil.changeSource(this.aliPlayer, entity.video.video_file_url);
          aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video.video_file_related);
        }
      }
    });
  }

  //上一个和下啊一个考生
  nextLastStd(flag) {
    let param = {real_exam_seq: this.responseEntity.real_exam_seq, flag: 'Active', subject_id: this.subject_id };
    //下一个考生
    if(flag == 'Active') {
      param.real_exam_seq = this.responseEntity.real_exam_seq+1;
    } else { //上一个考生
      param.real_exam_seq = this.responseEntity.real_exam_seq-1;
      param.flag = 'InActive';
    }
    this.AjaxService.requestEntity('std/material/review/std/last.htm', param, (entity) => {
      this.responseEntity = entity;
      if(!entity) {
        if(flag == 'Active') this.MsgService.warningToastr("已无下一个考生!");
        if(flag == 'InActive') this.MsgService.warningToastr("已经是第一个考生!");
      } else {
        if(this.firstLoad) {
          this.initVideoPlayer(entity.video.video_file_url);
          this.initOtherVideoPlayer(entity.video.video_file_related);
          this.firstLoad = false;
        } else {
          aliPlayerUtil.changeSource(this.aliPlayer, entity.video.video_file_url);
          aliPlayerUtil.changeSource(this.aliOtherPlayer, entity.video.video_file_related);
        }
      }
    });
  }

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

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

  //违纪窗口
  openDispline() {
    this.responseEntity.subject_id = this.subject_id;
    this.showComponentModal(StdMaterialReviewDisciplineComponent, {
      ResponseEntity : this.responseEntity
    }, {class: 'modal-5p'});
  }



  initVideoPlayer(url?) {
    this.aliPlayer = aliPlayerUtil.init('aliPlayer', url);
  }

  initOtherVideoPlayer(url?) {
    this.aliOtherPlayer = aliPlayerUtil.init('aliOtherPlayer', url);
  }

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

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

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