

<template>
  <UDashboardNavbar
    title="Labels"
  >
    <template #right>
      <UInput
          id="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(`/settings/labels/create`)" disabled>+ Label</UButton>
    </template>
  </UDashboardNavbar>
<!--  <UDashboardToolbar>

  </UDashboardToolbar>-->

  <UTable
    :rows="items"
    :columns="columns"
    @select="(i) => router.push(`/settings/labels/show/${i.id}`)"
    class="w-full"
    :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
    :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Artikel 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>
  </UTable>
</template>

<script setup>

definePageMeta({
  middleware: "auth"
})

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

const router = useRouter()

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

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

setupPage()

const templateColumns = [{key: 'name',label:'Name'},{key: 'widthInch',label:'Breite in Zoll'},{key: 'heightInch',label:'Höhe in Zoll'}]
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>