Files
lux/Lux.UI/Components/Compo/Generic/GenClassMan.razor.cs
T
2026-03-24 16:38:41 +01:00

241 lines
6.7 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Utils;
using EgwCoreLib.Lux.Data.Services.Utils;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
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 Fields
protected List<GenClassModel> AllRecords = new List<GenClassModel>();
protected List<GenClassModel> ListRecords = new List<GenClassModel>();
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IGenClassService GCService { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Genera nuovo record e lo salva
/// </summary>
/// <exception cref="NotImplementedException"></exception>
protected 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;
}
}
}
protected void DoCancel()
{
ResetEdit();
UpdateTable();
}
/// <summary>
/// Clona record
/// </summary>
/// <param name="curRec"></param>
protected void DoClone(GenClassModel curRec)
{
}
/// <summary>
/// Eliminazione record
/// </summary>
/// <param name="selRec"></param>
protected async Task DoDelete(GenClassModel selRec)
{
// controlo che NON abbia child obj... altrimenti esco
if (selRec.NumChild > 0)
return;
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler eliminare il record? Dettagli: {selRec.ClassCod} | {selRec.Description}"))
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>
protected Task DoEdit(GenClassModel curRec)
{
EditRecord = curRec;
SelRecord = curRec;
return EC_Selected.InvokeAsync(curRec.ClassCod);
}
/// <summary>
/// Reset selezione
/// </summary>
protected Task DoReset()
{
EditRecord = null;
SelRecord = null;
return EC_Selected.InvokeAsync("");
}
protected 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>
protected Task DoSelect(GenClassModel curRec)
{
SelRecord = curRec;
return EC_Selected.InvokeAsync(curRec.ClassCod);
}
protected override void OnParametersSet()
{
ReloadData();
UpdateTable();
}
protected void SaveNumRec(int newNum)
{
numRecord = newNum;
UpdateTable();
}
protected void SavePage(int newNum)
{
currPage = newNum;
UpdateTable();
}
protected void ToggleAdd()
{
AddVisible = !AddVisible;
NewRecord = new GenClassModel()
{
ClassCod = "NewClass",
Description = $"Descrizione-{DateTime.Now:yyyy.MM.dd-HH.mm.ss}"
};
}
#endregion Protected Methods
#region Private Fields
private bool AddVisible = false;
private int currPage = 1;
private GenClassModel? EditRecord = null;
private string ErrorMsg = "";
private bool isLoading = false;
private GenClassModel? NewRecord = null;
private int numRecord = 10;
private GenClassModel? SelRecord = null;
private int totalCount = 0;
#endregion Private Fields
#region Private Methods
private string checkSel(GenClassModel curRec)
{
string answ = "";
if (SelRecord != null)
{
answ = curRec.ClassCod == SelRecord.ClassCod ? "table-info" : "";
}
return answ;
}
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();
}
/// <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
}
}