@model List<Rs_system.Models.ReporteMensualGeneral>

@{
    ViewData["Title"] = "Contabilidad General";
    var anioActual = ViewBag.Anio;
}

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 text-gray-800">Contabilidad General</h1>
        
        <form asp-action="Index" method="get" class="form-inline">
            <label class="me-2">Año:</label>
            <select name="anio" class="form-control me-2" asp-items="ViewBag.Anios" onchange="this.form.submit()">
                <option value="@anioActual" selected>@anioActual</option>
            </select>
        </form>
    </div>

    @if (TempData["Error"] != null)
    {
        <div class="alert alert-danger">@TempData["Error"]</div>
    }

    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Reportes Mensuales @anioActual</h6>
                    <div>
                        <a asp-action="GestionCategorias" class="btn btn-info btn-sm">
                            <i class="fas fa-tags"></i> Gestionar Categorías
                        </a>
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#abrirMesModal">
                            <i class="fas fa-plus"></i> Abrir Nuevo Mes
                        </button>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>Mes</th>
                                    <th>Saldo Inicial</th>
                                    <th>Estado</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    <tr>
                                        <td>@item.NombreMes</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>
                                            <a asp-action="RegistroMensual" asp-route-id="@item.Id" class="btn btn-primary btn-sm">
                                                <i class="fas fa-edit"></i> Gestionar
                                            </a>
                                            <a asp-action="Consolidado" asp-route-id="@item.Id" class="btn btn-info btn-sm">
                                                <i class="fas fa-chart-pie"></i> Ver Consolidado
                                            </a>
                                        </td>
                                    </tr>
                                }
                                @if (!Model.Any())
                                {
                                    <tr>
                                        <td colspan="4" class="text-center">No hay reportes para este año.</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal Abrir Mes -->
<div class="modal fade" id="abrirMesModal" tabindex="-1" role="dialog" aria-labelledby="abrirMesModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="abrirMesModalLabel">Abrir Nuevo Mes Contable</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form asp-action="AbrirMes" method="post">
                <div class="modal-body">
                    <input type="hidden" name="anio" value="@anioActual" />
                    <div class="form-group mb-3">
                        <label>Mes</label>
                        <select name="mes" class="form-select" required>
                            @for (int i = 1; i <= 12; i++)
                            {
                                var nMes = new DateTime(2000, i, 1).ToString("MMMM", new System.Globalization.CultureInfo("es-ES"));
                                <option value="@i">@nMes</option>
                            }
                        </select>
                    </div>
                    <p class="text-muted small">
                        Nota: Al abrir el mes, se calculará automáticamente el saldo inicial basado en el cierre del mes anterior.
                    </p>
                </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">Crear Reporte</button>
                </div>
            </form>
        </div>
    </div>
</div>
