@page "/sales/customerform" @page "/sales/customerform/{CustomerId:int}" @inject HttpClient _httpClient @inject NavigationManager Navigation @inject IToastService toastService @inject AuthenticationStateProvider authenticationStateProvider
@foreach (var type in accountTypes) { }
@code { [Parameter] public int? CustomerId { get; set; } private ECustomer customer { get; set; } = new(); private List accountTypes = new(); private string returnUrl = "/sales/customers"; protected override async Task OnInitializedAsync() { // await LoadAccountTypes(); // 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() { var result = await _httpClient.GetFromJsonAsync>("/api/AccountType/GetAll"); accountTypes = result ?? new(); } 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); } }