<template>
  <div>

    <div class="flex items-center gap-1">
      <UButton @click="router.push(`/inventoryitems/create/`)">+ Inventarartikel</UButton>

      <UInput
          v-model="searchString"
          placeholder="Suche..."
      />
    </div>



    <UTable
      :rows="filteredRows"
      @select="selectItem"
      :columns="itemColumns"
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Noch keine Einträge' }"
    >


    </UTable>


  </div>
</template>

<script setup>

definePageMeta({
  middleware: "auth"
})
const dataStore = useDataStore()
const supabase = useSupabaseClient()
const router = useRouter()

const itemColumns = [
  {
    key: "name",
    label: "Name",
    sortable: true
  },
  {
    key: "description",
    label: "Beschreibung",
    sortable: true
  }
]

const selectItem = (item) => {
  console.log(item)
  router.push(`/inventoryitems/show/${item.id} `)
}

const searchString = ref('')

const filteredRows = computed(() => {
  if(!searchString.value) {
    return dataStore.inventoryitems
  }

  return dataStore.inventoryitems.filter(product => {
    return Object.values(product).some((value) => {
      return String(value).toLowerCase().includes(searchString.value.toLowerCase())
    })
  })
})


</script>

<style scoped>

</style>