Files
magman/MagMan.UI/Components/ResourcesAct.razor.cs
2024-07-17 08:13:11 +02:00

287 lines
8.5 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 ResourcesAct : IDisposable
{
#region Public Properties
[Parameter]
public int KeyNum { get; set; } = 0;
[Parameter]
public int ProjDbId { get; set; } = 0;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
AppMService.EA_SearchUpdated -= AppMService_EA_SearchUpdated;
AppMService.QueUpdInve.EA_NewMessage -= QueUpdInve_EA_NewMessage;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; } = null!;
[Inject]
protected IConfiguration Configuration { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected int totalCount { get; set; } = 0;
[Inject]
protected TenantService TService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected string CheckSel(ResourceExpDTO curItem)
{
string answ = "";
if (CurrItem != null)
{
answ = curItem.ResourceCloudId == CurrItem.ResourceCloudId ? "table-info" : "";
}
else
{
answ = curItem.ResourceCloudId == ResourceId ? "table-info" : "";
}
return answ;
}
protected void DoSelect(ResourceExpDTO? selItem)
{
if (selItem != null)
{
ResourceId = selItem.ResourceCloudId;
}
else
{
ResourceId = 0;
}
}
protected async Task ForceReload(bool force)
{
isLoading = true;
await Task.Delay(1);
CurrItem = null;
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
currSearch = "";
AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated;
AppMService.QueUpdInve.EA_NewMessage += QueUpdInve_EA_NewMessage; ;
numRecord = await AppMService.NumRowGridGet(gridKey);
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
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 ResourceExpDTO? CurrItem = null;
private string currSearch = "";
private int filtType = 0;
private string gridKey = "ResAct";
private List<ResourceExpDTO>? ListRecords = null;
private int ResourceId = 0;
private List<ResourceExpDTO>? SearchRecords = null;
private bool sortAsc = true;
private string sortField = "";
#endregion Private Fields
#region Private Properties
private int currPage { get; set; } = 1;
private int FiltType
{
get => filtType;
set
{
if (filtType != value)
{
filtType = value;
InvokeAsync(ReloadData);
InvokeAsync(StateHasChanged);
}
}
}
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 QueUpdInve_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;
if (KeyNum > 0 && ProjDbId > 0)
{
SearchRecords = await TService.ResourcesExpGetByProject(KeyNum, ProjDbId, false, false);
// verifico filtro per ricerca
if (!string.IsNullOrEmpty(currSearch))
{
SearchRecords = SearchRecords.Where(x => x.Note.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase)).ToList();
}
totalCount = SearchRecords.Count;
}
SortTable();
isLoading = false;
//await InvokeAsync(StateHasChanged);
}
private void SortTable()
{
if (SearchRecords != null)
{
// se ho ordinamento riordino...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "ID":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.ResourceCloudId).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.ResourceCloudId).ToList();
}
break;
case "DtRequest":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.DtRequest).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.DtRequest).ToList();
}
break;
case "Qty":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.Qty).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.Qty).ToList();
}
break;
case "Note":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.Note).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.Note).ToList();
}
break;
default:
break;
}
}
// filtro x display
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
else
{
ListRecords = new List<ResourceExpDTO>();
}
}
private string textCss(bool isActive)
{
return isActive ? "text-dark" : "text-secondary text-decoration-line-through";
}
#endregion Private Methods
}
}