/*
 * @Author: your name
 * @Date: 2021-12-03 11:10:14
 * @LastEditTime: 2021-12-28 17:24:54
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \tms-obc-web\src\app\routes\supply-management\components\qrcode-page\qrcode-page.component.ts
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { QRComponent } from '@delon/abc/qr';
import { SFSchema, SFUISchema } from '@delon/form';
import { NzModalRef } from 'ng-zorro-antd/modal';


@Component({
  selector: 'app-supply-management-qrcode-page',
  templateUrl: './qrcode-page.component.html',
})
export class SupplyManagementQrcodePageComponent implements OnInit {
  @ViewChild('qr') qr!: QRComponent;
  record: any = {};
  i: any;
  schema: SFSchema = {};
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $no: {
      widget: 'text'
    },
    $href: {
      widget: 'string',
    },
    $description: {
      widget: 'textarea',
      grid: { span: 24 },
    },
  };

  constructor(
    private modal: NzModalRef,
  ) { }

  ngOnInit(): void {
    console.log(this.i);
  }


  downLoadQrcode(downloadName: any): void {
    let aLink = document.createElement('a');
    const content = this.qr.dataURL;
    let blob = this.base64ToBlob(content); //new Blob([content]);
    let evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", true, true);//initEvent 不加后两个参数在IE下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
    aLink.download = downloadName;
    aLink.href = URL.createObjectURL(blob);
    // aLink.dispatchEvent(evt);
    aLink.click()

  }

  //base64转blob
  base64ToBlob(code: any) {
    let parts = code.split(';base64,');
    let contentType = parts[0].split(':')[1];
    let raw = window.atob(parts[1]);
    let rawLength = raw.length;
    let uInt8Array = new Uint8Array(rawLength);
    for (let i = 0; i < rawLength; ++i) {
      uInt8Array[i] = raw.charCodeAt(i);
    }
    return new Blob([uInt8Array], { type: contentType });
  }

  close(): void {
    this.modal.destroy();
  }
}
