<script setup>
import SupervisorLayout from '@/Layouts/SupervisorLayout.vue';
import { Head, Link, router } from '@inertiajs/vue3';
import { reactive, watch } from 'vue';

const props = defineProps({
    washes: {
        type: Object,
        required: true,
    },
    filters: {
        type: Object,
        default: () => ({}),
    },
    statusOptions: {
        type: Object,
        default: () => ({}),
    },
});

const localFilters = reactive({
    status: props.filters.status ?? '',
    date: props.filters.date ?? '',
});

watch(localFilters, () => {
    router.get(route('supervisor.washes.index'), localFilters, {
        preserveState: true,
        replace: true,
    });
});

const formatDate = (iso) => {
    if (!iso) return '—';
    return new Intl.DateTimeFormat('fr-FR', {
        dateStyle: 'short',
        timeStyle: 'short',
    }).format(new Date(iso));
};

const formatCurrency = (value) =>
    new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(value ?? 0);

const statusColor = (status) => {
    const colors = {
        pending_start: 'bg-gray-100 text-gray-800',
        running: 'bg-blue-100 text-blue-800',
        completed: 'bg-green-100 text-green-800',
        failed: 'bg-red-100 text-red-800',
        cancelled: 'bg-yellow-100 text-yellow-800',
    };
    return colors[status] ?? 'bg-gray-100 text-gray-800';
};
</script>

<template>
    <Head title="Lavages" />

    <SupervisorLayout title="Lavages">
        <div class="mb-6 flex flex-wrap gap-4 rounded-lg bg-white p-4 shadow">
            <div>
                <label class="block text-xs font-medium text-gray-500">Statut</label>
                <select
                    v-model="localFilters.status"
                    class="mt-1 block rounded-md border-gray-300 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                >
                    <option value="">Tous</option>
                    <option v-for="(label, value) in statusOptions" :key="value" :value="value">
                        {{ label }}
                    </option>
                </select>
            </div>
            <div>
                <label class="block text-xs font-medium text-gray-500">Date</label>
                <input
                    v-model="localFilters.date"
                    type="date"
                    class="mt-1 block rounded-md border-gray-300 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500"
                />
            </div>
        </div>

        <div class="overflow-hidden rounded-lg bg-white shadow">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Début</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Utilisateur</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Machine</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Statut</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Durée</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500">Coût</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <tr v-if="washes.data.length === 0">
                        <td colspan="6" class="px-4 py-8 text-center text-sm text-gray-500">
                            Aucun lavage trouvé.
                        </td>
                    </tr>
                    <tr v-for="wash in washes.data" :key="wash.uuid" class="hover:bg-gray-50">
                        <td class="whitespace-nowrap px-4 py-3 text-sm text-gray-800">
                            {{ formatDate(wash.started_at) }}
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <div class="font-medium text-gray-800">{{ wash.user?.name ?? '—' }}</div>
                            <div class="text-xs text-gray-500">{{ wash.user?.email }}</div>
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <Link
                                v-if="wash.machine"
                                :href="route('supervisor.machines.show', wash.machine.uuid)"
                                class="text-indigo-600 hover:text-indigo-800"
                            >
                                {{ wash.machine.name }}
                            </Link>
                            <span v-else>—</span>
                        </td>
                        <td class="px-4 py-3">
                            <span
                                class="rounded-full px-2.5 py-0.5 text-xs font-medium"
                                :class="statusColor(wash.status)"
                            >
                                {{ wash.status_label }}
                            </span>
                        </td>
                        <td class="whitespace-nowrap px-4 py-3 text-sm text-gray-600">
                            {{ wash.duration_minutes ? `${wash.duration_minutes} min` : '—' }}
                        </td>
                        <td class="whitespace-nowrap px-4 py-3 text-sm font-medium text-gray-800">
                            {{ formatCurrency(wash.cost) }}
                        </td>
                    </tr>
                </tbody>
            </table>

            <div v-if="washes.links.length > 3" class="flex items-center justify-between border-t px-4 py-3">
                <p class="text-sm text-gray-600">
                    {{ washes.from ?? 0 }}–{{ washes.to ?? 0 }} sur {{ washes.total }}
                </p>
                <div class="flex gap-1">
                    <Link
                        v-for="link in washes.links"
                        :key="link.label"
                        :href="link.url"
                        v-html="link.label"
                        class="rounded px-3 py-1 text-sm"
                        :class="
                            link.active
                                ? 'bg-indigo-600 text-white'
                                : link.url
                                  ? 'text-gray-600 hover:bg-gray-100'
                                  : 'cursor-not-allowed text-gray-300'
                        "
                        preserve-state
                    />
                </div>
            </div>
        </div>
    </SupervisorLayout>
</template>
