<template>
  <UDashboardNavbar title="Lieferanten" :badge="filteredRows.length">
    <template #right>
      <UInput
          id="searchinput"
          name="searchinput"
          v-model="searchString"
          icon="i-heroicons-funnel"
          autocomplete="off"
          placeholder="Suche..."
          class="hidden lg:block"
          @keydown.esc="$event.target.blur()"
      >
        <template #trailing>
          <UKbd value="/" />
        </template>
      </UInput>

      <UButton @click="router.push(`/vendors/create/`)">+ Lieferant</UButton>

    </template>
  </UDashboardNavbar>

  <UDashboardToolbar>
    <template #right>
      <USelectMenu
          v-model="selectedColumns"
          icon="i-heroicons-adjustments-horizontal-solid"
          :options="templateColumns"
          multiple
          class="hidden lg:block"
          by="key"
      >
        <template #label>
          Spalten
        </template>
      </USelectMenu>
    </template>
  </UDashboardToolbar>

  <UTable
      :rows="filteredRows"
      :columns="columns"
      :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
      class="w-full"
      @select="(i) => router.push(`/vendors/show/${i.id}`)"
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Lieferanten anzuzeigen' }"
  >
    <template #name-data="{row}">
      <span v-if="row === filteredRows[selectedItem]" class="text-primary-500 font-bold">{{row.name}}</span>
      <span v-else>{{row.name}}</span>
    </template>
    <template #address-data="{row}">
      {{row.infoData.street ? `${row.infoData.street}, ` : ''}}{{row.infoData.special ? `${row.infoData.special},` : ''}} {{(row.infoData.zip || row.infoData.city) ? `${row.infoData.zip} ${row.infoData.city}, ` : ''}} {{row.infoData.country}}
    </template>
  </UTable>

</template>

<script setup>
definePageMeta({
  middleware: "auth"
})

defineShortcuts({
  '/': () => {
    //console.log(searchinput)
    //searchinput.value.focus()
    document.getElementById("searchinput").focus()
  },
  '+': () => {
    router.push("/vendors/create")
  },
  'Enter': {
    usingInput: true,
    handler: () => {
      router.push(`/vendors/show/${filteredRows.value[selectedItem.value].id}`)
    }
  },
  'arrowdown': () => {
    selectedItem.value += 1
  },
  'arrowup': () => {
    selectedItem.value -= 1
  }
})

const dataStore = useDataStore()
const router = useRouter()
const mode = ref("show")

const items = ref([])
const selectedItem = ref(0)

const setupPage = async () => {
  items.value = await useSupabaseSelect("vendors","*","vendorNumber")
}

setupPage()


const templateColumns = [
  {
    key: 'vendorNumber',
    label: "Lieferantennr.",
    sortable: true
  },
  {
    key: "name",
    label: "Name",
    sortable: true
  },
  {
    key: "address",
    label: "Adresse",
    sortable: true
  }
]
const selectedColumns = ref(templateColumns)
const columns = computed(() => templateColumns.filter((column) => selectedColumns.value.includes(column)))

const searchString = ref('')

const filteredRows = computed(() => {
  return useSearch(searchString.value, items.value)
})
</script>

<style scoped>

</style>