import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from "../../../../app.component";
import { FrameUploaderComponent } from '../../../../core/uploader/uploader.component';
import { ByFilesPdfViewerModal } from '../.././by/by-files-pdf-viewer/by-files-pdf-viewer-modal';


@Component({
  selector: 'app-by-files',
  templateUrl: './by-files.component.html',
  styleUrls: ['./by-files.component.scss']
})
export class ByFilesComponent  extends FlexTableComponent implements OnInit {

  public query = {
    diaplma_no: "",
    std_name: "",
    cert_no: "",
    ticket_no: "",
  };

  public impQuery = {
    exam_time_id: ""
  };

  

  QUERY_URL = "by/byFiles/page.htm";
  ADD_URL = "by/byFiles/add.htm";
  DELETE_URL= "by/byFiles/delete.htm";
  GET_URL= "by/byFiles/get.htm";
  EDIT_URL= "by/byFiles/edit.htm";
  UPLOAD_URL="by/byFiles/uploadByFiles.htm";
  BATCH_UPLOAD_URL="by/byFiles/batchUpload.htm";

  @ViewChild('diaplmano') diaplmano: ElementRef;
  @ViewChild('uploader') uploader: FrameUploaderComponent;
  @ViewChild('byFilesPdfModal') byFilesPdfModal: ByFilesPdfViewerModal;
  @ViewChild('batchUploader') batchUploader:FrameUploaderComponent;
  

  byFiles:any;

  upload_img_passList: any;
  upload_img_failList: any;
  uploadImgsWin: any;
  preUploadImgsWin: any;


  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      diaplma_no: [null, Validators.required],
      std_name: [null, Validators.required],
      std_sex: [null],
      ticket_no: [null],
      cert_no: [null],
      zy: [null],
      zylx: [null],
      bysj: [null],
      zkyx: [null]
    });
   }

  ngOnInit() {
  }

  deleteByFiles() {

      let checkedArray = this.framePager.getCheckedArray();
      if(checkedArray.length === 0) {
        AppComponent.alert({title:"信息提示",text:"请选择数据!"})
        return;
      }
     
      AppComponent.confirm("确定删除吗？",()=>{
        let checkedArray = this.framePager.getCheckedArray((item)=>item.id);
        
        FrameRequest.requestMap("by/byFiles/delete.htm", {ids:checkedArray.join()}, () => {
          AppComponent.GobalComponent.successMsg('数据删除成功!');
          this.framePager.reload();
        });
      });
    
  }

  

   //关闭导入框，留在详情界面
   hideImpWin() {
    if(this.bsModalWin) 
      this.bsModalWin.hide();
      this.editShowType = "";
  }

  preShouModalWin(importModal){
    
    let checkedArray = this.framePager.getCheckedArray();
    if(checkedArray.length != 1) {
      AppComponent.alert({title:"信息提示",text:"请选择一条数据!"})
      return;
    }

    this.byFiles = checkedArray[0];
    this.shouModalWin(importModal,'modal-sm')
  }

  preshouBatchModalWin(preBatchUploadModal){
    this.preUploadImgsWin = this.bsModalService.show(preBatchUploadModal, { class: "modal-lg" });
  }


  uploadByFiles() {
    if (!this.byFiles.diaplma_no) {
      AppComponent.alert({title: "信息提示", text: "请选择毕业证号!"});
      return;
    }
    let param = {
      id: this.byFiles.id
    };
    this.uploader.choooseFileAndUpload(param, ((ret) => {
     if (ret.errorCode == 0) {
        this.hideModal();
        this.framePager.reload();
        this.editShowType = "";
        AppComponent.alert({title:"信息提示", text:"电子档案上传成功!"});
      }else{
        AppComponent.alert({title:"信息提示", text: ret.errorMsg});
      }
    }));
  }


  downByFile(item){

    FrameRequest.requestEntity("by/byFiles/byFilesIsExists.htm", {id: item.id}, (entity) => {
   
      if (!entity) {
        FrameRequest.download("by/byFiles/download.htm",{id:item.id,diaplma_no:item.diaplma_no});
      }else{
        AppComponent.alert({title:"信息提示", text: entity});
      }

    });

  }

  batchByFiles(){
    
      if(!this.impQuery.exam_time_id) {
        AppComponent.alert({title:"信息提示",text:"请选择批次!"})
        return;
      }
     
      AppComponent.confirm("确定将该批次审核通过的毕业信息归档到档案表吗？",()=>{
       
        FrameRequest.requestMap("by/byFiles/batchByFiles.htm", {exam_time_id:this.impQuery.exam_time_id}, () => {
          AppComponent.GobalComponent.successMsg('档案归档成功!');
          this.framePager.reload();
          this.hideImpWin();
        });
      });
  }

  preShow(){
    let checkedArray = this.framePager.getCheckedArray((item)=>item.id);
    if(checkedArray.length != 1) {
      AppComponent.alert({title:"信息提示",text:"请选择一条数据!"})
      return;
    }

    
    FrameRequest.requestEntity("by/byFiles/byFilesIsExists.htm", {id: checkedArray.join(),is_preView:true}, (entity) => {
      if (!entity) {
        this.byFilesPdfModal.show();
      }else{
        AppComponent.alert({title:"信息提示",text:entity});
      }
    });

    
  }

   //批量上传电子档案
   batchUpload(tplModal){
    this.batchUploader.choooseFileAndUpload({}, ((ajax) => {
     
      if(ajax.errorCode == '0'){
        this.upload_img_passList = ajax.map.passList;
        this.upload_img_failList = ajax.map.failList;
        this.preUploadImgsWin.hide();
        this.uploadImgsWin = this.bsModalService.show(tplModal, { class: "modal-lg" });
      }else{
        AppComponent.alert({title:"信息提示",text:ajax.errorMsg});
      }
     
      }).bind(this));
    }

    
 
}
