<template>
  <UDashboardNavbar title="Aufgaben" :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()"
      >
        <template #trailing>
          <UKbd value="/" />
        </template>
      </UInput>

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

  <UDashboardToolbar>
    <template #left>


      <UCheckbox
          label="Erledigte Anzeigen"
          v-model="showDone"
      />
    </template>

    <template #right>
      <USelectMenu
          v-model="selectedColumns"
          icon="i-heroicons-adjustments-horizontal-solid"
          :options="templateColumns"
          multiple
          class="hidden lg:block"
          by="key"
      >
        <template #label>
          Spalten
        </template>
      </USelectMenu>
    </template>
  </UDashboardToolbar>

  <UTable
      :rows="filteredRows"
      :columns="columns"
      class="w-full"
      :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
      @select="(i) => router.push(`/tasks/show/${i.id}`) "
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Aufgaben anzuzeigen' }"
  >
    <template #finish-data="{row}">
      <UButton
          icon="i-heroicons-check"
          variant="ghost"
          @click="markAsFinished(row)"
      />
    </template>
    <template #created_at-data="{row}">
      {{row.created_at ? dayjs(row.created_at).format("DD.MM.YY HH:mm") : ''}}
    </template>
    <template #user-data="{row}">
      {{dataStore.profiles.find(i => i.id === row.user) ? dataStore.profiles.find(i => i.id === row.user).fullName : ""}}
    </template>
    <template #project-data="{row}">
      {{dataStore.projects.find(i => i.id === row.project) ? dataStore.projects.find(i => i.id === row.project).name : ""}}
    </template>
    <template #customer-data="{row}">
      {{dataStore.customers.find(customer => customer.id === row.customer) ? dataStore.customers.find(customer => customer.id === row.customer).name : "" }}
    </template>
    <template #plant-data="{row}">
      {{dataStore.getPlantById(row.plant) ? dataStore.getPlantById(row.plant).name : "" }}
    </template>
  </UTable>
</template>

<script setup>
import  dayjs from "dayjs";

definePageMeta({
  middleware: "auth"
})

const dataStore = useDataStore()
const router = useRouter()

defineShortcuts({
  '/': () => {
    //console.log(searchinput)
    //searchinput.value.focus()
    document.getElementById("searchinput").focus()
  },
  '+': () => {
    router.push("/tasks/create")
  }
})

const templateColumns = [

  /*{
    key:"finish"
  },*/{
    key: "created_at",
    label: "Erstellt am:",
    sortable: true
  },{
    key: "name",
    label: "Name:",
    sortable: true
  },{
    key: "categorie",
    label: "Kategorie:",
    sortable: true
  },{
    key: "description",
    label: "Beschreibung:",
    sortable: true
  },{
    key: "user",
    label: "Benutzer:",
    sortable: true
  },{
    key: "project",
    label: "Projekt:",
    sortable: true
  },{
    key: "plant",
    label: "Objekt:",
    sortable: true
  }
]
const selectedColumns = ref(templateColumns)
const columns = computed(() => templateColumns.filter((column) => selectedColumns.value.includes(column)))

const markAsFinished = (item) => {
  dataStore.updateItem("tasks", {...item, categorie: "Erledigt"})
}

const searchString = ref('')
const showDone = ref(false)
const filteredRows = computed(() => {
  let items = dataStore.tasks

  items = items.filter(i => showDone.value === true ? i.categorie === "Erledigt" : i.categorie !== "Erledigt")

  return useSearch(searchString.value, items)
})
</script>

<style scoped>

</style>