using EgwCoreLib.Razor; 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 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; } #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 rowCss(AliasModel curItem) { string answ = curItem.Equals(CurrItem) ? "table-info" : ""; // verifico se attivo... if(!curItem.IsActive) { answ += " text-strike"; } return answ; } 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("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 void OnInitialized() { currSearch = ""; AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated; } protected override async Task OnParametersSetAsync() { await ReloadData(); } protected void SetNumRec(int newNum) { numRecord = newNum; currPage = 1; InvokeAsync(ReloadData); } protected void SetPage(int newNum) { currPage = newNum; DoSelect(null); 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 List? ListRecords = null; private int MaterialId = 0; private List? 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 Task ReloadData() { isLoading = true; await InvokeAsync(StateHasChanged); ListRecords = null; SearchRecords = await TService.AliasGetFilt(KeyNum, "MatCode"); // 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; await InvokeAsync(StateHasChanged); } 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(); } } #endregion Private Methods } }