<script setup>
import printJS from "print-js";
import { ref, watch } from "vue";
import { useAlert } from "../../composables/alert";
const props = defineProps({
  base64String: { type: String, required: false },
  fileName: { type: String, required: false },
  print: { type: Boolean, required: false, default: false },
  title: { type: String, required: false },
  disabled: { type: Boolean, required: false, default: false }
});
const emit = defineEmits(["update:print"]);
const isMobile = ref(false);
const alert = useAlert();
const openPdf = () => {
  if (/Android|Mobi|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Macintosh/i.test(navigator.userAgent)) {
    isMobile.value = true;
  } else {
    printJS({
      printable: props.base64String,
      type: "pdf",
      base64: true,
      onPrintDialogClose: endLoadPdf,
      onError: (error) => {
        alert?.addAlert({ message: error, alertType: "error" });
      }
    });
  }
};
const endLoadPdf = () => {
  emit("update:print", false);
  isMobile.value = false;
};
watch(() => props.print, () => {
  if (props.print) openPdf();
});
</script>

<template>
  <v-dialog
    v-model="isMobile"
    fullscreen
  >
    <PdfView
      :base64-string="props.base64String"
      :disabled="props.disabled"
      :title="props.title"
      :file-name="props.fileName"
      @close-dialog="endLoadPdf"
    />
  </v-dialog>
</template>
