Files
Annamaria Sassi d3b2b9e329 Correzioni
2026-05-11 11:24:46 +02:00

248 lines
7.0 KiB
C#

using EgwCoreLib.Razor;
namespace Lux.UI.Components.Compo.Generic
{
public partial class GenClassMan
{
#region Public Properties
[Parameter]
public EventCallback<string> EC_Selected { get; set; }
[Parameter]
public EventCallback<bool> EC_Updated { get; set; }
[Parameter]
public List<GenClassModel> 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<GenClassModel> AllRecords = new List<GenClassModel>();
private int currPage = 1;
private GenClassModel? EditRecord = null;
private string ErrorMsg = "";
private bool isLoading = false;
private List<GenClassModel> ListRecords = new List<GenClassModel>();
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<bool, string> modalOpt = new Dictionary<bool, string>();
#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;
}
/// <summary>
/// Genera nuovo record e lo salva
/// </summary>
/// <exception cref="NotImplementedException"></exception>
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();
}
/// <summary>
/// Clona record
/// </summary>
/// <param name="curRec"></param>
private void DoClone(GenClassModel curRec)
{
}
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="selRec"></param>
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<bool>())
return;
// esegue eliminazione del record...
await GCService.DeleteAsync(selRec);
// segnalo update x reload
await EC_Updated.InvokeAsync(true);
}
/// <summary>
/// Edit articolo selezionato
/// </summary>
/// <param name="curRec"></param>
private Task DoEdit(GenClassModel curRec)
{
EditRecord = curRec;
SelRecord = curRec;
return EC_Selected.InvokeAsync(curRec.ClassCod);
}
/// <summary>
/// Reset selezione
/// </summary>
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);
}
/// <summary>
/// Selezione articolo x display info
/// </summary>
/// <param name="curRec"></param>
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}"
};
}
/// <summary>
/// Filtro e paginazione
/// </summary>
private void UpdateTable()
{
// fix paginazione
ListRecords = AllRecords
.OrderBy(x => x.ClassCod)
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
}
#endregion Private Methods
}
}