@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: Documentos Tipo: Seleccione @foreach (var tipo in documentTypes) { @tipo.Name } Número: Agregar @if (customer.PhSCustomerDocuments?.Any() == true) { Tipo Número @foreach (var doc in customer.PhSCustomerDocuments) { @documentTypes.FirstOrDefault(t => t.Id == doc.DocumenttypesId)?.Name @doc.DocumentNumber RemoveCustomerDocument(doc)">🗑 } } Direcciones @foreach (var address in customer.PhSCustomerAddresses) { OnCountryChanged(e, address)"> Seleccionar país @foreach (var country in countries) { @country } @if (address.Country == "Argentina") { Seleccionar provincia @foreach (var province in argentinaProvinces) { @province } } else { } RemoveAddress(address)"> Eliminar dirección } Agregar dirección @code { [Parameter] public int? CustomerId { get; set; } private ECustomer customer { get; set; } = new(); private List accountTypes = new(); private List taxConditions = new(); private List documentTypes = new(); private ECustomerDocument documentFormModel = new(); private ECustomerAddress AddressForModel = new(); private List addresses => customer.PhSCustomerAddresses.ToList(); private string returnUrl = "/sales/customers"; protected override async Task OnInitializedAsync() { await LoadAccountTypes(); await LoadTaxConditions(); await LoadDocumentTypes(); 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 LoadDocumentTypes() { var result = await _httpClient.GetFromJsonAsync>("/api/DocumentType/GetAll"); documentTypes = result ?? new(); } private void AddCustomerDocument() { if (!string.IsNullOrWhiteSpace(documentFormModel.DocumentNumber)) { customer.PhSCustomerDocuments.Add(documentFormModel); documentFormModel = new(); } } private void RemoveCustomerDocument(ECustomerDocument document) { customer.PhSCustomerDocuments.Remove(document); } private void AddCustomerAddress() { if (!string.IsNullOrWhiteSpace(AddressForModel.Streetaddress1)) { customer.PhSCustomerAddresses.Add(AddressForModel); AddressForModel = new(); // limpiar el formulario } } private void RemoveCustomerAddress(ECustomerAddress address) { customer.PhSCustomerAddresses.Remove(address); } private List countries = new() { "Argentina", "Brasil", "Chile", "Uruguay", "Paraguay", "Bolivia", "Perú", "Colombia", "Venezuela", "México", "Estados Unidos", "Canadá", "España", "Reino Unido", "Alemania", "Francia", "Italia", "China", "India", "Japón" }; private List argentinaProvinces = new() { "Buenos Aires", "Catamarca", "Chaco", "Chubut", "Córdoba", "Corrientes", "Entre Ríos", "Formosa", "Jujuy", "La Pampa", "La Rioja", "Mendoza", "Misiones", "Neuquén", "Río Negro", "Salta", "San Juan", "San Luis", "Santa Cruz", "Santa Fe", "Santiago del Estero", "Tierra del Fuego", "Tucumán", "Ciudad Autónoma de Buenos Aires" }; private void OnCountryChanged(ChangeEventArgs e, ECustomerAddress address) { address.Country = e.Value?.ToString(); if (address.Country != "Argentina") { address.Stateprovince = string.Empty; } } private void AddAddress() { customer.PhSCustomerAddresses.Add(new()); } private void RemoveAddress(ECustomerAddress address) { customer.PhSCustomerAddresses.Remove(address); } private void OnCountryChanged(ECustomerAddress address) { if (address.Country != "Argentina") { address.Stateprovince = string.Empty; } } 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); } }