<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue'
import Pagination from '@/Components/Pagination.vue'
import ConfirmModal from '@/Components/ConfirmModal.vue'
import { Head, Link, router, useForm } from '@inertiajs/vue3'
import { ref, reactive } from 'vue'

const props = defineProps({
    fournisseurs: Object,
    filters:      Object,
})

const filters = reactive({ ...props.filters })
const deleteTarget = ref(null)
const deleteForm = useForm({})

function applyFilters() {
    router.get(route('fournisseurs.index'), filters, { preserveState: true, replace: true })
}

function doDelete() {
    deleteForm.delete(route('fournisseurs.destroy', deleteTarget.value.id), {
        onSuccess: () => deleteTarget.value = null,
    })
}

function toggleActive(f) {
    router.patch(route('fournisseurs.toggle-active', f.id))
}
</script>

<template>
    <Head title="Fournisseurs" />
    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center justify-between">
                <h1 class="text-xl font-semibold text-gray-900">Fournisseurs</h1>
                <Link :href="route('fournisseurs.create')"
                    class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 transition-colors">
                    + Ajouter
                </Link>
            </div>
        </template>

        <div class="space-y-4">
            <!-- Filtres -->
            <div class="rounded-xl bg-white p-4 shadow-sm border border-gray-100 flex gap-3">
                <input v-model="filters.search" @input="applyFilters" type="text" placeholder="Recherche nom, ville, email..."
                    class="flex-1 rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" />
                <select v-model="filters.active" @change="applyFilters"
                    class="rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none">
                    <option value="">Tous</option>
                    <option value="1">Actifs</option>
                    <option value="0">Inactifs</option>
                </select>
            </div>

            <!-- Table -->
            <div class="rounded-xl bg-white shadow-sm border border-gray-100 overflow-hidden">
                <table class="min-w-full divide-y divide-gray-100 text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Nom</th>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Contact</th>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Ville</th>
                            <th class="px-5 py-3 text-right text-xs font-medium text-gray-500 uppercase">Commandes</th>
                            <th class="px-5 py-3 text-center text-xs font-medium text-gray-500 uppercase">Statut</th>
                            <th class="px-5 py-3 text-center text-xs font-medium text-gray-500 uppercase">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50">
                        <tr v-for="f in fournisseurs.data" :key="f.id" :class="['hover:bg-gray-50 transition-colors', !f.active ? 'opacity-60' : '']">
                            <td class="px-5 py-3">
                                <Link :href="route('fournisseurs.show', f.id)" class="font-medium text-blue-600 hover:underline">{{ f.nom }}</Link>
                                <p v-if="f.raison_sociale" class="text-xs text-gray-400">{{ f.raison_sociale }}</p>
                            </td>
                            <td class="px-5 py-3 text-gray-600">
                                <p v-if="f.email">{{ f.email }}</p>
                                <p v-if="f.telephone" class="text-xs text-gray-400">{{ f.telephone }}</p>
                            </td>
                            <td class="px-5 py-3 text-gray-600">{{ f.ville ?? '—' }}</td>
                            <td class="px-5 py-3 text-right font-medium text-gray-900">{{ f.commandes_count }}</td>
                            <td class="px-5 py-3 text-center">
                                <button @click="toggleActive(f)"
                                    :class="['rounded-full px-2.5 py-1 text-xs font-medium transition-colors', f.active ? 'bg-green-100 text-green-700 hover:bg-green-200' : 'bg-gray-100 text-gray-600 hover:bg-gray-200']">
                                    {{ f.active ? 'Actif' : 'Inactif' }}
                                </button>
                            </td>
                            <td class="px-5 py-3 text-center">
                                <div class="flex items-center justify-center gap-2">
                                    <Link :href="route('fournisseurs.show', f.id)" class="text-gray-400 hover:text-blue-600 transition-colors">
                                        <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                        </svg>
                                    </Link>
                                    <Link :href="route('fournisseurs.edit', f.id)" class="text-gray-400 hover:text-indigo-600 transition-colors">
                                        <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                                        </svg>
                                    </Link>
                                    <button @click="deleteTarget = f" class="text-gray-400 hover:text-red-600 transition-colors">
                                        <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                        </svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div v-if="!fournisseurs.data.length" class="py-12 text-center text-gray-400">Aucun fournisseur trouvé.</div>
                <div class="border-t border-gray-100 px-5 py-3">
                    <Pagination :links="fournisseurs.links" :meta="fournisseurs.meta" />
                </div>
            </div>
        </div>

        <ConfirmModal :show="!!deleteTarget" title="Supprimer le fournisseur"
            :message="`Supprimer définitivement ${deleteTarget?.nom} ?`"
            :processing="deleteForm.processing" @confirm="doDelete" @cancel="deleteTarget = null" />
    </AuthenticatedLayout>
</template>
