@page "/sales/productform" @page "/sales/productform/{ProductId:int?}" @using System.ComponentModel.DataAnnotations @using phronCare.UIBlazor.Services.Sales @using phronCare.UIBlazor.Pages.Shared.Modals @inject ProductService ProductService @inject ProductCategoryService ProductCategoryService @inject BusinessUnitService BusinessUnitService @inject IToastService ToastService @inject NavigationManager Navigation @inject IModalService Modal

@((ProductId.HasValue ? "Editar producto" : "Nuevo producto"))

@foreach (var cat in _productCategories) { }
@foreach (var unit in _businessUnits) { }
@code { [Parameter] public int? ProductId { get; set; } [Parameter] public string? returnUrl { get; set; } = "/sales/products"; private EProduct _model = new(); private List _productCategories = new(); private List _businessUnits = new(); protected override async Task OnInitializedAsync() { _productCategories = await ProductCategoryService.GetAllAsync(); _businessUnits = await BusinessUnitService.GetAllAsync(); if (ProductId.HasValue) { _model = await ProductService.GetByIdAsync(ProductId.Value); } } private async Task HandleValidSubmit() { var parameters = new ModalParameters(); parameters.Add("Message", "¿Desea guardar los cambios del producto?"); var modal = Modal.Show("Confirmación", parameters); var result = await modal.Result; if (result.Cancelled) return; try { HttpResponseMessage response; if (_model.Id == 0) response = await ProductService.CreateAsync(_model); else response = await ProductService.UpdateAsync(_model); if (response.IsSuccessStatusCode) { ToastService.ShowSuccess("Producto guardado correctamente."); NavigateBack(); } else { var error = await response.Content.ReadAsStringAsync(); ToastService.ShowError($"Error: {error}"); } } catch (Exception ex) { ToastService.ShowError($"Error: {ex.Message}"); } } private void NavigateBack() { Navigation.NavigateTo(returnUrl ?? "/sales/products"); } }