<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
      v-if="openTasks.length > 0"
    :data="openTasks"
      :columns="normalizeTableColumns([{key:'name',label:'Name'},{key:'categorie',label:'Kategorie'}])"
      :on-select="(i) => router.push(`/tasks/show/${i.id}`)"
  />
  <div v-else>
    <p  class="text-center font-bold">Keine offenen Aufgaben</p>
  </div>
</template>

<style scoped>

</style>
