<script setup>

const openTasks = ref([])
const router = useRouter()
const auth = useAuthStore()

function isCompletedTask(task) {
  return ["Abgeschlossen", "Erledigt"].includes(String(task?.categorie || "").trim())
}

const setupPage = async () => {
  openTasks.value = (await useEntities("tasks").select()).filter((task) => {
    const assignee = task.userId || task.user_id || task.profile
    const currentUser = auth.user?.user_id || auth.user?.id
    return !task.archived && !isCompletedTask(task) && assignee === currentUser
  })
}

setupPage()

</script>

<template>
  <UTable
    :data="openTasks"
    :columns="normalizeTableColumns([{key:'name',label:'Name'},{key:'categorie',label:'Kategorie'}])"
    :on-select="(i) => router.push(`/tasks/show/${i.id}`)"
  >
    <template #empty>
      <TableEmptyState label="Keine offenen Aufgaben" />
    </template>
  </UTable>
</template>

<style scoped>

</style>
