Files

279 lines
8.7 KiB
C#

using EgwCoreLib.Razor;
using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using GPW.CORE.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using NLog;
namespace GPW.CORE.ADM.Components.Compo
{
public partial class GruppiMan : IDisposable
{
#region Public Methods
public void Dispose()
{
AppMServ.EA_SearchUpdated -= AppMServ_EA_SearchUpdated;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected MessageService AppMServ { get; set; } = null!;
[Inject]
protected GpwDataService GDataServ { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected string CheckSel(AnagGruppiModel curItem)
{
string answ = "";
if (SelItem != null)
{
answ = curItem.Gruppo == SelItem.Gruppo ? "table-info" : "";
}
return answ;
}
protected async Task ClonaAssegnDip()
{
bool fatto = await GDataServ.Dipendenti2GrpClona(idxDipFrom, idxDipTo);
await ReloadData();
}
protected async Task CreateNew()
{
SelItem = new AnagGruppiModel()
{
Gruppo = "__Nuovo Gruppo",
DescrGruppo = $"Nuovo Gruppo - {DateTime.Now}",
CodExt = "",
ExportEnab = true
};
isEdit = true;
await InvokeAsync(StateHasChanged);
}
protected async void DoDelete(AnagGruppiModel selItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare il record?"))
return;
isLoading = true;
bool fatto = await GDataServ.AnagGruppiDelete(selItem);
await ReloadData();
await InvokeAsync(StateHasChanged);
}
protected void DoEdit(AnagGruppiModel? selItem)
{
isEdit = true;
SelItem = selItem;
}
protected void DoSelect(AnagGruppiModel? selItem)
{
isEdit = false;
SelItem = selItem;
}
protected async Task ForceReload(bool force)
{
SelItem = null;
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
CurrSearch = "";
AppMServ.SearchVal = "";
AppMServ.EA_SearchUpdated += AppMServ_EA_SearchUpdated;
numRecord = await AppMServ.NumRowGridGet(gridKey);
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected async Task SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
await AppMServ.NumRowGridSet(gridKey, newNum);
await InvokeAsync(ReloadData);
}
protected async Task SetPage(int newNum)
{
currPage = newNum;
await InvokeAsync(ReloadData);
}
protected async Task SortRequested(Sorter.SortCallBack e)
{
sortField = e.ParamName;
sortAsc = e.IsAscending;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private string gridKey = "GruppiMan";
private int idxDipFrom = 0;
private int idxDipTo = 0;
private bool isEdit = false;
private AnagGruppiModel? SelItem = null;
private bool sortAsc = true;
private string sortField = "";
#endregion Private Fields
#region Private Properties
private int currPage { get; set; } = 1;
private string CurrSearch { get; set; } = "";
private bool isLoading { get; set; } = false;
private List<DipendentiModel> ListDipendenti { get; set; } = new List<DipendentiModel>();
private List<AnagGruppiModel>? ListRecords { get; set; } = null;
private int numRecord { get; set; } = 10;
private List<AnagGruppiModel>? SearchRecords { get; set; } = null;
private int totalCount { get; set; } = 0;
#endregion Private Properties
#region Private Methods
private async void AppMServ_EA_SearchUpdated()
{
CurrSearch = AppMServ.SearchVal;
await ReloadData();
await InvokeAsync(StateHasChanged);
}
private void DoResetSelClona()
{
idxDipFrom = 0;
idxDipTo = 0;
}
private async Task ReloadData()
{
isLoading = true;
ListRecords = null;
ListDipendenti = await GDataServ.DipendentiGetAll();
try
{
SearchRecords = await GDataServ.AnagGruppiAll();
// verifico filtro per ricerca
if (!string.IsNullOrEmpty(CurrSearch))
{
SearchRecords = SearchRecords.Where(x => x.Gruppo.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase) || x.DescrGruppo.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase)).ToList();
}
}
catch (Exception ex)
{
Log.Error($"Eccezione in recupero dati{Environment.NewLine}{ex}");
}
totalCount = SearchRecords.Count;
SortTable();
isLoading = false;
}
private void SortTable()
{
if (SearchRecords != null)
{
// se ho ordinamento riordino...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "Descrizione":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.DescrGruppo).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.DescrGruppo).ToList();
}
break;
case "CodGruppo":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.Gruppo).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.Gruppo).ToList();
}
break;
case "CodExt":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.CodExt).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.CodExt).ToList();
}
break;
case "Export":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.ExportEnab).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.ExportEnab).ToList();
}
break;
case "IsActive":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.IsActive).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.IsActive).ToList();
}
break;
default:
break;
}
}
// filtro x display
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
else
{
ListRecords = new List<AnagGruppiModel>();
}
}
#endregion Private Methods
}
}