@page "/quotes"

@using Core.Dtos
@using Domain.Generics
@using phronCare.UIBlazor.Services.Sales.Quotes
@inject NavigationManager Navigation
@inject QuoteService quoteService
@inject IToastService toastService

<div class="card" style="zoom:80%">
    <div class="card-header d-flex justify-content-center align-items-center">
        <h3 class="card-title m-0">Consulta de Presupuestos</h3>
    </div>
    <!-- BODY -->
    <div class="card-body px-4">
        <!-- FILTROS -->
        <div class="mb-3 row g-2 align-items-end">
            <div class="col-sm">
                <label for="quotenumber">Presupuesto</label>
                <input id="quotenumber" @bind="Filters.QuoteNumber" class="form-control form-control-sm" placeholder="Q-0000000X" />
            </div>
            <div class="col-sm">
                <label for="customer">Cliente</label>
                <input id="customer" @bind="Filters.CustomerText" class="form-control form-control-sm" placeholder="Nombre o código" />
            </div>
            <div class="col-sm">
                <label for="professional">Médico</label>
                <input id="professional" @bind="Filters.ProfessionalText" class="form-control form-control-sm" placeholder="Nombre o código" />
            </div>
            <div class="col-sm">
                <label for="institution">Hospital</label>
                <input id="institution" @bind="Filters.InstitutionText" class="form-control form-control-sm" placeholder="Nombre o código" />
            </div>
            <div class="col-sm">
                <label for="patient">Paciente</label>
                <input id="patient" @bind="Filters.PatientText" class="form-control form-control-sm" placeholder="Nombre o código" />
            </div>
            <div class="col-sm">
                <label for="status">Estado</label>
                <select id="status" @bind="Filters.Status" class="form-select form-select-sm">
                    <option value="">— Todos —</option>
                    <option value="Emitido">Emitido</option>
                    <option value="Aprobado">Aprobado</option>
                    <option value="Despacho">Despacho</option>
                    <option value="SinConsumo">Sin Consumo</option>
                    <option value="Transito">Transito</option>
                    <option value="Cerrado">Cerrado</option>
                </select>
            </div>
            <div class="col-sm">
                <label for="datefrom">Desde</label>
                <InputDate id="datefrom" @bind-Value="Filters.IssueDateFrom" class="form-control form-control-sm" />
            </div>
            <div class="col-sm">
                <label for="dateto">Hasta</label>
                <InputDate id="dateto" @bind-Value="Filters.IssueDateTo" class="form-control form-control-sm" />
            </div>
            <div class="col-auto">
                <button class="btn btn-primary btn-sm rounded-pill" @onclick="Search">
                    <i class="fas fa-binoculars me-1"></i> Buscar
                </button>
                <button class="btn btn-secondary btn-sm rounded-pill ms-1" @onclick="OnClear">
                    <i class="fas fa-eraser me-1"></i> Limpiar
                </button>
                <button class="btn btn-success btn-sm rounded-pill" @onclick="Create">
                    <i class="fas fa-plus me-1"></i> Nuevo
                </button>
            </div>
        </div>
        <hr />

        <!-- TABLA DE RESULTADOS -->
        @if (PagedQuotes?.Items?.Any() == true)
        {
            <table class="table table-sm table-striped table-hover" style="zoom:0.9;">
                <thead>
                    <tr>
                        <th>Quotenumber</th>
                        <th>Emisión</th>
                        <th>Estimada</th>
                        <th>Cliente</th>
                        <th>Médico</th>
                        <th>Hospital</th>
                        <th>Paciente</th>
                        <th>Unidad</th>
                        <th>Moneda</th>
                        <th>Total</th>
                        <th>Estado</th>
                        <th>Vendedor</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var quote in PagedQuotes.Items)
                    {
                        <tr>
                            <td>@quote.Quotenumber</td>
                            <td>@quote.IssueDate.ToString("dd/MM/yyyy")</td>
                            <td>@(quote.EstimatedDate.HasValue ? quote.EstimatedDate.Value.ToString("dd/MM/yyyy") : "—")</td>
                            <td>@quote.CustomerName</td>
                            <td>@quote.ProfessionalName</td>
                            <td>@quote.InstitutionName</td>
                            <td>@quote.PatientName</td>
                            <td>@quote.BusinessUnitName</td>
                            <td>@quote.Currency</td>
                            <td>@string.Format("{0:N2}", quote.Total)</td>
                            <td>
                                <span class="badge @GetStatusBadge(quote.Status)">@quote.Status</span>
                            </td>
                            <td>@quote.SalespersonName</td>
                            <td>
                                <button class="btn btn-link btn-lg p-0 text-success ms-2" @onclick="() => ToggleDetail(quote)"><i class="fas fa-eye"></i></button>
                                <button class="btn btn-link btn-lg p-0 text-primary ms-2" @onclick="() => PrintPdf(quote.Id)"><i class="fas fa-print"></i></button>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            @if (SelectedQuote != null)
            {
                <!-- BACKDROP semitransparente -->
                <div class="position-fixed top-0 start-0 w-100 h-100" style="background: rgba(0,0,0,0.4);"></div>
                <!-- DRAWER LATERAL -->
                <div class="position-fixed top-0 end-0 h-100 bg-white shadow" style="width: 45%; z-index:1050;">
                    <!-- CABECERA -->
                    <div class="d-flex justify-content-between align-items-center border-bottom px-3 py-2">
                        <h5 class="m-0">Detalle Presupuesto @SelectedQuote?.Quotenumber</h5>
                        <button class="btn-close" @onclick="() => SelectedQuote = null"></button>
                    </div>
                    <!-- CONTENIDO -->
                    <div class="p-3">
                        <ul class="nav nav-tabs mb-3" role="tablist">
                            <li class="nav-item">
                                <button class="nav-link @((activeTab == "Datos") ? "active bg-primary text-white" : "")"
                                        @onclick='() => activeTab = "Datos"'
                                        title="Datos generales">
                                    <i class="fas fa-info-circle me-1"></i> Datos
                                </button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link @((activeTab == "Items") ? "active bg-success text-white" : "")"
                                        @onclick='() => activeTab = "Items"'
                                        title="Productos e impuestos">
                                    <i class="fas fa-boxes-stacked me-1"></i> Items
                                </button>
                            </li>
                        </ul>

                        @if (activeTab == "Datos")
                        {
                            <dl class="row">
                                <dt class="col-5">Cliente:</dt>
                                <dd class="col-7">@SelectedQuote?.CustomerName</dd>
                                <dt class="col-5">Médico:</dt>
                                <dd class="col-7">@SelectedQuote?.ProfessionalName</dd>
                                <dt class="col-5">Hospital:</dt>
                                <dd class="col-7">@SelectedQuote?.InstitutionName</dd>
                                <dt class="col-5">Paciente:</dt>
                                <dd class="col-7">@SelectedQuote?.PatientName</dd>
                                <dt class="col-5">Emisión:</dt>
                                <dd class="col-7">@SelectedQuote?.IssueDate.ToString("dd/MM/yyyy")</dd>
                                <dt class="col-5">Estimada:</dt>
                                <dd class="col-7">@SelectedQuote?.EstimatedDate?.ToString("dd/MM/yyyy") ?? "—"</dd>
                                <dt class="col-5">Unidad Negocio:</dt>
                                <dd class="col-7">@SelectedQuote?.BusinessUnitName</dd>
                                <dt class="col-5">Moneda:</dt>
                                <dd class="col-7">@SelectedQuote?.Currency</dd>
                                <dt class="col-5">Vendedor:</dt>
                                <dd class="col-7">@SelectedQuote?.SalespersonName</dd>
                                <dt class="col-5">Estado:</dt>
                                <dd class="col-7">
                                    <span class="badge @GetStatusBadge(SelectedQuote?.Status ?? "")">@SelectedQuote?.Status</span>
                                </dd>
                                <dt class="col-5">Observaciones:</dt>
                                <dd class="col-7">SN</dd>
                            </dl>
                        }
                        else if (activeTab == "Items")
                        {
                            if (SelectedQuote?.Items == null || !SelectedQuote.Items.Any())
                            {
                                <p>No hay productos en este presupuesto.</p>
                            }
                            else
                            {
                                <h6 class="mt-2"><i class="fas fa-box-open me-1"></i> Productos cotizados</h6>
                                <table class="table table-sm table-bordered">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Descripción</th>
                                            <th>Cant.</th>
                                            <th>Precio U.</th>
                                            <th>Subtotal</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var item in SelectedQuote.Items)
                                        {
                                            <tr>
                                                <td>@item.Description</td>
                                                <td>@item.Quantity</td>
                                                <td>@string.Format("{0:N2}", item.UnitPrice)</td>
                                                <td>@string.Format("{0:N2}", item.Quantity * item.UnitPrice)</td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            }

                            if (SelectedQuote?.Taxes != null && SelectedQuote.Taxes.Any())
                            {
                                <hr class="my-3" />
                                <h6 class="mt-4"><i class="fas fa-receipt me-1"></i> Impuestos aplicados</h6>
                                <table class="table table-sm table-bordered">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Impuesto</th>
                                            <th>Código</th>
                                            <th>Porcentaje</th>
                                            <th>Importe</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var tax in SelectedQuote.Taxes)
                                        {
                                            <tr>
                                                <td>@tax.TaxName</td>
                                                <td>@tax.TaxCode</td>
                                                <td>@string.Format("{0:N2}%", tax.TaxRate)</td>
                                                <td>@string.Format("{0:N2}", tax.TaxAmount)</td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            }
                        }
                    </div>
                </div>
            }
        }
        else if (IsLoading)
        {
            <p>Cargando...</p>
        }
        else
        {
            <p>No hay resultados.</p>
        }
    </div>

    <!-- FOOTER: PAGINACIÓN -->
    <div class="card-footer d-flex justify-content-center align-items-center" style="zoom:80%;">
        <div class="d-flex align-items-center gap-3">
            <button class="btn btn-secondary rounded-pill" @onclick="PrimeraPagina" disabled="@(Filters.Page == 1)">
                <i class="fas fa-angle-double-left me-1"></i> Primera
            </button>
            <button class="btn btn-secondary rounded-pill" @onclick="AnteriorPagina" disabled="@(!PuedeRetroceder)">
                <i class="fas fa-chevron-left me-1"></i> Anterior
            </button>
            <span class="mx-2">
                Página <strong>@Filters.Page</strong> de <strong>@TotalPaginas</strong>
            </span>
            <button class="btn btn-secondary rounded-pill" @onclick="SiguientePagina" disabled="@(!PuedeAvanzar)">
                Siguiente <i class="fas fa-chevron-right ms-1"></i>
            </button>
            <button class="btn btn-secondary rounded-pill" @onclick="UltimaPagina" disabled="@(Filters.Page == TotalPaginas)">
                Última <i class="fas fa-angle-double-right ms-1"></i>
            </button>
            <div class="d-flex align-items-center ms-3">
                <input type="number"
                       class="form-control form-control-sm rounded"
                       style="width: 80px;"
                       min="1"
                       max="@TotalPaginas"
                       @bind="PaginaDeseada" />
                <button class="btn btn-outline-primary btn-sm ms-2 rounded-pill" @onclick="IrAPagina">
                    <i class="fas fa-arrow-right-to-bracket me-1"></i> Ir
                </button>
            </div>
        </div>
    </div>
