<script setup>
import dayjs from "dayjs"

const supabase = useSupabaseClient()
const dataStore = useDataStore()
const profileStore = useProfileStore()
const router = useRouter()


const items = ref([])

const setup = async () => {

  items.value = (await supabase.from("historyitems").select().like('text',`%@${profileStore.activeProfile.username}%`)/*.textSearch("text", `'@${profileStore.activeProfile.username}'`)*/.order("created_at")).data
}

const navigateToHistoryItem = (item) => {
  /*if(item.customer) {
    router.push(`/customers/show/${item.customer}`)
  } else if(item.vendor) {
    router.push(`/vendors/show/${item.vendor}`)
  } else if(item.project) {
    router.push(`/projects/show/${item.project}`)
  } else if(item.plant) {
    router.push(`/plants/show/${item.plant}`)
  } else if(item.incomingInvoice) {
    router.push(`/incomingInvoices/show/${item.incomingInvoice}`)
  }/!* else if(item.document) {
    router.push(`/documents/show/${item.document}`)
  }*!/ else if(item.contact) {
    router.push(`/contacts/show/${item.contact}`)
  } else if(item.inventoryitem) {
    router.push(`/inventoryitems/show/${item.inventoryitem}`)
  } else if(item.product) {
    router.push(`/products/show/${item.product}`)
  } else if(item.profile) {
    router.push(`/profiles/show/${item.profile}`)
  } else if(item.absenceRequest) {
    router.push(`/absenceRequests/show/${item.absenceRequest}`)
  } else if(item.event) {
    router.push(`/events/show/${item.event}`)
  } else if(item.task) {
    router.push(`/tasks/show/${item.task}`)
  } else if(item.vehicle) {
    router.push(`/vehicle/show/${item.vehicle}`)
  } else if(item.bankStatement) {
    router.push(`/bankStatements/show/${item.bankStatement}`)
  } else if(item.space) {
    router.push(`/spaces/show/${item.space}`)
  } else if(item.trackingtrip) {
    router.push(`/trackingtrips/show/${item.trackingtrip}`)
  }*/
  if(item.config && item.config.type !== "document") {
    router.push(`/${item.config.type}s/show/${item.config.id}`)
  }
}

setup()
</script>

<template>
  <UDashboardNavbar title="Erwähnungen in Logbüchern" :badge="items.length">

  </UDashboardNavbar>
  <UTable
    :rows="items"
    @select="navigateToHistoryItem"
    :columns="[{key:'created_at',label:'Datum'},{key:'config',label:'Typ'},{key:'text',label:'Text'}]"
    :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Erwähnungen anzuzeigen' }"
    class="w-full"
    :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
  >
    <template #config-data="{row}">
      <span v-if="row.config">{{dataStore.dataTypes[row.config.type + "s"].labelSingle}}</span>
    </template>
    <template #created_at-data="{row}">
      {{dayjs(row.created_at).format("HH:mm DD.MM.YYYY")}}
    </template>
  </UTable>
</template>

<style scoped>

</style>