<template>
  <div id="main">
    <!-- TODO: Kontakte erstellen und dem Kunden zuweisen    -->

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

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



    <UTable
        :rows="filteredRows"
        :columns="customerColumns"
        @select="selectCustomer"

        :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Noch keine Einträge' }"
    />

  </div>
</template>

<script setup>

definePageMeta({
  middleware: "auth"
})


const router = useRouter()
const {customers } = storeToRefs(useDataStore())
const mode = ref("show")

const customerColumns = [
  {
    key: 'customerNumber',
    label: "Kundennr.",
    sortable: true
  },
  {
    key: "name",
    label: "Name",
    sortable: true
  }
]


const selectCustomer = (customer) => {
  console.log(customer)
  router.push(`/customers/show/${customer.id} `)
}


const searchString = ref('')

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

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

<style scoped>

</style>