diff --git a/HerramientaCASA/Components/Pages/ConfiguracionPages/Enumeraciones.razor b/HerramientaCASA/Components/Pages/ConfiguracionPages/Enumeraciones.razor index 6fe8531..a51f400 100644 --- a/HerramientaCASA/Components/Pages/ConfiguracionPages/Enumeraciones.razor +++ b/HerramientaCASA/Components/Pages/ConfiguracionPages/Enumeraciones.razor @@ -52,7 +52,7 @@ - +
@@ -60,6 +60,14 @@ + + +
+ +
+
+
+
diff --git a/HerramientaCASA/Components/Pages/ConfiguracionPages/EnumeracionesTabs/CostesVariablesURBANConf.razor b/HerramientaCASA/Components/Pages/ConfiguracionPages/EnumeracionesTabs/CostesVariablesURBANConf.razor new file mode 100644 index 0000000..788babd --- /dev/null +++ b/HerramientaCASA/Components/Pages/ConfiguracionPages/EnumeracionesTabs/CostesVariablesURBANConf.razor @@ -0,0 +1,307 @@ +@inject NavigationManager Navigation +@layout ConfiguracionLayout +@using BlazorBootstrap +@using System.Globalization +@using bdHerramientaCACOA.db + + + + + +
+ ¿Estás seguro que desea eliminarlo? +
+
+ + + + +
+ + + + + +
+
+ + +
+
@GetExternalValidationMessage(nameof(descripcionItem))
+
+
+
+ + + +
+
+ +
+ + + + +
+
+ + + +
+ +
+

+ +
+ + + + + @context.ValorAlfabetico1 + + + @context.Orden + + +
+ + + + + + +
+
+
+
+
+ +@code { + + public List listadoCostesVariables { get; set; } = new List(); + + [SupplyParameterFromForm] + private enumeraciones itemSeleccionado { get; set; } = new enumeraciones(); + + private string tituloPopup = ""; + private Modal popupGestionDatos = default!; + private Modal popupConfirmarBorrado = default!; + + private bool nuevoElemento; + + private string descripcionItem = ""; + private int? ordenItem = 0; + + List mensajes = new List(); + + private EditContext? editContext; + private ValidationMessageStore? messageStore; + public tsHerramientasCACOA bd; + private string codigoEnumeracion = "CVURB"; + + + protected override async Task OnInitializedAsync() + { + bd = tsHerramientasCACOA.NuevoContexto(SoloLectura: false); + + editContext = new EditContext(itemSeleccionado); + messageStore = new ValidationMessageStore(editContext); + + listadoCostesVariables = UtilidadesCASA.devolverListadoOrdenadoEnumeracion(bd, codigoEnumeracion); + } + + + private async Task abrirPopupGestionDatos(enumeraciones objeto, bool esNuevo) + { + itemSeleccionado = objeto; + + if (esNuevo) + { + nuevoElemento = true; + descripcionItem = ""; + ordenItem = 0; + } + else + { + nuevoElemento = false; + descripcionItem = objeto.ValorAlfabetico1; + ordenItem = objeto.Orden; + } + + if (!nuevoElemento) + { + tituloPopup = "Modificar coste variable"; + } + else + { + tituloPopup = "Nuevo coste variable"; + } + + await popupGestionDatos.ShowAsync(); + } + private async Task cerrarPopupGestionDatos() + { + await popupGestionDatos.HideAsync(); + } + + private void ValidarYActualizar(ChangeEventArgs e, string fieldName) + { + var value = e.Value?.ToString(); + var field = new FieldIdentifier(itemSeleccionado, fieldName); + + switch (fieldName) + { + case nameof(descripcionItem): + if (!string.IsNullOrEmpty(value)) + { + + descripcionItem = value; + } + else + { + descripcionItem = ""; + } + break; + } + + messageStore?.Clear(field); + if (fieldName == nameof(descripcionItem) && descripcionItem == "") + { + messageStore?.Add(field, "La descripción no puede estar vacía."); + } + + editContext?.NotifyValidationStateChanged(); + } + + private void ValidarTodo() + { + ValidarYActualizar(new ChangeEventArgs { Value = descripcionItem }, nameof(descripcionItem)); + } + + private string GetExternalValidationMessage(string fieldName) + { + var field = new FieldIdentifier(itemSeleccionado, fieldName); + return messageStore?[field].FirstOrDefault() ?? string.Empty; + } + + private async Task GestionarDatos(string accion) + { + + var enumObtenido = bd.enumeraciones.Where(x => x.idEnumeracion == itemSeleccionado.idEnumeracion).FirstOrDefault(); + + itemSeleccionado.ValorAlfabetico1 = descripcionItem; + itemSeleccionado.Orden = ordenItem; + + switch (accion) + { + case "update": + UtilidadesCASA.guardarEnumeracion(bd, itemSeleccionado, codigoEnumeracion, nuevoElemento, enumObtenido); + + await cerrarPopupGestionDatos(); + + mensajes.Add(new ToastMessage + { + Type = ToastType.Primary, + Message = $"Actualizado correctamente.", + }); + + break; + + case "create": + UtilidadesCASA.guardarEnumeracion(bd, itemSeleccionado, codigoEnumeracion, nuevoElemento); + + await cerrarPopupGestionDatos(); + mensajes.Add(new ToastMessage + { + Type = ToastType.Primary, + Message = $"Guardado correctamente.", + }); + break; + + case "delete": + UtilidadesCASA.eliminarEnumeracion(bd, enumObtenido); + + await cerrarPopupConfirmarBorrado(); + mensajes.Add(new ToastMessage + { + Type = ToastType.Primary, + Message = $"Eliminado correctamente.", + }); + break; + } + bd.SaveChanges(); + listadoCostesVariables = UtilidadesCASA.devolverListadoOrdenadoEnumeracion(bd, codigoEnumeracion); + await InvokeAsync(StateHasChanged); + } + + + private async Task BorrarItem() + { + await GestionarDatos("delete"); + } + //////// PopUp eliminar registro + private async Task abrirPopupConfirmarBorrado(enumeraciones objeto) + { + itemSeleccionado = objeto; + await popupConfirmarBorrado.ShowAsync(); + } + private async Task cerrarPopupConfirmarBorrado() + { + await popupConfirmarBorrado.HideAsync(); + } + + // GUARDAR + private async Task guardarFormulario() + { + try + { + ValidarTodo(); + + if (!editContext!.GetValidationMessages().Any()) + { + string accion = nuevoElemento ? "create" : "update"; + await GestionarDatos(accion); + } + else + { + mensajes.Add(new ToastMessage + { + Type = ToastType.Warning, + Message = $"Debe rellenar los campos obligatorios.", + }); + } + + } + catch (Exception) + { + mensajes.Add(new ToastMessage + { + Type = ToastType.Danger, + Message = $"Error al guardar.", + }); + } + + } + + private void MostrarErrores() + { + messageStore?.Clear(); + foreach (var field in new[] { nameof(descripcionItem) }) + { + ValidarYActualizar(new ChangeEventArgs { Value = typeof(enumeraciones).GetProperty(field)?.GetValue(itemSeleccionado) }, field); + } + } + +} \ No newline at end of file