import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';

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

  materialArray = [];
  material_type: any;
  responseMap: any;
  total: any;
  pendArr = [];
  acceptStdIds = [];
  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);
    this.material_type = 'ArtExamCert';
    AjaxService.requestArray('std/material/all.htm', {}, (array) => {
      this.materialArray = array;
    });
   }

  ngOnInit(): void {
    this.responseMap = {};
    this.pendArr = [];
    this.acceptStdIds = [];
    this.randomStdBatch();
  }

  //打回重传
  refuseMaterial(m, refuse_msg) {
    this.MsgService.confirmAlert('确定要打回该考生吗？', () => {
      AjaxService.request('std/material/refuse.htm', {material_id: m.material_id, refuse_msg: refuse_msg, msg_flag: 'Active'}, () => {
        m.verify_status = 'InActive';
        m.reupload_status = 'InActive';
        this.refreshStdMaterial(m.material_id);
      });
    });
  }

  refreshStdMaterial(material_id) {
    AjaxService.requestEntity('std/material/find/by/id.htm',{material_id: material_id}, (entity) => {
      for(const pend of this.pendArr) {
        if(pend.material_id == entity.material_id) {
          pend.verify_time = entity.verify_time;
          pend.verify_status = entity.verify_status;
        }
      }
    });
  }

  //一键通过
  acceptAll() {
    this.MsgService.confirmAlert('确定要一键通过当前页面的考生吗？', () => {
      this.getAcceptStd();
      setTimeout(() => {
        AjaxService.request('std/material/batch/accept/all.htm', {material_type: this.material_type, accept_ids:this.acceptStdIds}, () => {
          this.MsgService.successToastr('审核成功！');
          this.randomStdBatch();
        }, {requestWithLoading: true});
      }, 300);
    });
  }


  getAcceptStd() {
    const arr = [];
    this.acceptStdIds = [];
    for(const pend of this.pendArr) {
      if(pend.verify_status !== 'InActive') {
        arr.push(pend.std_id);
      }
    }
    this.acceptStdIds = arr;
  }

  //下一批考生
  randomStdBatch() {
    this.pendArr = [];
    AjaxService.requestMap('std/material/verify/random/batch.htm', {material_type: this.material_type }, (map) => {
      this.responseMap = map;
      this.total = this.responseMap.NoVerify;
      this.pendArr = map.MaterialArray;
    });
  }

  randomStdBatchPage() {
    this.pager.reload({material_type: this.material_type});
  }
}
