@page "/sales/customerform" @page "/sales/customerform/{CustomerId:int}" @using phronCare.UIBlazor.Services.Sales @inject HttpClient _httpClient @inject NavigationManager Navigation @inject IToastService toastService @inject AuthenticationStateProvider authenticationStateProvider @inject AccountTypeService accountTypeService @inject TaxConditionService taxConditionService Nombre / Razón Social: Sucursal / Nombre Comercial: Tipo de Cuenta: -- Seleccionar -- @foreach (var type in accountTypes) { @type.Name } Condición Fiscal: -- Seleccionar -- @foreach (var tax in taxConditions) { @tax.Description } ¿Cuenta Corriente? Límite de Crédito: Activo: Código Externo (opcional): @code { [Parameter] public int? CustomerId { get; set; } private ECustomer customer { get; set; } = new(); private List accountTypes = new(); private List taxConditions = new(); private string returnUrl = "/sales/customers"; protected override async Task OnInitializedAsync() { await LoadAccountTypes(); await LoadTaxConditions(); if (CustomerId.HasValue) { // Cargar datos del cliente existente desde la API customer = await _httpClient.GetFromJsonAsync($"/api/Customer/GetById/{CustomerId.Value}") ?? new(); } } private async Task LoadAccountTypes() { accountTypes = await accountTypeService.GetAllAsync(); } private async Task LoadTaxConditions() { taxConditions = await taxConditionService.GetAllAsync(); } private async Task HandleValidSubmit() { try { HttpResponseMessage response; if (CustomerId.HasValue) { response = await _httpClient.PutAsJsonAsync("/api/Customer/Update", customer); } else { response = await _httpClient.PostAsJsonAsync("/api/Customer/Create", customer); } if (response.IsSuccessStatusCode) { toastService.ShowSuccess("Cliente guardado exitosamente"); Navigation.NavigateTo(returnUrl); } else { var error = await response.Content.ReadAsStringAsync(); toastService.ShowError($"Error: {error}"); } } catch (Exception ex) { toastService.ShowError($"Error: {ex.Message}"); } } private void Cancel() { Navigation.NavigateTo(returnUrl); } }