import { Component, OnInit, OnDestroy, AfterViewInit } from "@angular/core";
import { Router } from "@angular/router";
import { Toast } from "ng-zorro-antd-mobile";
import { BarcodeScanner } from "@ionic-native/barcode-scanner/ngx";
@Component({
  selector: "app-qr-scan",
  templateUrl: "./qr-scan.component.html",
  styleUrls: ["./qr-scan.component.scss"]
})
export class QrScanComponent implements OnInit, OnDestroy {
  constructor(
    private router: Router,
    private toast: Toast,
    private barcodeScanner: BarcodeScanner
  ) {}
  ngOnInit(): void {
    this.barcodeScanner
      .scan()
      .then(result => {
        console.log("Barcode data", result);
        const id = result.text.match(/(\w+)$/g)[0];
        console.log(id);
        this.loadingToast(id);
      })
      .catch(err => {
        console.log("Error", err);
        this.showToast();
      });
  }
  showToast() {
    const toast = Toast.show("扫描出错", 1000);
  }
  loadingToast(id) {
    const toast = Toast.loading("Loading...", 1000, () => {
      this.router.navigate(["/admin/assets-used/checkPlanAdd", id]);
    });
  }
  ngOnDestroy() {
    Toast.hide();
  }
}
