<script setup>
import CopyCreatedDocumentModal from "~/components/copyCreatedDocumentModal.vue";



defineShortcuts({
  'backspace': () => {
    router.push("/createDocument")
  },
})

const modal = useModal()
const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const dataStore = useDataStore()

const itemInfo = ref({})
const linkedDocument =ref({})
const setupPage = async () => {
  if(route.params) {
    if(route.params.id) itemInfo.value = await useEntities("createddocuments").selectSingle(route.params.id,"*,files(*),linkedDocument(*), statementallocations(bs_id)")

    console.log(itemInfo.value)

    linkedDocument.value = await useFiles().selectDocument(itemInfo.value.files[0].id)
  }
}

setupPage()

const openEmail = () => {
  if(["invoices","advanceInvoices"].includes(itemInfo.value.type)){
    router.push(`/email/new?loadDocuments=["${linkedDocument.value.id}"]&bcc=${encodeURIComponent(auth.activeTenantData.standardEmailForInvoices || "")}`)
  } else {
    router.push(`/email/new?loadDocuments=["${linkedDocument.value.id}"]`)
  }
}

const openBankstatements = () => {
  if(itemInfo.value.statementallocations.length > 1) {
    navigateTo(`/banking/?filter=${JSON.stringify(itemInfo.value.statementallocations.map(i => i.bs_id))}`)
  } else {
    navigateTo(`/banking/statements/edit/${itemInfo.value.statementallocations[0].bs_id}`)
  }
}
</script>

<template>
  <UDashboardNavbar
      title="Erstelltes Dokument anzeigen"
  >

  </UDashboardNavbar>
  <UDashboardToolbar>
    <template #left>
      <UButton
          @click="router.push(`/createDocument/edit/${itemInfo.id}`)"
          v-if="itemInfo.state === 'Entwurf'"
      >
        Bearbeiten
      </UButton>
      <!--      <UButton
                :to="dataStore.documents.find(i => i.createdDocument === itemInfo.id) ? dataStore.documents.find(i => i.createdDocument === itemInfo.id).url : ''"
                target="_blank"
            >In neuen Tab anzeigen</UButton>-->
      <UButton
          icon="i-heroicons-arrow-right-end-on-rectangle"
          @click="modal.open(CopyCreatedDocumentModal, {
              id: itemInfo.id,
              type: itemInfo.type
          })"
          variant="outline"
      >
        Kopieren
      </UButton>
      <UButton
          @click="openEmail"
          icon="i-heroicons-envelope"
      >
        E-Mail
      </UButton>
      <UButton
          @click="router.push(`/createDocument/edit/?linkedDocument=${itemInfo.id}&loadMode=storno`)"
          variant="outline"
          color="rose"
          v-if="itemInfo.type === 'invoices' || itemInfo.type === 'advanceInvoices'"
      >
        Stornieren
      </UButton>
      <UButton
          v-if="itemInfo.project"
          @click="router.push(`/standardEntity/projects/show/${itemInfo.project}`)"
          icon="i-heroicons-link"
          variant="outline"
      >
        Projekt
      </UButton>
      <UButton
          v-if="itemInfo.customer"
          @click="router.push(`/standardEntity/customers/show/${itemInfo.customer}`)"
          icon="i-heroicons-link"
          variant="outline"
      >
        Kunde
      </UButton>
      <UButton
          v-if="itemInfo.linkedDocument"
          @click="router.push(`/standardEntity/createDocument/show/${itemInfo.linkedDocument}`)"
          icon="i-heroicons-link"
          variant="outline"
      >
        {{dataStore.documentTypesForCreation[itemInfo.linkedDocument.type].labelSingle}} - {{itemInfo.linkedDocument.documentNumber}}
      </UButton>
      <UButton
          v-if="itemInfo.statementallocations?.length > 0"
          @click="openBankstatements"
          icon="i-heroicons-link"
          variant="outline"
      >
        Bankbuchungen
      </UButton>

    </template>
  </UDashboardToolbar>
  <UDashboardPanelContent>
<!--    <object
        :data="linkedDocument.url"
        class="w-full previewDocumentMobile"
    />-->
    <PDFViewer v-if="linkedDocument.id" :file-id="linkedDocument.id" location="show_create_document" />




  </UDashboardPanelContent>
</template>

<style scoped>
.previewDocumentMobile {
  aspect-ratio: 1 / 1.414;

}
</style>