<template>
    <div class="flex items-center justify-between">
        <div class="flex items-center gap-10">
            <Icon @click="router.push('/')" name="gg:arrow-left-o" size="44" class="cursor-pointer text-primary-500"/>
            <h1 class="text-3xl font-bold uppercase text-primary-500">listes des expéditions en attente</h1>
        </div>
    </div>

    <div class="px-[86px]">
        <div class="mt-6 border border-slate-200 mb-16 ">
            <div class="grid grid-cols-5 gap-4 bg-slate-100 px-4 py-3 text-sm font-semibold uppercase tracking-wide">
                <div>Client</div>
                <div>Adresse</div>
                <div>Type d'expéditions</div>
                <div>Transporteur</div>
                <div>Immatriculation</div>
            </div>
            <div
                v-for="shipment in shipmentList"
                :key="shipment.id"
                class="grid grid-cols-5 gap-4 px-4 py-3 text-sm hover:bg-slate-50 cursor-pointer border-t border-slate-200"
                role="button"
                tabindex="0"
                @click="goToShipment(shipment.id)"
                @keydown.enter="goToShipment(shipment.id)"
            >
                <div>{{ shipment.customer?.name }}</div>
                <div>{{ shipment.address?.fullAddress }}</div>
                <div>
                    <template v-if="formatShipmentLines(shipment).length">
                        <div
                            v-for="(line, index) in formatShipmentLines(shipment)"
                            :key="index"
                            class="leading-5"
                        >
                            {{ line }}
                        </div>
                    </template>
                </div>
                <div>{{ shipment.carrier?.name }}</div>
                <div>{{ shipment.licensePlate }}</div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">

import type {ShipmentData} from "~/services/dto/shipment-data";
import {getShipmentList} from "~/services/shipment";

const shipmentList = ref<ShipmentData[]>()
const router = useRouter()

const goToShipment = (id: number) => {
    router.push(`/shipment/${id}`)
}

const formatShipmentLines = (shipment: ShipmentData) => {
    if (!shipment.shipmentType && shipment.nbBovinSend == null) {
        return []
    }

    const label = typeof shipment.shipmentType === 'string'
        ? shipment.shipmentType
        : shipment.shipmentType?.label

    return [`${label ?? '—'} : ${shipment.nbBovinSend ?? '—'}`]
}

onMounted(async () => {
    shipmentList.value = await getShipmentList(false)
})
</script>
