@model foundation_system.Models.ViewModels.Reportes.ReporteGastosCategoriaViewModel

@{
    ViewData["Title"] = "Gastos por Categoría";
}

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0 text-gray-800">
                <i class="fas fa-chart-pie me-2 text-primary"></i>Gastos por Categoría
            </h1>
            <p class="text-muted small mb-0">Distribución de gastos por tipo</p>
        </div>
        <div class="d-flex gap-2">
            <button onclick="window.print()" class="btn btn-outline-secondary">
                <i class="fas fa-print me-2"></i>Imprimir
            </button>
        </div>
    </div>

    <!-- Filtro de Fecha -->
    <div class="card shadow-sm mb-4 d-print-none">
        <div class="card-body py-3">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-auto">
                    <label for="inicio" class="form-label fw-bold small">Desde</label>
                    <input type="date" class="form-control" id="inicio" name="inicio" 
                           value="@Model.FechaInicio.ToString("yyyy-MM-dd")">
                </div>
                <div class="col-auto">
                    <label for="fin" class="form-label fw-bold small">Hasta</label>
                    <input type="date" class="form-control" id="fin" name="fin" 
                           value="@Model.FechaFin.ToString("yyyy-MM-dd")">
                </div>
                <div class="col-auto">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-sync-alt me-2"></i>Generar
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="row">
        <!-- Tabla Detalle -->
        <div class="col-lg-8">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Desglose por Categoría</h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover" width="100%" cellspacing="0">
                            <thead class="table-light">
                                <tr>
                                    <th>Categoría</th>
                                    <th class="text-center">Transacciones</th>
                                    <th class="text-end">Monto Total</th>
                                    <th class="text-end">%</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.Categorias)
                                {
                                    <tr>
                                        <td>
                                            <span class="fw-bold text-dark">@item.Categoria</span>
                                            <div class="progress mt-1" style="height: 4px;">
                                                <div class="progress-bar bg-info" role="progressbar" 
                                                     style="width: @item.Porcentaje.ToString("0")%" 
                                                     aria-valuenow="@item.Porcentaje" aria-valuemin="0" aria-valuemax="100"></div>
                                            </div>
                                        </td>
                                        <td class="text-center">@item.CantidadTransacciones</td>
                                        <td class="text-end">@item.MontoTotal.ToString("C2")</td>
                                        <td class="text-end small">@item.Porcentaje.ToString("F1")%</td>
                                    </tr>
                                }
                                <tr class="table-secondary fw-bold">
                                    <td>TOTAL</td>
                                    <td class="text-center">@Model.Categorias.Sum(c => c.CantidadTransacciones)</td>
                                    <td class="text-end">@Model.TotalGastos.ToString("C2")</td>
                                    <td class="text-end">100.0%</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Resumen Visual (Solo visible en pantalla) -->
        <div class="col-lg-4 d-print-none">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Resumen</h6>
                </div>
                <div class="card-body">
                    <div class="text-center mb-4">
                        <h1 class="display-4 fw-bold text-gray-800">@Model.TotalGastos.ToString("C2")</h1>
                        <p class="text-muted">Gasto Total del Período</p>
                    </div>
                    <hr>
                    <div class="small text-muted">
                        <i class="fas fa-info-circle me-1"></i>
                        Las categorías con mayor consumo representan el 
                        <strong>@(Model.Categorias.OrderByDescending(c => c.MontoTotal).FirstOrDefault()?.Porcentaje.ToString("F0") ?? "0")%</strong> 
                        del total.
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="text-center mt-4 text-muted small">
        <p>Generado por: @Model.GeneradoPor | Fecha: @Model.FechaGeneracion.ToString("g")</p>
    </div>
</div>

@section Styles {
    <style>
        @@media print {
            .d-print-none, .no-print { display: none !important; }
            .sidebar, .top-header, .footer { display: none !important; }
            .main-content { margin-left: 0 !important; padding: 0 !important; }
            .page-container { padding: 0 !important; }
            .container-fluid { width: 100% !important; padding: 0 !important; }
            body { background: white !important; }
            .card { border: 1px solid #ddd !important; box-shadow: none !important; }
            .card-body { padding: 1rem !important; }
            .btn { display: none !important; }
            .col-lg-8 { width: 100% !important; flex: 0 0 100% !important; max-width: 100% !important; }
        }
    </style>
}
