import {Component, OnInit} from '@angular/core';
import {StdMaterialRefuseComponent} from '../std-material-refuse/std-material-refuse.component';
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 { MsgService } from '../../../../../core/service/msg.service';
import { FrameService } from '../../../../../core/service/frame.service';

@Component({
  selector: 'app-std-material-verify',
  templateUrl: './std-material-verify.component.html',
  styleUrls: ['./std-material-verify.component.scss']
})
export class StdMaterialVerifyComponent extends FramePageComponent implements OnInit {
  material_type;
  materialArray;
  refuseArray;
  responseMap;
  searchText: any;
  material_name: any;

  constructor(private modalService: BsModalService) {
    super(modalService);
    this.material_type = 'NoCrownPhoto';
    this.modalComponent = StdMaterialRefuseComponent;
    AjaxService.requestArray('std/material/all.htm', {}, (array) => {
      this.materialArray = array;
      AjaxService.requestArray('std/material/refuse/all.htm', {}, (vArray) => {
        this.refuseArray = vArray;
        this.randomStd();
      });
    });
  }

  ngOnInit() {
  }

  randomStd() {
    if (this.material_type === undefined || this.material_type === null || this.material_type === '') {
      return;
    }
    this.responseMap = undefined;
    AjaxService.requestMap('std/material/verify/random.htm', {material_type: this.material_type}, (map) => {
      const array = [];
      let s = {StdReg: map.StdReg, PayTotal: map.PayTotal, ReuploadTotal: map.ReuploadTotal, NoVerifyTotal: map.NoVerifyTotal
        , VerifyFailAndReupload: map.VerifyFailAndReupload
        , NoVerify: map.NoVerify};
      for (const m of map.MaterialArray) {
        if (m.material_id) {
          array.push(m);
        }
      }
      if (array.length > 0) {
        s['MaterialArray'] = array;
      }
      this.responseMap = s;
      if(FrameService.getParamValue('FilterByMaterialType') == 'Active') {
        this.filterByMaterialType();
      }
    });
  }

  //根据类型-过滤材料
  filterByMaterialType() {
    let materialArr = [];
    if(!this.material_type) return;
    for(const material of this.responseMap.MaterialArray) {
      if(material.material_type == this.material_type) {
        materialArr.push(material);
        break;
      }
    }
    this.responseMap.MaterialArray = materialArr;
  }




  refuseMaterial(m, refuse_msg) {
    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';
      if (this['material_type'] === m.material_type) {
        this.randomStd();
      }

    });
  }

  acceptMaterial(m) {
    AjaxService.request('std/material/accept.htm', {material_id: m.material_id}, () => {
      m.verify_status = 'Active';
      let b = true;
      for (const s of this.responseMap.MaterialArray) {
        if (s.verify_status !== 'Active') {
          b = false;
          break;
        }
      }
      if (b) {
        this.randomStd();
      }
    });
  }

  acceptAll() {
    let array = [];
    for (const s of this.responseMap.MaterialArray) {
      array.push(s.material_id);
    }
    AjaxService.request('std/material/accept/all.htm', {'material_ids': array}, (map) => {
      this.randomStd();
    });

  }
  modifyStd() {
    this.onModalClose = () => {
      AjaxService.requestMap('std/reg/info.htm', {std_id: this.responseMap.StdReg.std_id}, (map) => {
        this.responseMap.StdReg = map.StdReg;
      });
    };
    this.showComponentModal(StdRegModifyComponent, {ResponseEntity: this.responseMap.StdReg, 'arrayUrl': 'frame/region/province/list.htm'});
  }

  searchStd() {
    if (this.material_type === undefined || this.material_type === null || this.material_type === '') {
      return;
    }
    this.responseMap = undefined;
    AjaxService.requestMap('std/material/verify/random.htm', {material_type: this.material_type, query: this.searchText}, (map) => {
      const array = [];
      let s = {StdReg: map.StdReg, PayTotal: map.PayTotal, ReuploadTotal: map.ReuploadTotal, NoVerifyTotal: map.NoVerifyTotal
        , VerifyFailAndReupload: map.VerifyFailAndReupload
        , NoVerify: map.NoVerify};
      for (const m of map.MaterialArray) {
        if (m.material_id) {
          array.push(m);
        }
      }
      if (array.length > 0) {
        s['MaterialArray'] = array;
      }
      this.responseMap = s;
    });
  }

  //按照材料类型-全部审核通过
  acceptAllByType() {
    this.getMaterialName();
    MsgService.confirmAlert('确定要将【'+ this.material_name +"】所有考生的照片审核通过吗？一旦审核，数据将不可回退，请注意操作！", () => {
        AjaxService.request('std/material/accept/by/type.htm', {material_type : this.material_type}, () => {
          MsgService.successAlert(this.material_name+"所有考生已全部审核通过！");
          this.randomStd();
        }, {requestWithLoading : true});
    });
  }

  getMaterialName() {
    for(const mater of this.materialArray) {
      if(mater.material_type == this.material_type) {
        this.material_name = mater.material_name;
        break;
      }
    }
  }
 
}
