<div class="space-y-6">
    {{-- Header --}}
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Dashboard</h1>
            <p class="text-gray-500 text-sm">Resumen general de procesos y actividad</p>
        </div>
        <div class="flex items-center gap-2">
            <span class="text-sm text-gray-500">{{ now()->format('l, d M Y') }}</span>
        </div>
    </div>
    
    {{-- Stats Cards --}}
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
        {{-- Total Procesos --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-indigo-100 rounded-xl flex items-center justify-center">
                    <svg class="w-6 h-6 text-indigo-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-500">Total Procesos</p>
                    <p class="text-2xl font-bold text-gray-900">{{ number_format($stats['total_procesos'] ?? 0) }}</p>
                </div>
            </div>
        </div>
        
        {{-- Cartera Total --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-green-100 rounded-xl flex items-center justify-center">
                    <svg class="w-6 h-6 text-green-600" 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"/>
                    </svg>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-500">Cartera Total</p>
                    <p class="text-2xl font-bold text-gray-900">${{ number_format($stats['total_cartera'] ?? 0, 0, ',', '.') }}</p>
                </div>
            </div>
        </div>
        
        {{-- Alertas Pendientes --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-amber-100 rounded-xl flex items-center justify-center">
                    <svg class="w-6 h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                    </svg>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-500">Alertas Pendientes</p>
                    <p class="text-2xl font-bold text-gray-900">{{ $alertasPendientes->count() }}</p>
                </div>
            </div>
        </div>
        
        {{-- Capital --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center">
                    <svg class="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-500">Capital</p>
                    <p class="text-2xl font-bold text-gray-900">${{ number_format($stats['total_capital'] ?? 0, 0, ',', '.') }}</p>
                </div>
            </div>
        </div>
    </div>
    
    {{-- Charts Row --}}
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {{-- Procesos por Estado --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">Procesos por Estado</h3>
            <div class="h-64">
                <canvas id="chartEstados"></canvas>
            </div>
        </div>
        
        {{-- Procesos por Etapa --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">Procesos por Etapa Procesal</h3>
            <div class="h-64">
                <canvas id="chartEtapas"></canvas>
            </div>
        </div>
    </div>
    
    {{-- Alertas y Actividad --}}
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {{-- Alertas Pendientes --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900">Alertas Pendientes</h3>
                <a href="{{ route('alertas.index') }}" class="text-sm text-indigo-600 hover:text-indigo-800">Ver todas →</a>
            </div>
            <div class="space-y-3">
                @forelse($alertasPendientes as $alerta)
                    <div class="flex items-start gap-3 p-3 rounded-lg {{ $alerta->prioridad === 'critica' ? 'bg-red-50' : ($alerta->prioridad === 'alta' ? 'bg-amber-50' : 'bg-gray-50') }}">
                        <div class="flex-shrink-0">
                            @if($alerta->prioridad === 'critica')
                                <span class="w-2 h-2 bg-red-500 rounded-full block mt-2"></span>
                            @elseif($alerta->prioridad === 'alta')
                                <span class="w-2 h-2 bg-amber-500 rounded-full block mt-2"></span>
                            @else
                                <span class="w-2 h-2 bg-blue-500 rounded-full block mt-2"></span>
                            @endif
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-sm font-medium text-gray-900 truncate">{{ $alerta->titulo }}</p>
                            <p class="text-xs text-gray-500">{{ $alerta->proceso?->radicado_interno ?? 'N/A' }} · {{ $alerta->created_at->diffForHumans() }}</p>
                        </div>
                    </div>
                @empty
                    <p class="text-sm text-gray-500 text-center py-4">No hay alertas pendientes</p>
                @endforelse
            </div>
        </div>
        
        {{-- Actividad Reciente --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900">Actividad Reciente</h3>
            </div>
            <div class="space-y-3">
                @forelse($actividadReciente as $actuacion)
                    <div class="flex items-start gap-3 p-3 rounded-lg bg-gray-50">
                        <div class="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center flex-shrink-0">
                            <span class="text-xs font-medium text-indigo-600">{{ substr($actuacion->user?->name ?? 'S', 0, 1) }}</span>
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-sm text-gray-900 truncate">{{ Str::limit($actuacion->descripcion, 50) }}</p>
                            <p class="text-xs text-gray-500">{{ $actuacion->proceso?->radicado_interno }} · {{ $actuacion->fecha_actuacion->format('d/m/Y') }}</p>
                        </div>
                    </div>
                @empty
                    <p class="text-sm text-gray-500 text-center py-4">No hay actividad reciente</p>
                @endforelse
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Datos desde PHP
    const porEstado = @json($stats['por_estado'] ?? []);
    const porEtapa = @json($stats['por_etapa'] ?? []);
    
    // Chart Estados
    const ctxEstados = document.getElementById('chartEstados');
    if (ctxEstados && typeof Chart !== 'undefined') {
        new Chart(ctxEstados, {
            type: 'doughnut',
            data: {
                labels: Object.keys(porEstado).map(e => e.charAt(0).toUpperCase() + e.slice(1)),
                datasets: [{
                    data: Object.values(porEstado),
                    backgroundColor: ['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#06b6d4'],
                    borderWidth: 0,
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: { position: 'bottom' }
                }
            }
        });
    }
    
    // Chart Etapas
    const ctxEtapas = document.getElementById('chartEtapas');
    if (ctxEtapas && typeof Chart !== 'undefined') {
        new Chart(ctxEtapas, {
            type: 'bar',
            data: {
                labels: Object.keys(porEtapa).map(e => e.replace('_', ' ').charAt(0).toUpperCase() + e.slice(1)),
                datasets: [{
                    label: 'Procesos',
                    data: Object.values(porEtapa),
                    backgroundColor: '#6366f1',
                    borderRadius: 6,
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: { display: false }
                },
                scales: {
                    y: { beginAtZero: true }
                }
            }
        });
    }
});
</script>
@endpush
