<script setup>
import { Head, Link } from '@inertiajs/vue3';
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';
import MetricCard from '@/Components/MetricCard.vue';
import StatusBadge from '@/Components/StatusBadge.vue';

const props = defineProps({
    metrics: Object,
    pending_deliveries: Object,
    overdue_orders: Object,
});

// Formatage monétaire en euros
const formatCurrency = (value) => {
    return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(value);
};
</script>

<template>
    <Head title="Tableau de bord" />

    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center justify-between">
                <h2 class="text-xl font-bold leading-tight text-slate-800 dark:text-slate-200">
                    Tableau de bord de l'Infrastructure
                </h2>
                <span class="text-xs text-slate-500 dark:text-slate-400 font-medium">
                    Aujourd'hui : {{ new Date().toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) }}
                </span>
            </div>
        </template>

        <div class="py-6">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-6">

                <!-- Alertes de retard critiques -->
                <div
                    v-if="overdue_orders.data && overdue_orders.data.length > 0"
                    class="bg-rose-50 border border-rose-200 dark:bg-rose-950/20 dark:border-rose-900 rounded-xl p-5 shadow-sm space-y-3"
                >
                    <div class="flex items-center text-rose-800 dark:text-rose-350">
                        <svg class="w-5 h-5 mr-2.5 shrink-0" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
                        </svg>
                        <h3 class="text-sm font-bold uppercase tracking-wider">
                            Alertes de livraison : {{ overdue_orders.data.length }} commande(s) en retard
                        </h3>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
                        <div
                            v-for="order in overdue_orders.data"
                            :key="order.id"
                            class="bg-white dark:bg-slate-900 border border-rose-200/60 dark:border-rose-900/40 rounded-lg p-3 hover:shadow-sm transition-shadow flex justify-between items-center"
                        >
                            <div class="min-w-0">
                                <Link
                                    :href="route('commandes.show', { commande: order.id })"
                                    class="text-xs font-bold text-rose-700 dark:text-rose-400 hover:underline block truncate"
                                >
                                    {{ order.number }} - {{ order.label }}
                                </Link>
                                 <span class="text-xxs text-slate-400 block mt-0.5">
                                     Date limite : {{ order.delivery_deadline_formatted }} (par {{ order.requested_by }} - Service : {{ order.prescriber || 'Non spécifié' }})
                                 </span>
                            </div>
                            <span class="text-xs font-bold text-rose-600 dark:text-rose-400 ml-2">
                                {{ formatCurrency(order.amount_ttc) }}
                            </span>
                        </div>
                    </div>
                </div>

                <!-- Grille de KPIs principaux -->
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
                    <!-- Montant Total Engagé -->
                    <MetricCard
                        title="Montant engagé HT"
                        :value="formatCurrency(metrics.total_engaged_ht)"
                        subtitle="Commandes validées + transmises"
                    >
                        <template #icon>
                            <svg class="h-6 w-6 text-sky-600 dark:text-sky-400" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 18.75a60.07 60.07 0 0 1 15.797 2.101c.727.198 1.453-.342 1.453-1.096V18.75M3.75 4.5v.75m-.75-3h1.5m-1.5 0v-1.5m0 1.5H1.5m1.5 0c-.828 0-1.5.672-1.5 1.5v12a1.5 1.5 0 0 0 1.5 1.5h15a1.5 1.5 0 0 0 1.5-1.5v-12c0-.828-.672-1.5-1.5-1.5h-15Z" />
                            </svg>
                        </template>
                    </MetricCard>

                    <!-- Montant Total TTC des commandes de l'année en cours -->
                    <MetricCard
                        title="Montant total TTC"
                        :value="formatCurrency(metrics.total_ttc)"
                        :subtitle="`Année en cours (${new Date().getFullYear()})`"
                    >
                        <template #icon>
                            <svg class="h-6 w-6 text-emerald-600 dark:text-emerald-400" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m-3-2.818.879.659c1.171.879 3.07.879 4.242 0 1.172-.879 1.172-2.303 0-3.182C13.536 12.219 12.768 12 12 12c-.725 0-1.45-.22-2.003-.659-1.106-.879-1.106-2.303 0-3.182s2.9-.879 4.006 0l.415.33M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
                            </svg>
                        </template>
                    </MetricCard>

                    <!-- Alertes retards -->
                    <MetricCard
                        title="Commandes en retard"
                        :value="metrics.overdue_count"
                        :subtitle="metrics.overdue_count > 0 ? 'Livraison souhaitée dépassée' : 'Aucun retard constaté'"
                        :class="{ 'border-rose-300 bg-rose-50/10 dark:border-rose-900/30': metrics.overdue_count > 0 }"
                    >
                        <template #icon>
                            <svg
                                class="h-6 w-6"
                                :class="metrics.overdue_count > 0 ? 'text-rose-600 dark:text-rose-455 animate-pulse' : 'text-slate-400'"
                                fill="none"
                                viewBox="0 0 24 24"
                                stroke-width="1.5"
                                stroke="currentColor"
                            >
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
                            </svg>
                        </template>
                    </MetricCard>

                    <!-- Total Commandes actives -->
                    <MetricCard
                        title="Commandes actives"
                        :value="metrics.counts.validated + metrics.counts.ordered + metrics.counts.draft"
                        subtitle="Brouillons, validées et en cours"
                    >
                        <template #icon>
                            <svg class="h-6 w-6 text-indigo-650 dark:text-indigo-400" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m2.25 0H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
                            </svg>
                        </template>
                    </MetricCard>
                </div>

                <!-- Grille d'état des pipelines par statut -->
                <div class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-xl p-5 shadow-sm">
                    <h3 class="text-sm font-bold text-slate-800 dark:text-slate-200 uppercase tracking-wider mb-4">
                        Pipeline du cycle de vie des demandes
                    </h3>
                    
                    <div class="grid grid-cols-2 sm:grid-cols-5 gap-4">
                        <!-- Brouillon -->
                        <Link
                            :href="route('commandes.index', { status: 'draft' })"
                            class="bg-slate-50 dark:bg-slate-950/40 border border-slate-100 dark:border-slate-850 rounded-xl p-4 text-center hover:scale-[1.02] hover:shadow-md transition-all duration-200 block hover:border-slate-350 dark:hover:border-slate-750 cursor-pointer group"
                        >
                            <span class="text-xs font-semibold text-slate-500 uppercase tracking-wider group-hover:text-slate-755 dark:group-hover:text-slate-350 transition-colors">Brouillons</span>
                            <div class="mt-2 text-2xl font-bold text-slate-800 dark:text-slate-200">{{ metrics.counts.draft }}</div>
                        </Link>

                        <!-- Validée -->
                        <Link
                            :href="route('commandes.index', { status: 'validated' })"
                            class="bg-sky-50/40 dark:bg-slate-950/40 border border-sky-100/50 dark:border-slate-850 rounded-xl p-4 text-center hover:scale-[1.02] hover:shadow-md transition-all duration-200 block hover:border-sky-300 dark:hover:border-sky-900 cursor-pointer group"
                        >
                            <span class="text-xs font-semibold text-sky-600 dark:text-sky-400 uppercase tracking-wider group-hover:text-sky-755 dark:group-hover:text-sky-300 transition-colors">Validées (BC)</span>
                            <div class="mt-2 text-2xl font-bold text-sky-700 dark:text-sky-300">{{ metrics.counts.validated }}</div>
                        </Link>

                        <!-- Commandée -->
                        <Link
                            :href="route('commandes.index', { status: 'ordered' })"
                            class="bg-amber-50/40 dark:bg-slate-950/40 border border-amber-100/50 dark:border-slate-850 rounded-xl p-4 text-center hover:scale-[1.02] hover:shadow-md transition-all duration-200 block hover:border-amber-300 dark:hover:border-amber-900 cursor-pointer group"
                        >
                            <span class="text-xs font-semibold text-amber-600 dark:text-amber-400 uppercase tracking-wider group-hover:text-amber-755 dark:group-hover:text-amber-300 transition-colors">Transmises</span>
                            <div class="mt-2 text-2xl font-bold text-amber-700 dark:text-amber-300">{{ metrics.counts.ordered }}</div>
                        </Link>

                        <!-- Livrée -->
                        <Link
                            :href="route('commandes.index', { status: 'delivered' })"
                            class="bg-emerald-50/40 dark:bg-slate-950/40 border border-emerald-100/50 dark:border-slate-850 rounded-xl p-4 text-center hover:scale-[1.02] hover:shadow-md transition-all duration-200 block hover:border-emerald-300 dark:hover:border-emerald-900 cursor-pointer group"
                        >
                            <span class="text-xs font-semibold text-emerald-600 dark:text-emerald-400 uppercase tracking-wider group-hover:text-emerald-755 dark:group-hover:text-emerald-300 transition-colors">Livrées</span>
                            <div class="mt-2 text-2xl font-bold text-emerald-700 dark:text-emerald-300">{{ metrics.counts.delivered }}</div>
                        </Link>

                        <!-- Clôturée -->
                        <Link
                            :href="route('commandes.index', { status: 'closed' })"
                            class="bg-purple-50/40 dark:bg-slate-950/40 border border-purple-100/50 dark:border-slate-850 rounded-xl p-4 text-center col-span-2 sm:col-span-1 hover:scale-[1.02] hover:shadow-md transition-all duration-200 block hover:border-purple-300 dark:hover:border-purple-900 cursor-pointer group"
                        >
                            <span class="text-xs font-semibold text-purple-600 dark:text-purple-400 uppercase tracking-wider group-hover:text-purple-755 dark:group-hover:text-purple-300 transition-colors">Clôturées</span>
                            <div class="mt-2 text-2xl font-bold text-purple-700 dark:text-purple-300">{{ metrics.counts.closed }}</div>
                        </Link>
                    </div>
                </div>

                <!-- Section principale : Tableau des commandes en attente de livraison -->
                <div class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-xl shadow-sm overflow-hidden">
                    <div class="px-6 py-5 border-b border-slate-100 dark:border-slate-850 flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                        <div>
                            <h3 class="text-base font-bold text-slate-800 dark:text-slate-200">
                                Commandes en attente de livraison
                            </h3>
                            <p class="text-xs text-slate-500 dark:text-slate-400 mt-0.5">
                                Commandes validées ou transmises au fournisseur en attente de réception physique ou logique.
                            </p>
                        </div>
                        <Link
                            :href="route('commandes.index', { status: ['validated', 'ordered'] })"
                            class="text-xs font-bold text-sky-600 hover:text-sky-500 dark:text-sky-455 dark:hover:text-sky-350 inline-flex items-center"
                        >
                            Voir toutes les commandes actives
                            <svg class="w-3.5 h-3.5 ml-1" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5" />
                            </svg>
                        </Link>
                    </div>

                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-slate-200 dark:divide-slate-800 text-left">
                            <thead class="bg-slate-50 dark:bg-slate-950">
                                <tr>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Numéro</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Libellé</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Fournisseur</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Demandeur / Service</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Date de livraison souhaitée</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 text-right">Montant TTC</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400">Statut</th>
                                    <th scope="col" class="px-6 py-4 text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 text-right">Action</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200 dark:divide-slate-850">
                                <tr
                                    v-for="order in pending_deliveries.data"
                                    :key="order.id"
                                    class="hover:bg-slate-50/50 dark:hover:bg-slate-900/40 transition-colors"
                                >
                                    <!-- Numéro -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-sky-600 dark:text-sky-400">
                                        <Link :href="route('commandes.show', { commande: order.id })" class="hover:underline">
                                            {{ order.number }}
                                        </Link>
                                    </td>

                                    <!-- Libellé -->
                                    <td class="px-6 py-4 text-sm text-slate-900 dark:text-slate-200 max-w-xs truncate font-semibold">
                                        {{ order.label }}
                                    </td>

                                    <!-- Fournisseur -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-900 dark:text-slate-350 font-medium">
                                        {{ order.supplier }}
                                    </td>

                                    <!-- Demandeur / Service -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-900 dark:text-slate-350">
                                        <div class="font-medium text-slate-800 dark:text-slate-200">{{ order.requested_by }}</div>
                                        <div class="text-xs text-slate-450 dark:text-slate-400 font-semibold">{{ order.prescriber }}</div>
                                    </td>

                                    <!-- Date -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-500 dark:text-slate-400">
                                        <div class="flex items-center gap-1.5">
                                            <span>{{ order.delivery_deadline_formatted }}</span>
                                            <!-- Retard -->
                                            <span
                                                v-if="order.is_overdue"
                                                class="inline-flex items-center px-1.5 py-0.5 rounded text-xxs font-bold bg-rose-100 text-rose-800 border border-rose-200 dark:bg-rose-950/40 dark:text-rose-300 dark:border-rose-900 animate-pulse"
                                            >
                                                RETARD
                                            </span>
                                        </div>
                                    </td>

                                    <!-- Montant -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-900 dark:text-slate-100 text-right font-bold">
                                        {{ formatCurrency(order.amount_ttc) }}
                                    </td>

                                    <!-- Statut -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        <StatusBadge :status="order.status" />
                                    </td>

                                    <!-- Action -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-right">
                                        <Link
                                            :href="route('commandes.show', { commande: order.id })"
                                            class="text-slate-600 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-200"
                                        >
                                            Consulter
                                        </Link>
                                    </td>
                                </tr>

                                <tr v-if="pending_deliveries.data.length === 0">
                                    <td colspan="8" class="px-6 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
                                        Aucune commande active en attente de livraison pour le moment.
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </AuthenticatedLayout>
</template>
