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 AllRecords = new List(); protected List ListCostDriver = new List(); protected List ListRecords = new List(); protected List SearchRecords = new List(); #endregion Protected Fields #region Protected Properties [Inject] protected DataLayerServices DLService { get; set; } = null!; #endregion Protected Properties #region Protected Methods /// /// Reset selezione /// protected async void DoReset() { editRecord = null; selRecord = null; await ReloadBaseData(); ReloadData(); } [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; /// /// Selezione articolo x display info /// /// protected void DoSelect(ResourceModel curRec) { selRecord = curRec; } protected async Task DoDelete(ResourceModel rec2del) { if (!await JSRuntime.InvokeAsync("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("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(); 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 /// /// Check selezione riga item /// /// /// private string checkSel(ResourceModel curRec) { string answ = ""; if (selRecord != null) { answ = curRec.ResourceID == selRecord.ResourceID ? "table-info" : ""; } return answ; } /// /// Esegue gestione updated /// /// /// 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 } }