<template>
  <InputGroup>
    <UButton @click="router.push(`/projects/create/`)">+ Projekt</UButton>

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



  <UTable
      :rows="filteredRows"
      @select="selectItem"
      :columns="itemColumns"
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Noch keine Einträge' }"
  >
    <template #customer-data="{row}">
      {{dataStore.getCustomerById(row.customer) ? dataStore.getCustomerById(row.customer).name : ""}}
    </template>
    <template #plant-data="{row}">
      {{dataStore.getPlantById(row.plant) ? dataStore.getPlantById(row.plant).name : ""}}
    </template>
  </UTable>
</template>

<script setup>

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

const itemColumns = [
  {
    key: "measure",
    label: "Gewerk"
  },{
    key: "name",
    label: "Name"
  },
  {
    key: "notes",
    label: "Notizen",
    sortable: true
  },
  {
    key: "customer",
    label: "Kunde",
    sortable: true
  },
  {
    key: "plant",
    label: "Anlage",
    sortable: true
  }
]

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

const searchString = ref('')

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

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


</script>

<style scoped>

</style>