Files
2024-07-17 08:13:11 +02:00

289 lines
8.4 KiB
C#

using EgwCoreLib.Razor;
using MagMan.Core;
using MagMan.Core.DTO;
using MagMan.Core.Services;
using MagMan.Data.Tenant.DbModels;
using MagMan.Data.Tenant.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace MagMan.UI.Components
{
public partial class AliasMan : IDisposable
{
#region Public Properties
[Parameter]
public int CustomerId { get; set; } = 0;
[Parameter]
public EventCallback<MaterialModel?> E_MaterialSel { get; set; }
[Parameter]
public int KeyNum { get; set; } = 0;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
AppMService.EA_SearchUpdated -= AppMService_EA_SearchUpdated;
AppMService.QueUpdAliasMat.EA_NewMessage -= QueUpdAliasMat_EA_NewMessage;
}
#endregion Public Methods
#region Protected Properties
protected string actMessage
{
get => onlyActive ? "Solo Attivi" : "Mostra Eliminati";
}
[Inject]
protected MessageService AppMService { get; set; } = null!;
[Inject]
protected IConfiguration Configuration { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected bool OnlyActive
{
get => onlyActive;
set
{
if (onlyActive != value)
{
onlyActive = value;
DoSelect(null);
var pUpd = Task.Run(async () =>
{
await ForceReload(true);
});
pUpd.Wait();
}
}
}
protected int totalCount { get; set; } = 0;
[Inject]
protected TenantService TService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected async Task CreateNew()
{
string dtCode = $"{DateTime.Now:yyMMdd_HHmmss}";
CurrItem = new AliasModel()
{
Family = "MatCode",
ValueOriginal = $"Orig_{dtCode}",
ValueAlias = $"Alias_{dtCode}"
};
await InvokeAsync(StateHasChanged);
}
protected async Task DeleteRecord(AliasModel selItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare il record?"))
return;
if (selItem != null)
{
bool fatto = await TService.AliasDelete(KeyNum, selItem);
}
await ReloadData();
}
protected void DoSelect(AliasModel? editRec)
{
CurrItem = editRec;
}
protected async Task ForceReload(bool force)
{
DoSelect(null);
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
currSearch = "";
AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated;
AppMService.QueUpdAliasMat.EA_NewMessage += QueUpdAliasMat_EA_NewMessage;
numRecord = await AppMService.NumRowGridGet(gridKey);
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected string rowCss(AliasModel curItem)
{
string answ = curItem.Equals(CurrItem) ? "table-info" : "";
// verifico se attivo...
if (!curItem.IsActive)
{
answ += " text-strike";
}
return answ;
}
protected async Task SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
await AppMService.NumRowGridSet(gridKey, newNum);
await InvokeAsync(ReloadData);
}
protected async Task SetPage(int newNum)
{
currPage = newNum;
DoSelect(null);
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 AliasModel? CurrItem = null;
private string currSearch = "";
private int filtType = 0;
private string gridKey = "AliasMan";
private List<AliasModel>? ListRecords = null;
private int MaterialId = 0;
private bool onlyActive = true;
private List<AliasModel>? SearchRecords = null;
private bool sortAsc = true;
private string sortField = "";
#endregion Private Fields
#region Private Properties
private int currPage { get; set; } = 1;
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 10;
#endregion Private Properties
#region Private Methods
private async void AppMService_EA_SearchUpdated()
{
currSearch = AppMService.SearchVal;
await ReloadData();
}
private async void QueUpdAliasMat_EA_NewMessage(object? sender, EventArgs e)
{
// verifico se il messaggio sia per la mia KEY e nel caso --> refresh!
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// se c'è un messaggio
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
// se è di mia pertinenza x key e Proj
if (KeyNum > 0 && currArgs.newMessage == $"{KeyNum}")
{
await ReloadData();
await InvokeAsync(StateHasChanged);
}
}
}
private async Task ReloadData()
{
isLoading = true;
ListRecords = null;
SearchRecords = await TService.AliasGetFilt(KeyNum, "MatCode");
// se voglio solo attivi --> filtro
if (onlyActive)
{
SearchRecords = SearchRecords
.Where(x => x.IsActive)
.ToList();
}
// verifico filtro per ricerca
if (!string.IsNullOrEmpty(currSearch))
{
SearchRecords = SearchRecords.Where(x => x.ValueAlias.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase) || x.ValueOriginal.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase)).ToList();
}
totalCount = SearchRecords.Count;
SortTable();
isLoading = false;
}
private void SortTable()
{
if (SearchRecords != null)
{
// se ho ordinamento riordino...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "ValOrig":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.ValueOriginal).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.ValueOriginal).ToList();
}
break;
case "ValAlias":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.ValueAlias).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.ValueAlias).ToList();
}
break;
default:
break;
}
}
// filtro x display
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
else
{
ListRecords = new List<AliasModel>();
}
}
#endregion Private Methods
}
}