@extends('layouts.app')

@section('title', 'Magasins')
@section('page-title', 'Magasins')

@section('content')
<script>
    window.__magasinsData = @json($magasins);
    window.magasinManager = function(magasins) {
        return {
            showModal: false
            , showDeleteModal: false
            , deleteIndex: null
            , deleteLabel: ''
            , editingIndex: null
            , magasins: magasins || []
            , form: {
                nom: ''
                , adresse: ''
                , telephone: ''
            }
            , openModal() {
                this.editingIndex = null;
                this.form = {
                    nom: ''
                    , adresse: ''
                    , telephone: ''
                };
                this.showModal = true;
            }
            , editMagasin(i) {
                this.editingIndex = i;
                this.form = {
                    ...this.magasins[i]
                };
                this.showModal = true;
            }
            , openDeleteModal(i) {
                this.deleteIndex = i;
                this.deleteLabel = this.magasins[i].nom;
                this.showDeleteModal = true;
            }
            , confirmDelete() {
                if (this.deleteIndex === null) return;
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '{{ url('
                magasins ') }}/' + this.magasins[this.deleteIndex].id;
                const token = document.querySelector('meta[name="csrf-token"]') ? .content || '';
                form.innerHTML = '<input type="hidden" name="_token" value="' + token + '"><input type="hidden" name="_method" value="DELETE">';
                document.body.appendChild(form);
                form.submit();
            }
        };
    };

</script>
<div x-data="magasinManager(__magasinsData)">
    <x-page-card title="Liste des magasins" action="openModal()" actionLabel="Nouveau magasin">
        <div class="overflow-x-auto -mx-4 sm:mx-0">
            <table class="min-w-full divide-y divide-slate-200">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Reférence</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Nom</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Adresse</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">Téléphone</th>
                        <th class="px-4 py-3 text-right text-xs font-semibold text-slate-500 uppercase tracking-wider w-24">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <template x-for="(magasin, i) in magasins" :key="magasin.id">
                        <tr class="hover:bg-slate-50/50 transition-colors">
                            <td class="px-4 py-3 font-medium text-slate-800" x-text="magasin.reference"></td>
                            <td class="px-4 py-3 font-medium text-slate-800" x-text="magasin.nom"></td>
                            <td class="px-4 py-3 text-slate-600" x-text="magasin.adresse || '-'"></td>
                            <td class="px-4 py-3 text-slate-600" x-text="magasin.telephone || '-'"></td>
                            <td class="px-4 py-3">
                                <x-action-buttons :editAction="'editMagasin(i)'" :deleteAction="'openDeleteModal(i)'" />
                            </td>
                        </tr>
                    </template>
                    <tr x-show="magasins.length === 0">
                        <td colspan="4" class="px-4 py-8 text-center text-slate-500">Aucun magasin.</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </x-page-card>

    {{-- Modal création / modification --}}
    <div x-show="showModal" x-cloak x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100" x-transition:leave="transition ease-in duration-150" x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0" class="fixed inset-0 z-50 overflow-y-auto">
        <div class="flex min-h-screen items-center justify-center p-4">
            <div x-show="showModal" @click="showModal = false" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm" x-transition.opacity></div>
            <div x-show="showModal" x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" @click.stop class="relative bg-white rounded-2xl shadow-2xl max-w-lg w-full overflow-hidden">
                <div class="px-6 py-5 border-b border-slate-100">
                    <h3 class="text-lg font-semibold text-slate-800" x-text="editingIndex === null ? 'Nouveau magasin' : 'Modifier le magasin'"></h3>
                </div>
                <form :action="editingIndex !== null ? '{{ url('magasins') }}/' + magasins[editingIndex].id : '{{ route('magasins.store') }}'" method="POST" class="p-6 space-y-5">
                    @csrf
                    <template x-if="editingIndex !== null">
                        <input type="hidden" name="_method" value="PUT">
                    </template>
                    @if($errors->any())
                    <div class="p-3 rounded-lg bg-red-50 text-red-700 text-sm">
                        @foreach($errors->all() as $error)
                        <p>{{ $error }}</p>
                        @endforeach
                    </div>
                    @endif
                    <x-form-group label="Nom" :required="true">
                        <input type="text" name="nom" x-model="form.nom" required class="form-input" placeholder="Ex: Magasin Central">
                    </x-form-group>
                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                        <x-form-group label="Adresse">
                            <input type="text" name="adresse" x-model="form.adresse" class="form-input" placeholder="Ex: 123 Avenue principale">
                        </x-form-group>
                        <x-form-group label="Téléphone">
                            <input type="tel" name="telephone" x-model="form.telephone" class="form-input" placeholder="+225 07 00 00 00 00">
                        </x-form-group>
                    </div>
                    <div class="flex gap-3 pt-2">
                        <button type="submit" class="form-btn-primary flex-1">Enregistrer</button>
                        <button type="button" @click="showModal = false" class="form-btn-secondary">Annuler</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    {{-- Modal suppression --}}
    <x-delete-modal>
        Êtes-vous sûr de vouloir supprimer « <span x-text="deleteLabel"></span> » ? Cette action est irréversible.
    </x-delete-modal>
</div>
@endsection
