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


let printers = ref([])
let scanners = ref([])


find().then(devices => {
  console.log(devices)
})


const doScanning = () =>  {

}

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 temp = [];
    if(JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) {
      JSPM.JSPrintManager.getPrinters().then(function (myPrinters) {
        temp = myPrinters;
        ok(temp);
      }).catch((e)=>err(e));
    } else { console.warn("JSPM WS not open"); ok(temp); }
  });
}
const getScanners = () =>  {
  return new Promise((ok, err) => {
    let scanners = [];
    if(JSPM.JSPrintManager.websocket_status === JSPM.WSStatus.Open) {
      JSPM.JSPrintManager.getScanners().then(function (myScanners) {
        printers = myScanners;
        console.log(scanners);
        ok(scanners);
      }).catch((e)=>err(e));
    } else { console.warn("JSPM WS not open"); ok(printers); }
  });
}*/
const initJSPM = async () => {
  JSPM.JSPrintManager.auto_reconnect = true
  await JSPM.JSPrintManager.start();

  JSPM.JSPrintManager.WS.onStatusChanged = async () => {
    console.log("Status Changed")

    if(JSPM.JSPrintManager.websocket_status === JSPM.WSStatus.Open){
      printers.value = await JSPM.JSPrintManager.getPrinters()
      scanners.value = await JSPM.JSPrintManager.getScanners()
    }


  }
}
initJSPM()





</script>

<template>
{{printers}}
  {{scanners}}


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

  <UButton @click="initJSPM">Init</UButton>

</template>

<style scoped>

</style>