@page "/clientes"
@attribute [Authorize]
@inject ICustomerService CustomerSvc
@inject IDialogService DialogSvc
@inject ISnackbar Snackbar
Clientes - AutoTaller ERP
@total cliente(s)
Código
Tipo
Nombre / Razón social
Documento
Contacto
Vehículos
Saldo
Estado
Acciones
@context.Codigo
@if (context.Tipo == TipoCliente.Empresa)
{
Empresa
}
else
{
Persona
}
@context.NombreRazonSocial
@context.DocumentoIdentidad
@if (!string.IsNullOrEmpty(context.Email))
{
@context.Email
}
@if (!string.IsNullOrEmpty(context.TelefonoPrincipal))
{
@context.TelefonoPrincipal
}
@context.CantidadVehiculos
@if (context.SaldoPendiente > 0)
{
S/. @context.SaldoPendiente.ToString("N2")
}
else
{
S/. 0.00
}
@if (context.Activo)
{
Activo
}
else
{
Inactivo
}
No se encontraron clientes. Crear el primero
@if (totalPages > 1)
{
}
@code {
private string? buscar;
private List clientes = new();
private bool cargando = true;
private int page = 1;
private const int pageSize = 10;
private int total = 0;
private int totalPages = 0;
private CancellationTokenSource? _debounceCts;
protected override async Task OnInitializedAsync() => await CargarAsync();
private async Task CargarAsync()
{
cargando = true;
var resultado = await CustomerSvc.ListarClientesAsync(buscar, page, pageSize);
clientes = resultado.Items;
total = resultado.Total;
totalPages = resultado.TotalPages;
cargando = false;
}
private async Task OnBuscarChanged()
{
_debounceCts?.Cancel();
_debounceCts = new CancellationTokenSource();
var token = _debounceCts.Token;
try
{
await Task.Delay(350, token);
page = 1;
await CargarAsync();
}
catch (TaskCanceledException) { }
}
private async Task OnPageChanged(int p)
{
page = p;
await CargarAsync();
}
private async Task NuevoCliente()
{
var parametros = new DialogParameters { ["Cliente"] = new ClienteFormDto() };
var opciones = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true };
var dialog = await DialogSvc.ShowAsync("Nuevo cliente", parametros, opciones);
var resultado = await dialog.Result;
if (resultado is { Canceled: false })
{
Snackbar.Add("Cliente creado correctamente", Severity.Success);
await CargarAsync();
}
}
private async Task EditarCliente(int id)
{
var dto = await CustomerSvc.ObtenerClienteAsync(id);
if (dto == null) return;
var parametros = new DialogParameters { ["Cliente"] = dto };
var opciones = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true };
var dialog = await DialogSvc.ShowAsync("Editar cliente", parametros, opciones);
var resultado = await dialog.Result;
if (resultado is { Canceled: false })
{
Snackbar.Add("Cliente actualizado", Severity.Success);
await CargarAsync();
}
}
private async Task EliminarCliente(ClienteListDto cliente)
{
var confirmar = await DialogSvc.ShowMessageBox(
"Confirmar",
$"¿Desactivar al cliente '{cliente.NombreRazonSocial}'? Esta acción puede revertirse desde la base de datos.",
yesText: "Sí, desactivar", cancelText: "Cancelar");
if (confirmar == true)
{
try
{
await CustomerSvc.EliminarClienteAsync(cliente.Id);
Snackbar.Add("Cliente desactivado", Severity.Info);
await CargarAsync();
}
catch (Exception ex)
{
Snackbar.Add($"Error: {ex.Message}", Severity.Error);
}
}
}
}