{{-- resources/views/dashboard/tramites.blade.php --}}
@extends('layouts.app')

@section('title', 'Dashboard - Trámites')

@push('styles')
<style>
    .tramite-card {
        transition: all 0.3s;
        cursor: pointer;
    }
    .tramite-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    }
    .status-badge {
        position: absolute;
        top: 10px;
        right: 10px;
    }
</style>
@endpush

@section('content')
<div class="content">
    {{-- Cabecera --}}
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="fw-bold mb-1">
                <i class="fa fa-clipboard-list me-2 text-info"></i>Panel de Trámites
            </h1>
            <p class="text-muted mb-0">Gestión de autorizaciones, notas y seguimiento de trámites</p>
        </div>
        <div>
            <a href="{{ route('autorizaciones.create') }}" class="btn btn-primary">
                <i class="fa fa-plus me-1"></i> Nueva Autorización
            </a>
            <a href="{{ route('notas.create') }}" class="btn btn-alt-success ms-1">
                <i class="fa fa-file-alt me-1"></i> Nueva Nota
            </a>
        </div>
    </div>

    {{-- KPIs de Trámites --}}
    <div class="row g-3 mb-4">
        <div class="col-md-3">
            <div class="block block-rounded bg-body-light tramite-card">
                <div class="block-content block-content-full text-center position-relative">
                    @if(isset($autorizacionesPendientes) && $autorizacionesPendientes > 0)
                        <span class="badge bg-danger status-badge">{{ $autorizacionesPendientes }} pendientes</span>
                    @endif
                    <i class="fa fa-check-circle fa-2x text-primary mb-2"></i>
                    <div class="fs-2 fw-bold">{{ number_format($autorizacionesMes ?? 0) }}</div>
                    <div class="text-muted">Autorizaciones este mes</div>
                    <small class="text-info">Total: {{ number_format(($autorizacionesPendientes ?? 0) + ($autorizacionesMes ?? 0)) }}</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="block block-rounded bg-body-light tramite-card">
                <div class="block-content block-content-full text-center position-relative">
                    @if(isset($notasPendientes) && $notasPendientes > 0)
                        <span class="badge bg-warning status-badge">{{ $notasPendientes }} pendientes</span>
                    @endif
                    <i class="fa fa-file-alt fa-2x text-success mb-2"></i>
                    <div class="fs-2 fw-bold">{{ number_format($notasMes ?? 0) }}</div>
                    <div class="text-muted">Notas este mes</div>
                    <small class="text-info">Total: {{ number_format(($notasPendientes ?? 0) + ($notasMes ?? 0)) }}</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="block block-rounded bg-body-light tramite-card">
                <div class="block-content block-content-full text-center">
                    <i class="fa fa-tasks fa-2x text-warning mb-2"></i>
                    <div class="fs-2 fw-bold">{{ number_format($tramitesActivos ?? 0) }}</div>
                    <div class="text-muted">Trámites Activos</div>
                    <small class="text-success">+{{ $tramitesCompletados ?? 0 }} completados</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="block block-rounded bg-body-light tramite-card">
                <div class="block-content block-content-full text-center">
                    <i class="fa fa-clock fa-2x text-danger mb-2"></i>
                    <div class="fs-2 fw-bold">{{ $tiempoPromedio ?? 0 }} días</div>
                    <div class="text-muted">Tiempo promedio</div>
                    <small class="text-muted">Duración de trámites</small>
                </div>
            </div>
        </div>
    </div>
 {{-- Acciones Rápidas --}}
    <div class="block block-rounded mb-4">
        <div class="block-header block-header-default">
            <h3 class="block-title">
                <i class="fa fa-bolt me-1 text-warning"></i> Acciones Rápidas
            </h3>
        </div>
        <div class="block-content">
            <div class="row g-3 text-center">
                <div class="col-md-3 col-6">
                    <a href="{{ route('autorizaciones.create') }}" class="d-block p-3 rounded bg-body-light tramite-card">
                        <i class="fa fa-check-circle fa-2x text-primary mb-2"></i>
                        <div class="fw-semibold">Nueva Autorización</div>
                        <small class="text-muted">Para clientes</small>
                    </a>
                </div>
                <div class="col-md-3 col-6">
                    <a href="{{ route('notas.create') }}" class="d-block p-3 rounded bg-body-light tramite-card">
                        <i class="fa fa-file-alt fa-2x text-success mb-2"></i>
                        <div class="fw-semibold">Nueva Nota</div>
                        <small class="text-muted">Entrega de lote</small>
                    </a>
                </div>
                <div class="col-md-3 col-6">
                    <a href="{{ route('tramites.index') }}" class="d-block p-3 rounded bg-body-light tramite-card">
                        <i class="fa fa-tasks fa-2x text-warning mb-2"></i>
                        <div class="fw-semibold">Ver Trámites</div>
                        <small class="text-muted">Seguimiento</small>
                    </a>
                </div>
                <div class="col-md-3 col-6">
                    <a href="{{ route('pagos-tramites.index') }}" class="d-block p-3 rounded bg-body-light tramite-card">
                        <i class="fa fa-money-bill fa-2x text-info mb-2"></i>
                        <div class="fw-semibold">Pagos Trámites</div>
                        <small class="text-muted">Registrar pagos</small>
                    </a>
                </div>
            </div>
        </div>
    </div>
{{-- Listado de Notas (Ahora a ancho completo) --}}
<div class="row g-3 mb-4">
    <div class="col-xl-12">
        <div class="block block-rounded">
            <div class="block-header block-header-default">
                <h3 class="block-title">
                    <i class="fa fa-file-alt me-1 text-success"></i> Últimas Notas de Entrega
                </h3>
                <a href="{{ route('notas.index') }}" class="btn btn-sm btn-alt-primary">
                    Ver todas <i class="fa fa-arrow-right ms-1"></i>
                </a>
            </div>
            <div class="block-content p-0">
                <div class="table-responsive">
                    <table class="table table-striped table-vcenter fs-sm mb-0">
                        <thead>
                            <tr>
                                <th>N°</th>
                                <th>Cliente</th>
                                <th>Lote</th>
                                <th>Fecha</th>
                                <th>Estado</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($ultimasNotas as $nota)
                            <tr>
                                <td class="fw-semibold">#{{ $nota->id }}</td>
                                <td>{{ $nota->cliente_nombre ?? 'N/A' }}</td>
                                <td>{{ $nota->lote_codigo ?? 'N/A' }}</td>
                                <td>{{ \App\Helpers\FechaHelper::bdToLocal($nota->fecha) }}</td>
                                <td>
                                    <span class="badge bg-{{ ($nota->estado ?? 'PENDIENTE') === 'ENTREGADO' ? 'success' : 'warning' }}">
                                        {{ $nota->estado ?? 'PENDIENTE' }}
                                    </span>
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">No hay notas registradas</td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

    {{-- Footer --}}
    <div class="text-center text-muted fs-sm py-3">
        <i class="fa fa-sync-alt me-1"></i> Datos actualizados al {{ $fechaSistema ?? date('d/m/Y') }}
        <span class="mx-2">•</span>
        <i class="fa fa-database me-1"></i> Base: {{ $bd ?? 'Cosmos 79' }}
    </div>
</div>

@endsection