<script setup>
const profileStore = useProfileStore()

const workingtimes = ref([])
const setupPage = async () => {
    const profiles = profileStore.profiles || []
    const checks = await Promise.all(profiles.map(async (profile) => {
        try {
            const spans = await useNuxtApp().$api(`/api/staff/time/spans?targetUserId=${profile.user_id || profile.id}`)
            const openSpan = (spans || []).find((s) => !s.endedAt && s.type === "work")
            if (openSpan) return { profile: profile.id }
        } catch (e) {
            return null
        }
        return null
    }))
    workingtimes.value = checks.filter(Boolean)
}

setupPage()
</script>

<template>
  <div v-if="workingtimes.length > 0">
    <p v-for="time in workingtimes"><UIcon name="i-heroicons-check"/> {{profileStore.getProfileById(time.profile).fullName}}</p>
  </div>
  <div v-else>
    <p>Keine Mitarbeiter anwesend</p>
  </div>
</template>

<style scoped>

</style>
