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 {FrameFunction} from '../../../../shared/frame/frame_function';
import {AppComponent} from '../../../../app.component';

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

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

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

  item:any;
  no:any;
  flag:any;

  @ViewChild('diaplmano') diaplmano: ElementRef;
  @ViewChild('saveBtn') saveBtn: ElementRef;
  

  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() {
    this.item = {};
    this.doFocus();
  }

  doFocus(){
    this.diaplmano.nativeElement.focus();
  }

  doFocusBtn(e){
    let keycode = window.event ? e.keyCode : e.which;
 
    if(keycode == 13){//回车键
      this.flag=0;
      this.saveBtn.nativeElement.focus();
    }
    
  }


  queryByByDiaplmano(e){
    
    let keycode = window.event ? e.keyCode : e.which;
 
    if(keycode == 13){//回车键
      if(this.item && this.item.diaplma_no){
        FrameRequest.requestEntity("by/byFiles/queryByByDiaplmano.htm", {diaplma_no: this.item.diaplma_no}, (entity) => {
          
          if(entity && entity.diaplma_no){
            
            this.item = entity;
            
            this.saveBtn.nativeElement.focus();
          }
          
        }); 
      }
     
    }
    
  }


  doSave(e){
    if(this.flag==0){
      let keycode = window.event ? e.keyCode : e.which;
 
      if(keycode == 13){//回车键
        this.saveByFlies();
      }
   
      if(keycode == 27){//esc键
        this.cancel();
      }
    }
    
  }

  cancel(){
    this.item = {};
    this.doFocus();
  }

  async saveByFlies() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    let url = this.item.id ? this.EDIT_URL: this.ADD_URL;
    if(!await this.preSaveEvent()) return;
    
    //循环判断元素是否undefined
    for (const key in this.item) {
      if(this.item[key] === undefined) {
        this.item[key] = "";
      }
    }
    FrameRequest.requestEntity(url, this.item, async () => {
      AppComponent.GobalComponent.successMsg('信息保存成功');
      this.aftSaveEvent();
      
    });
  }

  
  async aftSaveEvent(){
    this.no = this.item.diaplma_no;
    this.item = {};
    this.doFocus();

    this.browserPrint();

  }

  browserPrint(){
  
    if(this.no){
      FrameRequest.requestEntity("by/byFiles/qrCodeEncode.htm", {diaplma_no: this.no}, (entity) => {
          
        if(entity){
          //let html = '<img src="https://file.gxeea.cn:8811/remote/file/gxzk/default/21030313342101103518544609449985.jpg">';
         

         var src = URL.createObjectURL(this.basetoBlob(entity));
         let html = '<img src='+src+' style="padding-top:0px;padding-left:0px;margin-top:0px;margin-left:0px;">';

          const WindowPrt = window.open('', '', 'left=50%,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
          WindowPrt.document.write(html);
          WindowPrt.document.close();
          setTimeout( function() { 
            
            WindowPrt.focus();
            WindowPrt.print();
            WindowPrt.close();
          }, 500);
          
        }
        
      }); 
    }
    
  }

   convertDataURIToBinary(base64data) {   //编码转换
    var raw = window.atob(base64data);//这个方法在ie内核下无法正常解析。
    var rawLength = raw.length;
    var array = new Uint8Array(new ArrayBuffer(rawLength));
    for (let i = 0; i < rawLength; i++) {
        array[i] = raw.charCodeAt(i) & 0xff;
    }
    return array;
  }

  basetoBlob(base64data){
    let u8arr = this.convertDataURIToBinary(base64data);
    let mine = "application/pdf";//固定为pdf格式
    return new Blob([u8arr], {type: mine});
  }
  

}