</div>

@code {
    private QuoteSearchParams Filters = new();
    private PagedResult<QuoteDto>? PagedQuotes;
    private QuoteDto? SelectedQuote { get; set; }
    private bool IsLoading;
    private int PaginaDeseada = 1;

    private string activeTab = "Datos";

    private async Task Search()
    {
        try
        {
            IsLoading = true;
            PagedQuotes = await quoteService.SearchAsync(
                Filters.CustomerId, Filters.CustomerText,
                Filters.QuoteNumber,
                Filters.ProfessionalId, Filters.ProfessionalText,
                Filters.InstitutionId, Filters.InstitutionText,
                Filters.PatientId, Filters.PatientText, Filters.Status,
                Filters.IssueDateFrom, Filters.IssueDateTo,
                Filters.Page, Filters.PageSize);
            PaginaDeseada = Filters.Page;
        }
        catch (Exception ex)
        {
            toastService.ShowError(ex.Message);
        }
        finally
        {
            IsLoading = false;
        }
    }

    private void ToggleDetail(QuoteDto quote)
    {
        if (SelectedQuote?.Id == quote.Id)
            SelectedQuote = null;
        else
        {
            SelectedQuote = quote;
            activeTab = "Datos";
        }
    }

    private async Task PrimeraPagina() { Filters.Page = 1; await Search(); }
    private async Task UltimaPagina() { Filters.Page = TotalPaginas; await Search(); }
    private async Task SiguientePagina() => await CambiarPagina(1);
    private async Task AnteriorPagina() => await CambiarPagina(-1);
    private async Task CambiarPagina(int delta)
    {
        var nuevaPagina = Filters.Page + delta;
        if (nuevaPagina >= 1 && nuevaPagina <= TotalPaginas)
        {
            Filters.Page = nuevaPagina;
            await Search();
        }
    }
    private async Task IrAPagina()
    {
        if (PaginaDeseada >= 1 && PaginaDeseada <= TotalPaginas)
        {
            Filters.Page = PaginaDeseada;
            await Search();
        }
        else
        {
            toastService.ShowWarning("Número de página fuera de rango.");
        }
    }

    private bool PuedeRetroceder => PagedQuotes != null && Filters.Page > 1;
    private bool PuedeAvanzar => PagedQuotes != null && Filters.Page < TotalPaginas;
    private int TotalPaginas => PagedQuotes is null ? 1 :
        (int)Math.Ceiling((double)(PagedQuotes.TotalItems) / Filters.PageSize);

    private void Create()
    {
        Navigation.NavigateTo("/quote/create/");
    }
    private void OnClear()
    {
        Filters = new QuoteSearchParams();
        PagedQuotes = null;
        PaginaDeseada = 1;
    }
    private string GetStatusBadge(string status) => status switch
    {
        "Emitido" => "bg-primary text-white",
        "Aprobado" => "bg-success",
        "Despacho" => "bg-info text-white",
        "SinConsumo" => "bg-warning text-dark",
        "Transito" => "bg-secondary text-white",
        "Cerrado" => "bg-dark text-white",
        _ => "bg-light text-dark"
    };

    private void PrintPdf(int quoteId)
    {
        // lógica de impresión...
    }
}
