Files
lux/Lux.UI/Components/Compo/JobTask/ResourcesMan.razor.cs
T
2025-11-12 19:15:28 +01:00

199 lines
5.5 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Cost;
using EgwCoreLib.Lux.Data.DbModel.Task;
using EgwCoreLib.Lux.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.JSInterop;
namespace Lux.UI.Components.Compo.JobTask
{
public partial class ResourcesMan
{
#region Public Properties
[Parameter]
public string CurrSearch { get; set; } = string.Empty;
#endregion Public Properties
#region Protected Fields
protected List<ResourceModel> AllRecords = new List<ResourceModel>();
protected List<CostDriverModel> ListCostDriver = new List<CostDriverModel>();
protected List<ResourceModel> ListRecords = new List<ResourceModel>();
protected List<ResourceModel> SearchRecords = new List<ResourceModel>();
#endregion Protected Fields
#region Protected Properties
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Reset selezione
/// </summary>
protected async void DoReset()
{
editRecord = null;
selRecord = null;
await ReloadBaseData();
ReloadData();
}
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
/// <summary>
/// Selezione articolo x display info
/// </summary>
/// <param name="curRec"></param>
protected void DoSelect(ResourceModel curRec)
{
selRecord = curRec;
}
protected async Task DoDelete(ResourceModel rec2del)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler eliminare il record {rec2del.Name}?"))
return;
isLoading = true;
// elimino e ricarico...
await DLService.ResourcesDeleteAsync(rec2del);
await ReloadBaseData();
ReloadData();
}
protected override async Task OnInitializedAsync()
{
await ReloadBaseData();
ReloadData();
}
protected override void OnParametersSet()
{
ReloadData();
}
protected async void DoAdd()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler aggiungere un nuovo record risorsa?"))
return;
isLoading = true;
var newRecord = new ResourceModel()
{
Name = $"Nuova Risorsa | {DateTime.Now:yyyy.MM.dd-HH.mm.ss}",
CostDriverID = 1
};
await DLService.ResourcesUpsertAsync(newRecord);
AllRecords = new List<ResourceModel>();
await Task.Delay(100);
await ReloadBaseData();
ReloadData();
}
#endregion Protected Methods
#region Private Fields
private int currPage = 1;
private ResourceModel? editRecord = null;
private bool isLoading = false;
private int numRecord = 10;
private ResourceModel? selRecord = null;
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
private string mainCss
{
get => selRecord == null ? "col-6" : "col-4";
}
#endregion Private Properties
#region Private Methods
/// <summary>
/// Check selezione riga item
/// </summary>
/// <param name="curRec"></param>
/// <returns></returns>
private string checkSel(ResourceModel curRec)
{
string answ = "";
if (selRecord != null)
{
answ = curRec.ResourceID == selRecord.ResourceID ? "table-info" : "";
}
return answ;
}
/// <summary>
/// Esegue gestione updated
/// </summary>
/// <param name="updRec"></param>
/// <returns></returns>
private async Task DoUpdate(ResourceModel? updRec)
{
var tmpSel = selRecord;
// se non nullo salvo!
if (updRec != null)
{
await DLService.ResourcesUpsertAsync(updRec);
}
await ReloadBaseData();
ReloadData();
selRecord = tmpSel;
}
private async Task ReloadBaseData()
{
AllRecords = await DLService.ResourcesGetAllAsync();
ListCostDriver = await DLService.CostDriverGetAllAsync();
}
private void ReloadData()
{
isLoading = true;
// se ho ricerca testuale faccio filtro ulteriore...
if (!string.IsNullOrEmpty(CurrSearch))
{
SearchRecords = AllRecords
.Where(x => x.Name.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase))
.ToList();
}
else
{
SearchRecords = AllRecords;
}
totalCount = SearchRecords.Count;
// fix paginazione
ListRecords = SearchRecords
.OrderBy(x => x.CodResource)
.ThenBy(x => x.Name)
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
}
#endregion Private Methods
}
}