<script setup>

import dayjs from "dayjs";

const profileStore = useProfileStore();

let unpaidInvoicesSum = ref(0)
let unpaidInvoicesCount = ref(0)
let unpaidOverdueInvoicesSum = ref(0)
let unpaidOverdueInvoicesCount = ref(0)
let draftInvoicesSum = ref(0)
let draftInvoicesCount = ref(0)

let countPreparedOpenIncomingInvoices = ref(0)

const setupPage = async () => {
  let items = (await useEntities("createddocuments").select("*, statementallocations(*), customer(id,name), linkedDocument(*)")).filter(i => !i.archived)
  let documents = items.filter(i => i.type === "invoices" ||i.type === "advanceInvoices")

  let draftDocuments = documents.filter(i => i.state === "Entwurf")
  let finalizedDocuments = documents.filter(i => i.state === "Gebucht")

  finalizedDocuments = finalizedDocuments.filter(i => i.statementallocations.reduce((n,{amount}) => n + amount, 0).toFixed(2) !== useSum().getCreatedDocumentSum(i, documents).toFixed(2))

  finalizedDocuments = finalizedDocuments.filter(x => (x.type === 'invoices' || x.type === 'advanceInvoices') && x.state === 'Gebucht' && !items.find(i => i.linkedDocument && i.linkedDocument.id === x.id))



  finalizedDocuments.forEach(i => {
    if(dayjs().subtract(i.paymentDays,"days").isAfter(i.documentDate)) {
      unpaidOverdueInvoicesSum.value += useSum().getCreatedDocumentSum(i, documents) - i.statementallocations.reduce((n,{amount}) => n + amount, 0)
      unpaidOverdueInvoicesCount.value += 1
    } else {
      unpaidInvoicesSum.value += useSum().getCreatedDocumentSum(i, items) - i.statementallocations.reduce((n,{amount}) => n + amount, 0)
      unpaidInvoicesCount.value += 1
    }
  })
  //unpaidInvoicesCount.value = finalizedDocuments.length

  draftDocuments.forEach(i => {
    draftInvoicesSum.value +=  useSum().getCreatedDocumentSum(i, documents) - i.statementallocations.reduce((n,{amount}) => n + amount, 0)
  })
  draftInvoicesCount.value = draftDocuments.length

  countPreparedOpenIncomingInvoices.value = (await useEntities("incominginvoices").select("id, state")).filter(i => i.state === "Vorbereitet").length


}

setupPage()

</script>

<template>
  <table>
    <tr>
      <td class="break-all">Offene Rechnungen:</td>
      <td
          v-if="unpaidInvoicesSum > 0"
          class="text-orange-500 font-bold text-nowrap"
      >{{unpaidInvoicesCount}} Stk /<br> {{useCurrency(unpaidInvoicesSum)}}</td>
      <td v-else class="text-primary-500 font-bold text-no-wrap">0 Stk / 0,00€</td>
    </tr>
    <tr>
      <td class="break-all">Überfällige Rechnungen:</td>
      <td
          v-if="unpaidOverdueInvoicesSum !== 0"
          class="text-rose-600 font-bold text-nowrap"
      >{{unpaidOverdueInvoicesCount}} Stk /<br> {{useCurrency(unpaidOverdueInvoicesSum)}}</td>
      <td v-else class="text-primary-500 font-bold text-no-wrap">0 Stk / 0,00€</td>
    </tr>
    <tr>
      <td class="break-all">Angelegte Rechnungsentwürfe:</td>
      <td
          v-if="draftInvoicesSum > 0"
          class="text-orange-500 font-bold text-nowrap"
      >{{draftInvoicesCount}} Stk /<br> {{useCurrency(draftInvoicesSum)}}</td>
      <td v-else class="text-primary-500 font-bold text-no-wrap">0 Stk / 0,00€</td>
    </tr>
    <tr>
      <td class="break-all">ToDo Eingangsrechnungsrechnungen:</td>
      <td
          v-if="countPreparedOpenIncomingInvoices > 0"
          class="text-orange-500 font-bold text-nowrap"
      >{{countPreparedOpenIncomingInvoices}} Stk </td>
      <td v-else class="text-primary-500 font-bold text-no-wrap">0 Stk</td>
    </tr>
  </table>

</template>

<style scoped>

</style>