Files
lux/Lux.UI/Components/Compo/Templates/TemplateRowList.razor.cs
T
2026-03-05 19:41:24 +01:00

219 lines
6.5 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Items;
using EgwCoreLib.Lux.Data.DbModel.Sales;
using EgwCoreLib.Lux.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Lux.UI.Components.Compo.Templates
{
public partial class TemplateRowList
{
#region Public Properties
/// <summary>
/// Modello dell'offerta corrente (bindato dal genitore).
/// </summary>
[Parameter]
public TemplateModel CurrParent { get; set; } = null!;
/// <summary>
/// Ricerca libera da parent
/// </summary>
[Parameter]
public string CurrSearchVal { get; set; } = "";
/// <summary>
/// Callback invocato al select
/// </summary>
[Parameter]
public EventCallback<TemplateRowModel?> EC_Selected { get; set; }
#endregion Public Properties
#region Protected Methods
protected override async Task OnParametersSetAsync()
{
await ReloadData();
UpdateTable();
}
#endregion Protected Methods
#region Private Fields
private List<TemplateRowModel> AllRecords = new List<TemplateRowModel>();
private int currPage = 1;
private TemplateRowModel? EditRecord = null;
private bool isLoading = false;
private List<TemplateRowModel> ListFilt = new List<TemplateRowModel>();
private List<TemplateRowModel> ListRecords = new List<TemplateRowModel>();
private List<SellingItemModel> ListSellItems = new List<SellingItemModel>();
private int numRecord = 10;
private TemplateRowModel? SelRecord = null;
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Servizi di accesso ai dati iniettati dal framework.
/// </summary>
[Inject]
private DataLayerServices DLService { get; set; } = null!;
[Inject]
private IJSRuntime JSRuntime { get; set; } = null!;
#endregion Private Properties
#region Private Methods
private string CheckSelect(TemplateRowModel curRec)
{
string answ = "";
if (SelRecord != null)
{
answ = curRec.TemplateRowID == SelRecord.TemplateRowID ? "table-info" : "";
}
return answ;
}
private void DoAdd()
{
// valori helper
DateTime adesso = DateTime.Now;
int newRow = 1 + (AllRecords.Count > 0 ? AllRecords.Max(x => x.RowNum) : 0);
EditRecord = new TemplateRowModel()
{
TemplateID = CurrParent.TemplateID,
Envir = CurrParent.Envir,
Name = $"New Template ITEM {adesso.Year}",
RowNum = newRow,
SellingItemID = CurrSellingItemId
};
}
private async Task DoClone(TemplateRowModel rec2clone)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler duplicare interamente il template selezionato?{Environment.NewLine}---------------------------{Environment.NewLine}Env: {rec2clone.Envir} | {rec2clone.Name}"))
return;
// clona intera offerta + tutte le righe...
rec2clone.Name += " - clone";
await DLService.TemplateRowCloneAsync(rec2clone);
await ReloadData();
UpdateTable();
}
private void DoClose()
{
SelRecord = null;
EditRecord = null;
}
private async Task DoDelete(TemplateRowModel curRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler eliminare il template selezionato?{Environment.NewLine}---------------------------{Environment.NewLine}Env: {curRec.Envir} | {curRec.Name}"))
return;
// clona intera offerta + tutte le righe...
await DLService.TemplateRowDeleteAsync(curRec);
await ReloadData();
UpdateTable();
}
private void DoEdit(TemplateRowModel curRec)
{
EditRecord = curRec;
}
private async Task DoReset()
{
EditRecord = null;
SelRecord = null;
await ReloadData();
UpdateTable();
await EC_Selected.InvokeAsync(null);
}
/// <summary>
/// Salva record ed avanza compilazione
/// </summary>
/// <param name="updRec"></param>
/// <returns></returns>
private async Task DoSave(TemplateRowModel updRec)
{
// salvo record
await DLService.TemplateRowUpsertAsync(updRec);
EditRecord = null;
SelRecord = null;
await ReloadData();
UpdateTable();
}
private Task DoSelect(TemplateRowModel curRec)
{
SelRecord = curRec;
return EC_Selected.InvokeAsync(curRec);
}
private async Task ReloadData()
{
// leggo i selling items
ListSellItems = await DLService.SellingItemsByEnvirAsync(CurrParent.Envir);
// leggo i dati principali
if (CurrParent != null)
{
AllRecords = await DLService.TemplateRowByParentAsync(CurrParent.TemplateID);
}
else
{
AllRecords.Clear();
}
}
private int CurrSellingItemId = 0;
private void SaveNumRec(int newNum)
{
numRecord = newNum;
UpdateTable();
}
private void SavePage(int newNum)
{
currPage = newNum;
UpdateTable();
}
private void UpdateTable()
{
//ListFilt.Clear();
ListFilt = AllRecords;
if (!string.IsNullOrEmpty(CurrSearchVal))
{
ListFilt = AllRecords
.Where(x => x.Name.Contains(CurrSearchVal, StringComparison.InvariantCultureIgnoreCase) || x.TemplateRowUID.Contains(CurrSearchVal, StringComparison.InvariantCultureIgnoreCase))
.ToList();
}
totalCount = ListFilt.Count;
// gestione paginazione
ListRecords = ListFilt
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
#endregion Private Methods
}
}