<script setup>
import * as JSPM from 'jsprintmanager'

let printers = []


const doPrintZPL = () => {
  /*if(this.selected_printer === '' && !this.print2default) {
    alert("You must select a printer");
    return;
  }*/
  let cpj = new JSPM.ClientPrintJob();
  /*if ( this.print2default ) {
    cpj.clientPrinter = new JSPM.DefaultPrinter();
  } else {
    cpj.clientPrinter = new JSPM.InstalledPrinter(this.selected_printer);
  }*/

  cpj.clientPrinter = new JSPM.InstalledPrinter("ZebraZD230");
  let cmds =  "^XA";
  cmds += "^CF0,60";
  cmds += "^FO20,10^BY4^BC,200,Y,N,,U^FD0012345123451234512^FS";
  cmds += "^FO20,250^GB650,3,3^FS";
  cmds += "^CFA,30";
  cmds += "^FO20,300^FDFederspiel Technology UG^FS";
  cmds += "^XZ";


  cpj.printerCommands = cmds;
  cpj.sendToClient();
}



const getPrinters = () =>  {
  return new Promise((ok, err) => {
    let printers = [];
    if(JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) {
      JSPM.JSPrintManager.getPrinters().then(function (myPrinters) {
        printers = myPrinters;
        console.log(printers);
        ok(printers);
      }).catch((e)=>err(e));
    } else { console.warn("JSPM WS not open"); ok(printers); }
  });
}
const initJSPM = () => {
  JSPM.JSPrintManager.auto_reconnect = true
  JSPM.JSPrintManager.start();

  JSPM.JSPrintManager.WS.onStatusChanged = () => {
    console.log("Status Changed")
    getPrinters().then(p => printers = p)
  }
}
initJSPM()
</script>

<template>
{{printers}}


  <UButton @click="doPrintZPL">Print</UButton>

</template>

<style scoped>

</style>