@model IEnumerable<Rs_system.Models.RegistroCulto>
@{
    ViewData["Title"] = "Registro de Ofrendas";
}

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1">Registro de Ofrendas</h4>
        <p class="text-muted mb-0">Gestión de ofrendas por culto</p>
    </div>
    <a asp-action="Create" class="btn btn-primary-custom">
        <i class="bi bi-plus-lg me-1"></i> Nuevo Registro
    </a>
</div>

<!-- Filters -->
<div class="card-custom mb-4">
    <form method="get" class="row g-3 align-items-end">
        <div class="col-md-3">
            <label class="form-label">Mes</label>
            <select name="mes" class="form-select" asp-items="@(ViewBag.Meses as List<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>)">
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label">Año</label>
            <select name="anio" class="form-select" asp-items="@(ViewBag.Anios as List<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>)">
            </select>
        </div>
        <div class="col-md-3">
            <button type="submit" class="btn btn-outline-primary">
                <i class="bi bi-funnel me-1"></i> Filtrar
            </button>
        </div>
    </form>
</div>

<script>
    document.querySelector('select[name="mes"]').value = '@ViewBag.MesActual';
    document.querySelector('select[name="anio"]').value = '@ViewBag.AnioActual';
</script>

<!-- Summary Cards -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="card-custom text-center">
            <h6 class="text-muted mb-2">Total Ofrendas</h6>
            <h3 class="text-primary mb-0">$ @Model.Sum(r => r.TotalOfrendas).ToString("N2")</h3>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card-custom text-center">
            <h6 class="text-muted mb-2">Total Descuentos</h6>
            <h3 class="text-warning mb-0">$ @Model.Sum(r => r.TotalDescuentos).ToString("N2")</h3>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card-custom text-center">
            <h6 class="text-muted mb-2">Monto Neto</h6>
            <h3 class="text-success mb-0">$ @Model.Sum(r => r.MontoNeto).ToString("N2")</h3>
        </div>
    </div>
</div>

<!-- Records Table -->
<div class="card-custom">
    <div class="table-responsive">
        <table class="table-custom">
            <thead>
                <tr>
                    <th>Fecha</th>
                    <th>Ofrendas</th>
                    <th class="text-end">Total</th>
                    <th class="text-end">Descuentos</th>
                    <th class="text-end">Neto</th>
                    <th class="text-center">Acciones</th>
                </tr>
            </thead>
            <tbody>
                @if (!Model.Any())
                {
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">
                            <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                            No hay registros para el período seleccionado
                        </td>
                    </tr>
                }
                @foreach (var registro in Model)
                {
                    <tr>
                        <td>
                            <strong>@registro.Fecha.ToString("dd/MM/yyyy")</strong>
                            <br>
                            <small class="text-muted">@registro.Fecha.DayOfWeek</small>
                        </td>
                        <td>
                            @foreach (var ofrenda in registro.Ofrendas.Take(3))
                            {
                                <span class="badge bg-light text-dark me-1">@ofrenda.Concepto</span>
                            }
                            @if (registro.Ofrendas.Count > 3)
                            {
                                <span class="badge bg-secondary">+@(registro.Ofrendas.Count - 3) más</span>
                            }
                        </td>
                        <td class="text-end">$ @registro.TotalOfrendas.ToString("N2")</td>
                        <td class="text-end text-warning">$ @registro.TotalDescuentos.ToString("N2")</td>
                        <td class="text-end text-success fw-bold">$ @registro.MontoNeto.ToString("N2")</td>
                        <td class="text-center">
                            <a asp-action="Details" asp-route-id="@registro.Id" class="btn btn-sm btn-outline-primary" title="Ver detalles">
                                <i class="bi bi-eye"></i>
                            </a>
                            <a asp-action="Edit" asp-route-id="@registro.Id" class="btn btn-sm btn-outline-secondary" title="Editar">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <button type="button" class="btn btn-sm btn-outline-danger" onclick="confirmDelete(@registro.Id)" title="Eliminar">
                                <i class="bi bi-trash"></i>
                            </button>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

<!-- Delete Form -->
<form id="deleteForm" asp-action="Delete" method="post" style="display: none;">
    <input type="hidden" name="id" id="deleteId" />
</form>

@section Scripts {
    <script>
        function confirmDelete(id) {
            Swal.fire({
                title: '¿Eliminar registro?',
                text: 'Esta acción no se puede deshacer.',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#6c757d',
                confirmButtonText: 'Sí, eliminar',
                cancelButtonText: 'Cancelar'
            }).then((result) => {
                if (result.isConfirmed) {
                    document.getElementById('deleteId').value = id;
                    document.getElementById('deleteForm').submit();
                }
            });
        }
    </script>
}
