@extends('layouts.app')

@section('title', 'Départements')
@section('page-title', 'Départements')

@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">Départements</h1>
            <p class="mt-1 text-sm text-gray-600">Gérez les départements de l'hôpital</p>
        </div>
        <div class="flex items-center gap-3">
            <button type="button" data-modal-toggle="modal-import-department" class="inline-flex items-center px-5 py-2.5 bg-orange-500 hover:bg-orange-600 text-white font-medium rounded-lg 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="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
                </svg>
                Import
            </button>
            @can('create', App\Models\Department::class)
            <button type="button" data-modal-toggle="modal-new-department" 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>
                Nouveau
            </button>
            @endcan
        </div>
    </div>

    @can('create', App\Models\Department::class)
    <x-modal-form id="modal-new-department" title="Nouveau département">
        @include('components.modals.department', ['modalId' => 'modal-new-department'])
    </x-modal-form>
    @endcan

    <!-- Modal d'import des départements -->
    <x-modal-form id="modal-import-department" title="Importer des départements">
        <form method="POST" action="{{ route('departments.import') }}" enctype="multipart/form-data" class="space-y-6">
            @csrf
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Fichier Excel <span class="text-red-500">*</span></label>
                <input type="file" name="file" accept=".xlsx,.xls,.csv" required 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 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
                <p class="mt-2 text-xs text-gray-500">Formats acceptés : xlsx, xls, csv. Colonnes attendues : <strong>nom</strong>, <strong>responsable</strong>, <strong>telephone</strong></p>
            </div>
            <div class="flex justify-end gap-3 pt-4">
                <button type="button" data-modal-close="modal-import-department" class="px-4 py-2.5 text-sm font-medium border border-gray-300 rounded-lg text-gray-700 bg-white hover:bg-gray-50 transition-colors">
                    Annuler
                </button>
                <button type="submit" class="px-4 py-2.5 text-sm font-medium rounded-lg bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700 text-white shadow-sm transition-colors">
                    Importer la liste
                </button>
            </div>
        </form>
    </x-modal-form>

    <!-- Barre de recherche -->
    <div class="bg-white shadow-sm border border-gray-200 rounded-xl p-4">
        <form method="GET" action="{{ route('departments.index') }}" class="flex gap-3">
            <div class="flex-1 relative">
                <input type="text" name="search" value="{{ request('search') }}" placeholder="Rechercher par département / responsable..." class="w-full pl-10 pr-4 py-2.5 border border-gray-200 rounded-lg bg-white text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all">
            </div>
            <button type="submit" class="px-5 py-2.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors">
                Rechercher
            </button>
            @if(request('search'))
            <a href="{{ route('departments.index') }}" class="px-5 py-2.5 border border-gray-200 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors">
                Effacer
            </a>
            @endif
        </form>
    </div>

    <!-- Tableau des départements -->
    <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">
                            Responsable
                        </th>
                        <th class="px-6 py-4 text-left text-xs font-semibold text-gray-700 uppercase tracking-wider">
                            Téléphone
                        </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($departments as $department)
                    <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">
                                {{ $department->reference }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-medium text-gray-900">{{ $department->name }}</div>
                        </td>
                        <td class="px-6 py-4">
                            <div class="text-sm text-gray-700">{{ $department->name_of_manager ?? '-' }}</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-blue-100 text-blue-800">
                                {{ $department->phone_of_manager }}
                            </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 -->
                                @can('update', $department)
                                <div class="relative group">
                                    <button type="button" onclick="openEditModal('{{ $department->id }}', '{{ $department->name }}' ,'{{ $department->name_of_manager }}', '{{ $department->phone_of_manager  }}')" 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>
                                @endcan

                                <!-- Bouton Supprimer (uniquement pour Administrateur et Super Administrateur) -->
                                @can('delete', $department)
                                <div class="relative group">
                                    <form method="POST" action="{{ route('departments.destroy', $department->id) }}" class="delete-form inline-block" id="delete-form-{{ $department->id }}">
                                        @csrf
                                        @method('DELETE')
                                        <button type="button" onclick="openDeleteDepartmentModal('{{ $department->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>
                                @endcan
                            </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="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                                </svg>
                                <p class="text-sm font-medium text-gray-600 mb-1">Aucun département enregistré</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

    </div>
</div>

<!-- Modal d'édition du département -->
@can('update', App\Models\Department::class)
<x-modal-form id="modal-edit-department" title="Modifier le département">
    <form method="POST" id="edit-department-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">Nom du responsable <span class="text-red-500">*</span></label>
            <input type="text" name="name_of_manager" id="edit_manager_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"> Téléphone du responsable <span class="text-red-500">*</span></label>
            <input type="text" name="phone_of_manager" id="edit_manager_phone" 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>
        @include('components.modals.modal-actions', ['modalId' => 'modal-edit-department'])
    </form>
</x-modal-form>
@endcan

<!-- Composant modal de confirmation de suppression -->
@can('delete', App\Models\Department::class)
<x-delete-confirmation-modal modalId="delete-department-modal" title="Confirmer la suppression" message="Êtes-vous sûr de vouloir supprimer cet département ? Cette action est irréversible." />
@endcan
<script src="{{ asset('js/department.js') }}"> </script>

<!-- Ouvre le modal de création de département en cas derreur -->
@if($errors->any())
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const modal = document.getElementById('modal-new-department');
        if (modal) {
            modal.classList.remove('hidden');
            modal.classList.add('flex', 'items-center', 'justify-center');
        }
    });

</script>
@endif
@endsection
