@extends('layouts.app')

@section('title', 'Catégories')
@section('page-title', 'Catégories')

@section('content')
<div class="space-y-6">
    <!-- En-tête -->
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Catégories</h1>
            <p class="mt-1 text-sm text-gray-600">Gérez les catégories de produits</p>
        </div>
        <button type="button" data-modal-toggle="modal-new-category" class="inline-flex items-center px-5 py-2.5 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg shadow-sm hover:shadow-md transition-all">
            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
            </svg>
            Nouvelle catégorie
        </button>
    </div>

    <x-modal-form id="modal-new-category" title="Nouvelle catégorie">
        @include('components.modals.category', ['modalId' => 'modal-new-category'])
    </x-modal-form>

    <!-- Tableau des catégories -->
    <div class="bg-white shadow-sm border border-gray-200 rounded-xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Référence
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Nom
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Description
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Produits
                        </th>
                        <th class="px-6 py-4 text-right text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Actions
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($categories as $category)
                    <tr class="hover:bg-blue-50/50 transition-colors">
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="text-sm font-medium text-gray-700">
                                {{ $category->reference }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="flex items-center">
                                <div class="ml-4">
                                    <div class="text-sm font-medium text-gray-900">{{ $category->name }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="text-sm text-gray-700">{{ $category->description ?? '-' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                                {{ $category->products->count() }} produit(s)
                            </span>
                        </td>

                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <div class="flex items-center justify-end space-x-3">
                                <!-- Bouton Modifier -->
                                <div class="relative group">
                                    <button type="button" onclick="openEditModal('{{ $category->id }}', '{{ $category->name }}', '{{ $category->description }}')" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-blue-50 to-blue-100 text-blue-600 hover:from-blue-500 hover:to-blue-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md">
                                        <svg class="w-5 h-5" 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>
                                    </button>
                                    <div class="tooltip">Modifier</div>
                                </div>

                                <!-- Bouton Supprimer (uniquement pour Administrateur et Super Administrateur) -->
                                @if(Auth::user()->hasRole(['Administrateur', 'Super Administrateur']))
                                <div class="relative group">
                                    <form method="POST" action="{{ route('categories.destroy', $category->id) }}" class="delete-form inline-block" id="delete-form-{{ $category->id }}">
                                        @csrf
                                        @method('DELETE')
                                        <button type="button" onclick="openDeleteCategoryModal('{{ $category->id }}')" class="inline-flex items-center justify-center w-9 h-9 rounded-lg bg-gradient-to-br from-red-50 to-red-100 text-red-600 hover:from-red-500 hover:to-red-600 hover:text-white transform hover:scale-110 transition-all duration-200 shadow-sm hover:shadow-md">
                                            <svg class="w-5 h-5" 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>
                                    </form>
                                    <div class="tooltip">Supprimer</div>
                                </div>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr class="hover:bg-blue-50/50 transition-colors">
                        <td colspan="5" class="px-6 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-16 h-16 text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
                                </svg>
                                <p class="text-sm font-medium text-gray-600 mb-1">Aucune catégorie enregistrée</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        @if($categories->hasPages())
        <div class="px-6 py-4 border-t border-gray-200">
            {{ $categories->links() }}
        </div>
        @endif
    </div>
</div>

<!-- Modal d'édition de catégorie -->
<x-modal-form id="modal-edit-category" title="Modifier la catégorie">
    <form method="POST" id="edit-category-form" class="space-y-6">
        @csrf
        @method('PUT')
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Nom <span class="text-red-500">*</span></label>
            <input type="text" name="name" id="edit-name" class="w-full px-3 py-2.5 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" placeholder="Entrez le nom de la catégorie" required>
        </div>
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">Description</label>
            <textarea name="description" id="edit-description" rows="3" class="w-full px-3 py-2.5 text-sm border border-gray-200 rounded-lg bg-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors resize-none" placeholder="Entrez la description de la catégorie"></textarea>
        </div>
        @include('components.modals.modal-actions', ['modalId' => 'modal-edit-category'])
    </form>
</x-modal-form>

<!-- Composant modal de confirmation de suppression -->
<x-delete-confirmation-modal modalId="delete-category-modal" title="Confirmer la suppression" message="Êtes-vous sûr de vouloir supprimer cette catégorie ? Cette action est irréversible." />

<!-- Styles pour les tooltips -->
<style>
    .tooltip {
        position: absolute;
        bottom: 100%;
        left: 50%;
        transform: translateX(-50%) translateY(-8px);
        padding: 0.5rem 0.75rem;
        background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
        color: white;
        font-size: 0.75rem;
        font-weight: 500;
        white-space: nowrap;
        border-radius: 0.5rem;
        opacity: 0;
        pointer-events: none;
        transition: all 0.2s ease;
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.2);
        z-index: 50;
    }

    .tooltip::before {
        content: '';
        position: absolute;
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        border: 6px solid transparent;
        border-top-color: #1e293b;
    }

    .group:hover .tooltip {
        opacity: 1;
        transform: translateX(-50%) translateY(-12px);
    }

    /* Animation au survol des boutons */
    .group button:hover,
    .group a:hover {
        animation: pulse-shadow 1s ease-in-out infinite;
    }

    @keyframes pulse-shadow {

        0%,
        100% {
            box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
        }

        50% {
            box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
        }
    }

</style>

<!-- Scripts -->
<script>
    // Ouvrir le modal d'édition avec les données de la catégorie
    function openEditModal(categoryId, categoryName, categoryDescription) {
        const modal = document.getElementById('modal-edit-category');
        const form = document.getElementById('edit-category-form');
        const nameInput = document.getElementById('edit-name');
        const descriptionInput = document.getElementById('edit-description');

        // Mettre à jour l'action du formulaire
        form.action = '/categories/' + categoryId;

        // Remplir les champs avec les données de la catégorie
        nameInput.value = categoryName;
        descriptionInput.value = categoryDescription || '';

        // Ouvrir le modal
        modal.classList.remove('hidden');
        modal.classList.add('flex', 'items-center', 'justify-center');

        // Empêcher le scroll du body
        document.body.style.overflow = 'hidden';
    }

    // Ouvrir le modal de confirmation de suppression de catégorie
    function openDeleteCategoryModal(categoryId) {
        openDeleteConfirmModal('delete-category-modal', function() {
            const form = document.getElementById('delete-form-' + categoryId);
            if (form) {
                form.submit();
            }
        });
    }

</script>

<!-- Script pour rouvrir le modal en cas d'erreur de validation -->
@if($errors->any())
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const modal = document.getElementById('modal-new-category');
        if (modal) {
            modal.classList.remove('hidden');
            modal.classList.add('flex', 'items-center', 'justify-center');
        }
    });

</script>
@endif
@endsection
