Leandro Hernan Rojas 0177366fc9
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m4s
Update Customers Update Create and List
2025-04-12 18:51:25 -03:00

151 lines
5.7 KiB
Plaintext

@page "/sales/customers"
@using phronCare.UIBlazor.Services.Sales
@using phronCare.UIBlazor.Data
@using Domain.Entities
@using Domain.Generics
@inject NavigationManager Navigation
@inject CustomerService customerService
<div class="card " style="zoom:90%">
<div class="card-header">
<h3 class="card-title">Listado de clientes</h3> @* wtf? *@
</div>
<div class="card-body" style="zoom:70%;">
<div class="mb-4 space-y-2">
<input @bind="SearchParams.Name" placeholder="Nombre" class="border rounded p-1 w-full" />
<input @bind="SearchParams.Email" placeholder="Email" class="border rounded p-1 w-full" />
<input @bind="SearchParams.Document" placeholder="Documento" class="border rounded p-1 w-full" />
<button class="btn btn-primary" @onclick="BuscarClientes">Buscar</button>
</div>
@if (TablaClientes != null && TablaClientes.Any())
{
<PhTable Columns="TableColumns"
Data="TablaClientes"
SelectionField="Id"
RowsPerPage=SearchParams.PageSize
RenderButtons="true" Buttons="botones"
ShowPageButtons="false"
ShowQuickSearch="false"
RenderSelect="false"
/>
<div class="mt-4 flex justify-between items-center">
<button class="btn btn-secondary" @onclick="AnteriorPagina" disabled="@(!PuedeRetroceder)">Anterior</button>
<span> Página @SearchParams.Page de @TotalPaginas </span>
<button class="btn btn-secondary" @onclick="SiguientePagina" disabled="@(!PuedeAvanzar)">Siguiente</button>
</div>
}
else
{
<p>No hay resultados.</p>
}
</div>
<div class="card-footer">
<div class="row">
<div class="col">
<button type="button" class="btn btn-success" @onclick="XSLXExportar">
<i class="fas fa-file-excel"></i> Exportar a Excel
</button>
<button type="button" class="btn btn-secondary" @onclick="Cancel">Volver</button>
</div>
</div>
</div>
</div>
@code {
private CustomerSearchParams SearchParams = new();
private PagedResult<ECustomer>? PagedResult;
private List<Dictionary<string, object>> TablaClientes = new();
private List<string> TableColumns = new()
{
"Id", "Nombre", "Activo", "Crédito", "Límite",
"Email", "Teléfono", "Dirección", "Documento"
};
private async Task BuscarClientes()
{
SearchParams.Page = 1;
await CargarClientes();
}
private async Task CargarClientes()
{
PagedResult = await customerService.SearchCustomersAsync(SearchParams);
if (PagedResult?.Items is not null)
{
TablaClientes = PagedResult.Items.Select(c =>
{
var addr = c.PhSCustomerAddresses.FirstOrDefault();
var doc = c.PhSCustomerDocuments.FirstOrDefault();
return new Dictionary<string, object>
{
{ "Id", c.Id },
{ "Nombre", c.Name ?? string.Empty },
{ "Activo", c.Active ? "Sí" : "No" },
{ "Crédito", c.HasCreditAccount ? "Sí" : "No" },
{ "Límite", c.CreditLimit },
{ "Email", addr?.Email ?? string.Empty },
{ "Teléfono", addr?.Phonenumber ?? string.Empty },
{ "Dirección", addr is not null
? $"{addr.Streetaddress1}, {addr.City}, {addr.Postalcode}"
: string.Empty },
{ "Documento", doc?.DocumentNumber ?? string.Empty }
};
}).ToList();
}
}
private async Task SiguientePagina() => await CambiarPagina(1);
private async Task AnteriorPagina() => await CambiarPagina(-1);
private async Task CambiarPagina(int delta)
{
var nuevaPagina = SearchParams.Page + delta;
if (nuevaPagina >= 1 && nuevaPagina <= TotalPaginas)
{
SearchParams.Page = nuevaPagina;
await CargarClientes();
}
}
private async Task XSLXExportar()
{
// string endpoint = "/api/Ticket/ExportDashboardDetail";
// var response = await _httpClient.PostAsJsonAsync(endpoint, new { Param1 = Group, Param2 = "ASC" });
// response.EnsureSuccessStatusCode();
// var fileBytes = await response.Content.ReadAsByteArrayAsync();
// var currentDate = DateTime.Now.ToString("ddMMyyyyhhmmss");
// var filename = $"Tickets_{Group}_{currentDate}.xlsx";
// await js.InvokeAsync<object>("saveAsFile", filename, Convert.ToBase64String(fileBytes));
}
List<PhTable.ButtonOptions> botones = new();
protected override void OnInitialized()
{
botones = new List<PhTable.ButtonOptions>
{
new PhTable.ButtonOptions
{
Caption = "Editar",
ElementClass = "btn btn-primary btn-sm",
UrlAction = "/sales/customers/edit/",
OnClickAction = async (id) =>
{
if (int.TryParse(id, out var customerId))
{
Navigation.NavigateTo($"/sales/customerform/{customerId}");
}
}
}
};
}
private int TotalPaginas => PagedResult is null ? 1 :
(int)Math.Ceiling((double)(PagedResult.TotalItems) / SearchParams.PageSize);
private bool PuedeAvanzar => PagedResult != null && SearchParams.Page < TotalPaginas;
private bool PuedeRetroceder => PagedResult != null && SearchParams.Page > 1;
public void Cancel()
{
Navigation.NavigateTo("/DashboardPanel");
}
}