<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight flex items-center">
                <svg class="w-6 h-6 mr-2 text-rose-500 dark:text-rose-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                {{ __('Control de Egresos (Gastos)') }}
            </h2>
            <div class="flex items-center gap-4 w-full md:w-auto">
                <div class="bg-rose-50 dark:bg-rose-900 border border-rose-100 dark:border-rose-800 px-4 py-2 rounded-lg text-rose-800 dark:text-rose-200 flex items-center shadow-sm transition-colors">
                    <span class="text-xs font-bold uppercase tracking-wider mr-2">Total Hoy:</span>
                    <span class="font-black text-lg">S/{{ number_format($totalDaily, 2) }}</span>
                </div>
                <a href="{{ route('expenses.create') }}" class="inline-flex items-center px-4 py-2 bg-indigo-600 border border-transparent rounded-lg font-bold text-xs text-white uppercase tracking-widest hover:bg-indigo-700 focus:bg-indigo-700 active:bg-indigo-900 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 transition ease-in-out duration-150 shadow-md">
                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
                    Registrar Gasto
                </a>
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            


            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-lg sm:rounded-xl border border-gray-100 dark:border-gray-700 transition-colors duration-200">
                <div class="p-0 border-b border-gray-200 dark:border-gray-700">
                    
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                            <thead class="bg-gray-50 dark:bg-gray-700">
                                <tr>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Fecha</th>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Categoría</th>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Descripción</th>
                                    <th scope="col" class="px-6 py-4 text-right text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Monto</th>
                                    <th scope="col" class="px-6 py-4 text-center text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Por</th>
                                    <th scope="col" class="px-6 py-4 text-right text-xs font-bold text-gray-500 dark:text-gray-300 uppercase tracking-wider">Acciones</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-100 dark:divide-gray-700">
                                @forelse ($expenses as $expense)
                                    <tr class="hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="text-sm font-semibold text-gray-900 dark:text-gray-100">{{ $expense->expense_date->format('d/m/Y') }}</div>
                                            <div class="text-xs text-gray-500 dark:text-gray-400">{{ $expense->created_at->format('H:i') }}</div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            @php
                                                $catColors = [
                                                    'supplier_payment' => 'bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200',
                                                    'internal_credit' => 'bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200',
                                                    'services' => 'bg-amber-100 dark:bg-amber-900 text-amber-800 dark:text-amber-200',
                                                    'other' => 'bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-200'
                                                ];
                                                $catNames = [
                                                    'supplier_payment' => 'Pago Proveedor',
                                                    'internal_credit' => 'Créd. Interno',
                                                    'services' => 'Servicios',
                                                    'other' => 'Varios'
                                                ];
                                            @endphp
                                            <span class="px-2.5 py-1 inline-flex text-xs leading-5 font-semibold rounded-full {{ $catColors[$expense->category] ?? $catColors['other'] }}">
                                                {{ $catNames[$expense->category] ?? 'Otro' }}
                                            </span>
                                        </td>
                                        <td class="px-6 py-4">
                                            <div class="text-sm text-gray-900 dark:text-gray-100 font-medium">{{ $expense->description }}</div>
                                            @if($expense->reference)
                                                <div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5"><span class="font-bold">Ref:</span> {{ $expense->reference }}</div>
                                            @endif
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-right">
                                            <span class="text-sm font-black text-rose-600 dark:text-rose-400">S/{{ number_format($expense->amount, 2) }}</span>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-center">
                                            <div class="text-xs text-gray-600 dark:text-gray-300 border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-700 rounded px-2 py-1 inline-block">
                                                {{ $expense->user->name ?? 'Sistema' }}
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                            <button type="button" onclick="swalDelete('{{ route('expenses.destroy', $expense->id) }}', '{{ csrf_token() }}', '\u00bfEliminar este gasto?', 'Esta acci\u00f3n afectar\u00e1 los reportes del sistema.')" title="Eliminar registro" class="text-rose-400 hover:text-rose-700 transition-colors p-1 rounded hover:bg-rose-50">
                                                <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                    <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>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="6" class="px-6 py-12 whitespace-nowrap text-sm text-center text-gray-500">
                                            <div class="flex flex-col items-center justify-center">
                                                <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 14l6-6m-5.5.5h.01m4.49 4.49h.01M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16l3.5-2 3.5 2 3.5-2 3.5 2zM10 8.5a.5.5 0 11-1 0 .5.5 0 011 0zm5 5a.5.5 0 11-1 0 .5.5 0 011 0z"></path></svg>
                                                <p class="text-base font-semibold text-gray-600">No hay egresos registrados</p>
                                                <p class="text-xs text-gray-400 mt-1 mb-4">Aún no has registrado ningún gasto en el sistema.</p>
                                                <a href="{{ route('expenses.create') }}" class="text-indigo-600 hover:text-indigo-800 font-medium hover:underline">Registrar el primer gasto</a>
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    @if($expenses->hasPages())
                        <div class="px-6 py-4 border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-800">
                            {{ $expenses->links() }}
                        </div>
                    @endif

                </div>
            </div>
        </div>
    </div>
</x-app-layout>
