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

const props = defineProps({ articles: Object, categories: Array, filters: Object })
const filters = reactive({ ...props.filters })

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

function formatCurrency(v) {
    if (v == null) return '—'
    return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(v)
}
</script>

<template>
    <Head title="Articles" />
    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center justify-between">
                <h1 class="text-xl font-semibold text-gray-900">Articles</h1>
                <Link :href="route('articles.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">
            <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 désignation, référence..." 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.categorie_id" @change="applyFilters" class="rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none">
                    <option value="">Toutes catégories</option>
                    <option v-for="c in categories" :key="c.id" :value="c.id">{{ c.nom }}</option>
                </select>
            </div>

            <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">Désignation</th>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Référence</th>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Catégorie</th>
                            <th class="px-5 py-3 text-left text-xs font-medium text-gray-500 uppercase">Fournisseur</th>
                            <th class="px-5 py-3 text-right text-xs font-medium text-gray-500 uppercase">Prix HT</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="art in articles.data" :key="art.id" :class="['hover:bg-gray-50', !art.active ? 'opacity-60' : '']">
                            <td class="px-5 py-3 font-medium text-gray-900">{{ art.designation }}</td>
                            <td class="px-5 py-3 text-gray-500">{{ art.reference || '—' }}</td>
                            <td class="px-5 py-3 text-gray-500">{{ art.categorie?.nom || '—' }}</td>
                            <td class="px-5 py-3 text-gray-500">{{ art.fournisseur?.nom || '—' }}</td>
                            <td class="px-5 py-3 text-right text-gray-900">{{ formatCurrency(art.prix_unitaire_ht) }}</td>
                            <td class="px-5 py-3 text-center">
                                <Link :href="route('articles.edit', art.id)" class="text-gray-400 hover:text-indigo-600 transition-colors">
                                    <svg class="h-4 w-4 inline" 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>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div v-if="!articles.data.length" class="py-10 text-center text-gray-400">Aucun article.</div>
                <div class="border-t border-gray-100 px-5 py-3">
                    <Pagination :links="articles.links" :meta="articles.meta" />
                </div>
            </div>
        </div>
    </AuthenticatedLayout>
</template>
