@using Rs_system.Models
@model List<ReporteMensualContable>
@{
    ViewData["Title"] = "Contabilidad Mensual";
    var grupoId = ViewBag.GrupoId as long?;
}

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1">Contabilidad Mensual</h4>
        <p class="text-muted mb-0">Gestión de reportes financieros por grupo</p>
    </div>
</div>

<div class="card card-custom mb-4">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-end">
            <div class="col-md-8">
                <label class="form-label">Grupo de Trabajo</label>
                <select name="grupoId" class="form-select" asp-items="ViewBag.Grupos" onchange="this.form.submit()">
                    <option value="">Seleccione un grupo...</option>
                </select>
            </div>
            <div class="col-md-4">
                <button type="button" class="btn btn-primary-custom w-100" data-bs-toggle="modal" data-bs-target="#abrirMesModal" @(grupoId.HasValue ? "" : "disabled")>
                    <i class="bi bi-plus-lg me-1"></i> Abrir Nuevo Mes
                </button>
            </div>
        </form>
    </div>
</div>

@if (grupoId.HasValue)
{
    @if (Model != null && Model.Any())
    {
        <div class="card card-custom">
            <div class="table-responsive">
                <table class="table table-custom">
                    <thead>
                        <tr>
                            <th>Mes / Año</th>
                            <th>Saldo Inicial</th>
                            <th>Estado</th>
                            <th>Fecha Creación</th>
                            <th class="text-end">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td class="fw-bold">@item.NombreMes @item.Anio</td>
                                <td>@item.SaldoInicial.ToString("C")</td>
                                <td>
                                    @if (item.Cerrado)
                                    {
                                        <span class="badge bg-secondary">Cerrado</span>
                                    }
                                    else
                                    {
                                        <span class="badge bg-success">Abierto</span>
                                    }
                                </td>
                                <td>@item.FechaCreacion.ToLocalTime().ToString("dd/MM/yyyy HH:mm")</td>
                                <td class="text-end">
                                    <a asp-action="RegistroMensual" asp-route-id="@item.Id" class="btn btn-sm btn-outline-success">
                                        <i class="bi bi-table me-1"></i> @(item.Cerrado ? "Ver Detalles" : "Gestionar Registros")
                                    </a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
    else
    {
        <div class="alert alert-info py-4 text-center">
            <i class="bi bi-info-circle fs-3 d-block mb-3"></i>
            <h5>No hay reportes mensuales para este grupo</h5>
            <p class="mb-0">Haga clic en "Abrir Nuevo Mes" para comenzar el registro contable de este mes.</p>
        </div>
    }
}
else
{
    <div class="alert alert-light border text-center py-5">
        <i class="bi bi-people fs-2 d-block mb-3 text-muted"></i>
        <h5 class="text-muted">Seleccione un grupo para ver sus reportes mensuales</h5>
    </div>
}

<!-- Modal Abrir Mes -->
<div class="modal fade" id="abrirMesModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form asp-action="AbrirMes" method="post">
                <input type="hidden" name="grupoId" value="@grupoId" />
                <div class="modal-header">
                    <h5 class="modal-title">Abrir Nuevo Mes Contable</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Mes</label>
                            <select name="mes" class="form-select">
                                @for (int i = 1; i <= 12; i++)
                                {
                                    bool isCurrent = i == DateTime.Now.Month;
                                    <!option value="@i" @(isCurrent ? "selected" : "")>@(new DateTime(2000, i, 1).ToString("MMMM", new System.Globalization.CultureInfo("es-ES")))</!option>
                                }
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Año</label>
                            <select name="anio" class="form-select">
                                @for (int i = DateTime.Now.Year - 1; i <= DateTime.Now.Year + 1; i++)
                                {
                                    bool isCurrent = i == DateTime.Now.Year;
                                    <!option value="@i" @(isCurrent ? "selected" : "")>@i</!option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="alert alert-info mt-3 mb-0 py-2 small">
                        <i class="bi bi-lightbulb me-2"></i>El saldo inicial se calculará automáticamente a partir del cierre del mes anterior si existe.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-primary-custom">Abrir Mes</button>
                </div>
            </form>
        </div>
    </div>
</div>

