<template>
  <EntityList
      type="projects"
      :items="items"
      :filters="filters"
  ></EntityList>
</template>

<script setup>
import EntityList from "~/components/EntityList.vue";

definePageMeta({
  middleware: "auth"
})



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

const items = ref([])

const setupPage = async () => {
  let profiles = (await useSupabaseSelect("profiles"))
  items.value = (await useSupabaseSelect("projects","*, customer (name), plant(name), projecttype(name, id)","projectNumber")).map(project => {
    return {
      ...project,
      profiles: project.profiles.map(x => profiles.find(z => z.id === x).fullName).join(", "),
      phase: getActivePhaseLabel(project)
    }
  })
}

const getActivePhaseLabel = (item) => {
  if(item.phases) {
    if(item.phases.length > 0) {

      let activePhase = item.phases.find(i => i.active)

      if(activePhase) {
        return activePhase.label
      } else {
        return  ""
      }
    }
  }
}

setupPage()

</script>