// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this // file to you under the MIT license. using EgwCoreLib.Razor; using MagMan.Core; using MagMan.Core.DTO; using MagMan.Core.Services; using MagMan.Data.Admin.DbModels; using MagMan.Data.Admin.Services; using MagMan.Data.Tenant.DbModels; using MagMan.Data.Tenant.Services; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MimeKit.Text; namespace MagMan.UI.Components { public partial class MaterialMan : 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; AppMService.QueUpdAliasMat.EA_NewMessage -= QueUpdAliasMat_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 string remnMessage { get => showRemn ? "Acquisto + Spezzoni" : "Solo Acquisto"; //get => "Spezzoni";// showRemn ? "Mostra Spezzoni" : "Solo Acquisto"; } protected bool ShowRemn { get => showRemn; set { if (showRemn != value) { showRemn = 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 string CheckSel(MaterialDTO curItem) { string answ = ""; if (CurrItem != null) { answ = curItem.MatCloudId == CurrItem.MatId ? "table-info" : ""; } else { answ = curItem.MatCloudId == MaterialId ? "table-info" : ""; } return answ; } protected async Task CreateNew() { IsEdit = false; CurrItem = new MaterialModel() { MatCode = "", MatDesc = "" }; await InvokeAsync(StateHasChanged); } protected async Task DeleteRecord(MaterialDTO selItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record?")) return; MaterialId = 0; var rec2del = TService.MaterialFromDto(selItem); if (rec2del != null) { bool fatto = await TService.MaterialDelete(KeyNum, rec2del); } await ReloadData(); } protected void DoEdit(MaterialDTO? selItem) { IsEdit = true; if (selItem != null) { CurrItem = TService.MaterialFromDto(selItem); } DoSelect(null); } protected void DoSelect(MaterialDTO? selItem) { if (selItem != null) { MaterialId = selItem.MatCloudId; E_MaterialSel.InvokeAsync(TService.MaterialFromDto(selItem)); } else { MaterialId = 0; E_MaterialSel.InvokeAsync(null); } } protected async Task ForceReload(bool force) { CurrItem = null; DoEdit(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 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 MaterialModel? CurrItem = null; private string currSearch = ""; private int filtType = 0; private string gridKey = "MatMan"; private List? ListRecords = null; private int MaterialId = 0; private List? SearchRecords = null; private bool showRemn = false; 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; var pUpd = Task.Run(async () => { await ReloadData(); }); pUpd.Wait(); } } } private bool IsBeam { get => FiltType == 1; } private bool IsEdit { get; set; } = false; 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 bool hasDecimal(decimal num) { return (num - Math.Round(num) != 0); } 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.MaterialDtoGetAll(KeyNum, false, showRemn); // verifico se filtrare x beam/wall if (FiltType > 0) { SearchRecords = SearchRecords.Where(x => (x.IsBeam && FiltType == 1) || (x.IsWall && FiltType == 2)).ToList(); } // verifico filtro per ricerca if (!string.IsNullOrEmpty(currSearch)) { SearchRecords = SearchRecords.Where(x => x.MatCode.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase) || x.MatDesc.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 "MatId": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.MatCloudId).ThenBy(x => x.MatCode).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.MatCloudId).ThenByDescending(x => x.MatCode).ToList(); } break; case "MatCode": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.MatCode).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.MatCode).ToList(); } break; case "MatDesc": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.MatDesc).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.MatDesc).ToList(); } break; case "W": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.WMm).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.WMm).ToList(); } break; case "H": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.HMm).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.HMm).ToList(); } break; case "L": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.LMm).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.LMm).ToList(); } break; case "SizeNum": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.SizeNum).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.SizeNum).ToList(); } break; case "QtyTot": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.QtyTot).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.QtyTot).ToList(); } break; default: break; } } // filtro x display ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); } else { ListRecords = new List(); } } #endregion Private Methods } }