import {Component, OnInit} from '@angular/core';
import {FramePageComponent} from '../../../../frame/core/FramePageComponent';
import {BsModalService} from 'ngx-bootstrap';
import {StdMaterialRefuseComponent} from '../std-material-refuse/std-material-refuse.component';
import {AjaxService} from '../../../../../core/ajax/ajax.service';
import {StdRegModifyComponent} from '../../reg/std-reg-modify/std-reg-modify.component';

@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;

  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};
      for (const m of map.MaterialArray) {
        if (m.material_id) {
          array.push(m);
        }
      }
      if (array.length > 0) {
        s['MaterialArray'] = array;
      }
      this.responseMap = s;
    });
  }


  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';
      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();
      }
    });
  }



  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/zone/province/list.htm'});
  }
}
