using EgwCoreLib.Razor; namespace Lux.UI.Components.Compo.Generic { public partial class GenClassMan { #region Public Properties [Parameter] public EventCallback EC_Selected { get; set; } [Parameter] public EventCallback EC_Updated { get; set; } [Parameter] public List ListGenClass { get; set; } = null!; [Parameter] public string SearchVal { get; set; } = ""; #endregion Public Properties #region Protected Methods protected override void OnParametersSet() { ReloadData(); UpdateTable(); } #endregion Protected Methods #region Private Fields private bool AddVisible = false; private List AllRecords = new List(); private int currPage = 1; private GenClassModel? EditRecord = null; private string ErrorMsg = ""; private bool isLoading = false; private List ListRecords = new List(); private GenClassModel? NewRecord = null; private int numRecord = 10; private GenClassModel? SelRecord = null; private int totalCount = 0; private BootstrapModal Modal = new(); private string mTitle = ""; private string mMessage = ""; private BootstrapModal.ModalMode mMode = BootstrapModal.ModalMode.ND; private Dictionary modalOpt = new Dictionary(); #endregion Private Fields #region Private Properties [Inject] private IGenClassService GCService { get; set; } = null!; [Inject] private IJSRuntime JSRuntime { get; set; } = null!; #endregion Private Properties #region Private Methods private string checkSel(GenClassModel curRec) { string answ = ""; if (SelRecord != null) { answ = curRec.ClassCod == SelRecord.ClassCod ? "table-info" : ""; } return answ; } /// /// Genera nuovo record e lo salva /// /// private async Task DoAdd() { if (NewRecord != null) { // verifico non sia duplicato... var recExist = AllRecords.Count(x => x.ClassCod == NewRecord.ClassCod); if (recExist > 0) { ErrorMsg = $"Errore: record già esistente con codice {NewRecord.ClassCod}"; } else { ErrorMsg = ""; // faccio upsert record... await GCService.UpsertAsync(NewRecord); // segnalo update x reload await EC_Updated.InvokeAsync(true); NewRecord = null; } } } private void DoCancel() { ResetEdit(); UpdateTable(); } /// /// Clona record /// /// private void DoClone(GenClassModel curRec) { } /// /// Eliminazione record /// /// private async Task DoDelete(GenClassModel selRec) { // controlo che NON abbia child obj... altrimenti esco if (selRec.NumChild > 0) return; mTitle = "Attenzione"; mMessage = "Sicuro di voler eliminare il record?\n" + $"Dettagli: {selRec.ClassCod} | {selRec.Description}"; mMode = BootstrapModal.ModalMode.Confirm; modalOpt = new(); modalOpt.Add(true, "Si"); modalOpt.Add(false, "No"); if (!await Modal!.ShowAsync()) return; // esegue eliminazione del record... await GCService.DeleteAsync(selRec); // segnalo update x reload await EC_Updated.InvokeAsync(true); } /// /// Edit articolo selezionato /// /// private Task DoEdit(GenClassModel curRec) { EditRecord = curRec; SelRecord = curRec; return EC_Selected.InvokeAsync(curRec.ClassCod); } /// /// Reset selezione /// private Task DoReset() { EditRecord = null; SelRecord = null; return EC_Selected.InvokeAsync(""); } private async Task DoSave(GenClassModel currRec) { // faccio upsert record... await GCService.UpsertAsync(currRec); // segnalo update x reload await EC_Updated.InvokeAsync(true); } /// /// Selezione articolo x display info /// /// private Task DoSelect(GenClassModel curRec) { SelRecord = curRec; return EC_Selected.InvokeAsync(curRec.ClassCod); } private void ReloadData() { isLoading = true; AllRecords = ListGenClass; // se ho ricerca testuale faccio filtro ulteriore... if (!string.IsNullOrEmpty(SearchVal)) { AllRecords = AllRecords .Where(x => x.ClassCod.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase)) .ToList(); } totalCount = AllRecords.Count; } private void ResetEdit() { // reset edit EditRecord = null; ReloadData(); } private void SaveNumRec(int newNum) { numRecord = newNum; UpdateTable(); } private void SavePage(int newNum) { currPage = newNum; UpdateTable(); } private void ToggleAdd() { AddVisible = !AddVisible; NewRecord = new GenClassModel() { ClassCod = "NewClass", Description = $"Descrizione-{DateTime.Now:yyyy.MM.dd-HH.mm.ss}" }; } /// /// Filtro e paginazione /// private void UpdateTable() { // fix paginazione ListRecords = AllRecords .OrderBy(x => x.ClassCod) .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); isLoading = false; } #endregion Private Methods } }