<script setup>


defineShortcuts({
  '/': () => {
    //console.log(searchinput)
    //searchinput.value.focus()
    document.getElementById("searchinput").focus()
  },
  '+': () => {
    router.push("/projects/create")
  },
  'Enter': {
    usingInput: true,
    handler: () => {
      router.push(`/projecttypes/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 tempStore = useTempStore()

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


const setup = async () => {
  items.value = await useEntities("projecttypes").select()
}

setup()

const templateColumns = [
  {
    key: "name",
    label: "Name"
  },

]
const selectedColumns = ref(templateColumns)
const columns = computed(() => templateColumns.filter((column) => selectedColumns.value.includes(column)))

const searchString = ref(tempStore.searchStrings["projecttypes"] || '')

const filteredRows = computed(() => {
  return useListFilter(searchString.value, items.value)
})

</script>

<template>
  <UDashboardNavbar title="Projekttypen" :badge="filteredRows.length">
    <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()"
          @change="tempStore.modifySearchString('projecttypes',searchString)"
      >
        <template #trailing>
          <UKbd value="/"/>
        </template>
      </UInput>

      <UButton @click="router.push(`/projecttypes/create`)">+ Projekttyp</UButton>
    </template>
  </UDashboardNavbar>

  <UTable
      :data="filteredRows"
      :columns="normalizeTableColumns(columns)"
      class="w-full"
      :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
      :on-select="(i) => router.push(`/projecttypes/show/${i.id}`) "
  >
    <template #name-cell="{row}">
      <span class="text-primary-500 font-bold" v-if="row.original === filteredRows[selectedItem]">{{ row.original.name }}</span>
      <span v-else>{{ row.original.name }}</span>
    </template>
    <template #empty>
      <TableEmptyState label="Keine Projekttypen anzuzeigen" />
    </template>
  </UTable>
</template>

<style scoped>

</style>
