<template>
  <UDashboardPage>
    <UDashboardPanel grow>
      <UDashboardNavbar title="Aufgaben" :badge="filteredRows.length">
        <template #right>
          <UInput
              ref="input"
              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>
<!--          <UButton @click="router.push(`/tasks/create`)">+ Aufgabe</UButton>

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

          <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"
          >
            <template #label>
              Spalten
            </template>
          </USelectMenu>
        </template>
      </UDashboardToolbar>

      <UTable
          v-model:sort="sort"
          :rows="filteredRows"
          :columns="columns"
          sort-mode="manual"
          class="w-full"
          :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
          @select="(i) => router.push(`/tasks/show/${i.id}`) "
      >
        <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>
    </UDashboardPanel>
  </UDashboardPage>
</template>

<script lang="ts" setup>
import  dayjs from "dayjs";

definePageMeta({
  middleware: "auth"
})
const input = ref<{ input: HTMLInputElement }>()

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


defineShortcuts({
  '/': () => {
    input.value?.input?.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 searchString = ref('')
const showDone = ref(false)


const filteredRows = computed(() => {
  let tasks = dataStore.tasks

  if(!showDone.value) {
    tasks = tasks.filter(i => i.categorie !== "Erledigt")
  }

  if(!searchString.value) {
    return tasks
  }

  return tasks.filter(item => {
    return Object.values(item).some((value) => {
      return String(value).toLowerCase().includes(searchString.value.toLowerCase())
    })
  })
})

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

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

<style scoped>

</style>