<template>
    <div class="flex items-center justify-between">
        <h1 class="text-3xl font-bold uppercase">Liste des fournisseurs</h1>
        <NuxtLink
            to="/admin/supplier"
            class="flex items-center justify-center text-xl uppercase bg-primary-500 text-white h-[50px] w-[272px]"
            :class="auth.isAdmin ? '' : 'cursor-not-allowed opacity-60'"
            @click="handleAddClick"
        >
            Ajouter
        </NuxtLink>
    </div>

    <div v-if="auth.isAdmin" class="mt-6 border border-slate-200 mb-16">
        <div class="max-h-96 overflow-y-auto">
            <div
                class="sticky top-0 z-10 grid grid-cols-7 gap-4 bg-slate-100 px-4 py-3 text-sm font-semibold uppercase tracking-wide"
            >
                <div>Nom</div>
                <div>Mail</div>
                <div>Rue</div>
                <div>Complément</div>
                <div>Code Postal</div>
                <div>Ville</div>
                <div>Pays</div>
            </div>

            <div v-if="supplierList.length === 0" class="px-4 py-6 text-slate-400">
                Aucun fournisseur.
            </div>

            <div v-for="supplier in supplierList" :key="supplier.id">
                <div
                    v-if="!supplier.addresses || supplier.addresses.length === 0"
                    class="grid grid-cols-7 border-t gap-4 px-4 py-2 hover:bg-slate-50 cursor-pointer"
                    @click="goToSupplier(supplier.id)"
                >
                    <div class="truncate">{{ supplier.name }}</div>
                    <div class="truncate">{{ supplier.email }}</div>
                    <div class="col-span-1">Pas d'adresse</div>
                    <div class="uppercase truncate">{{"—"}}</div>
                    <div class="uppercase truncate">{{"—"}}</div>
                    <div class="uppercase truncate">{{"—"}}</div>
                    <div class="uppercase truncate">{{"—"}}</div>
                </div>

                <template v-else-if="supplier.addresses.length > 0">
                    <div
                        v-for="(address, idx) in supplier.addresses"
                        :key="address.id ?? `${supplier.id}-${idx}-${address.street}-${address.postalCode}`"
                        class="grid grid-cols-7 hover:bg-slate-50 border-t gap-4 px-4 py-2 cursor-pointer"
                        :class="idx > 0 ? 'pl-4 border-l-4 border-l-slate-200 bg-slate-50' : ''"
                        @click="goToSupplier(supplier.id)"
                    >
                        <div class="truncate">
                            {{ idx === 0 ? supplier.name : "↳" }}
                        </div>
                        <div class="truncate">{{ idx === 0 ? supplier.email : ""  }}</div>
                        <div class="truncate">{{ address.street || "—" }}</div>
                        <div class="truncate">{{ address.street2 || "—" }}</div>
                        <div>{{ address.postalCode || "—"  }}</div>
                        <div class="uppercase truncate">{{ address.city || "—"  }}</div>
                        <div class="uppercase truncate">{{ address.countryCode || "—" }}</div>
                    </div>
                </template>

                <template v-else>
                    <div
                        class="grid grid-cols-7 hover:bg-slate-50 border-t gap-4 px-4 py-2 cursor-pointer"
                        @click="goToSupplier(supplier.id)"
                    >
                        <div class="truncate">{{ supplier.name }}</div>
                        <div class="truncate">{{ supplier.email }}</div>
                        <div class="col-span-5 text-slate-400">
                            Adresses non chargées
                        </div>
                    </div>
                </template>
            </div>
        </div>
    </div>
    <div v-else class="mt-6 border border-slate-200 mb-16 px-4 py-6 text-slate-400">
        Accès réservé aux administrateurs.
    </div>
</template>

<script setup lang="ts">
import { getSupplierList } from "~/services/supplier"
import type { SupplierData } from "~/services/dto/supplier-data"
import { useAuthStore } from "~/stores/auth"

definePageMeta({ layout: "admin" })

const supplierList = ref<SupplierData[]>([])
const router = useRouter()
const auth = useAuthStore()

const goToSupplier = (id: number) => {
    if (!auth.isAdmin) return
    router.push(`/admin/supplier/${id}`)
}

const handleAddClick = (event: Event) => {
    if (auth.isAdmin) return
    event.preventDefault()
}

onMounted(async () => {
    if (!auth.isAdmin) return
    supplierList.value = await getSupplierList()
})
</script>
