import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FrameAjax } from 'src/app/core/frame/ajax/frame-ajax/frame-ajax';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { StdRegDateModifyComponent } from '../std-reg-date-modify/std-reg-date-modify.component';

declare var aliPlayerUtil :any;

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

  std_id: any;
  std: any;
  materialArr = [];
  picArr = []; //作品
  videoModal: any;
  bsModalWin: any;
  aliPlayer: any;
  searchText: any;
  @ViewChild('examAjax', {static: false}) examAjax: FrameAjax;
  @ViewChild('scoreAjax', {static: false}) scoreAjax: FrameAjax;

  constructor(private activateInfo: ActivatedRoute, private router: Router, private modalService: BsModalService) {
    super(modalService);
    this.activateInfo.paramMap.subscribe((params: ParamMap) => {
      this.std_id = params.get('std_id');
      this.queryStd();
    });
    setTimeout(() => {
      if (this.std_id === undefined) {
        this.router.navigate(['/art/material/reg/page']);
        return;
      }
    }, 100);
  }

  ngOnInit(): void {
  }
  
  //查询考生
  queryStd() {
    this.std = {};
    this.AjaxService.requestEntity('std/reg/find.htm', {std_id: this.std_id}, (entity) => {
      this.std = entity;
    });
  }

  //搜索考生
  searchStd() {
    if(!this.searchText || this.searchText.length < 2) {
      return;
    }
    this.AjaxService.requestPage('std/reg/page.htm', {query: this.searchText, start: 0, limit : 2}, (page) => {
      if(page.records.length == 0) {
        this.MsgService.errorToastr('未找到符合条件的考生');
        return;
      }
      if(page.records.length > 1) {
        this.MsgService.errorToastr('有多个符合条件的考生，请从考生列表中查询，在去选择指定的考生！');
        return;
      }
      this.std_id = page.records[0].std_id;
      this.queryStd();
      this.requestAll();
    }, {RequestWithLoading: true});
  }

  requestAll() {
    this.examAjax.reload({std_id: this.std_id});
    this.requestStdMaterial();
    this.scoreAjax.reload({std_id: this.std_id});
  }

  //返回到考生列表
  goStd() {
    this.router.navigate(['/art/material/reg/page']);
  }

  //加载考生材料
  requestStdMaterial() {
   this.picArr = [];
   this.AjaxService.requestArray('std/reg/material/list.htm', {std_id: this.std_id}, (array) => {
      this.materialArr = array;
      for(const r of this.materialArr) {
        if(r.picArr && r.picArr.length > 0)  this.picArr.push(r);
      }
   });
  }
  //播放视频
  startVideo(videoModal, video_file) {
    this.videoModal = videoModal;
    this.nextVideo(true, video_file);
  }

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

  //视频初始化
  initVideoPlayer(url?) {
    this.bsModalWin = this.modalService.show(this.videoModal, { class: 'modal-9p' , backdrop: 'static', keyboard: false});
    this.aliPlayer = aliPlayerUtil.init('aliPlayer', url);
  }

  //隐藏视频窗口
  hideModal(){
    if(this.bsModalWin) this.bsModalWin.hide();
  }

  //视频暂停播放
  pauseVideo(){
    aliPlayerUtil.pause(this.aliPlayer);
  }

  //视频继续播放
  playVideo(){
    aliPlayerUtil.play(this.aliPlayer);
  }

  //调整考试时间
  modifyInterview(g) {
    this.onModalClose = () => {
      this.examAjax.reload({std_id: this.std_id});
    }
    this.showComponentModal(StdRegDateModifyComponent, {ResponseEntity: g});
  }

  
}
