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

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


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

</template>

<script setup>

definePageMeta({
  middleware: "auth"
})

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

const itemColumns = [
  {
    key: 'vendorNumber',
    label: "Lieferantennr.",
    sortable: true
  },
  {
    key: "name",
    label: "Name",
    sortable: true
  }
]


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


const searchString = ref('')

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

  return dataStore.vendors.filter(item => {
    return Object.values(item).some((value) => {
      return String(value).toLowerCase().includes(searchString.value.toLowerCase())
    })
  })
})
</script>

<style scoped>

</style>