using EgwCoreLib.Razor; using k8s.Models; using MagMan.Core; 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; namespace MagMan.UI.Components { public partial class ProjectMan : IDisposable { #region Public Properties [Parameter] public int CustomerId { get; set; } = 0; [Parameter] public EventCallback E_ProjSel { 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.QueUpdProj.EA_NewMessage -= QueUpdProj_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!; [Inject] protected MTAdminService MTService { 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(ProjModel curItem) { string answ = ""; if (CurrItem != null) { answ = curItem.ProjDbId == CurrItem.ProjDbId ? "table-info" : ""; } else { answ = curItem.ProjDbId == ProjDbId ? "table-info" : ""; } return answ; } protected async Task DeleteRecord(ProjModel selItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record?")) return; await TService.ProjectDelete(KeyNum, selItem); await ReloadData(); } protected void DoSelect(ProjModel? selItem) { if (selItem != null) { ProjDbId = selItem.ProjDbId; } else { ProjDbId = 0; } E_ProjSel.InvokeAsync(selItem); } protected async Task ForceReload(bool force) { CurrItem = null; await ReloadData(); } private string gridKey = "ProjectMan"; protected override async Task OnParametersSetAsync() { await ReloadData(); } protected override async Task OnInitializedAsync() { currSearch = ""; AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated; AppMService.QueUpdProj.EA_NewMessage += QueUpdProj_EA_NewMessage; yLim = Configuration.GetValue("OptConf:projProgYLim"); rLim = Configuration.GetValue("OptConf:projProgRLim"); numRecord = await AppMService.NumRowGridGet(gridKey); } 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 ProjModel? CurrItem = null; private string currSearch = ""; private int filtType = 0; private List? ListRecords = null; private int machIdSel = 0; private List MachineList = new List(); private int ProjDbId = 0; private List? SearchRecords = null; private bool sortAsc = false; private string sortField = "DtLastAction"; #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 isLoading { get; set; } = false; private int MachineIdSel { get => machIdSel; set { if (machIdSel != value) { machIdSel = value; var pUpd = Task.Run(async () => { await ReloadData(); }); pUpd.Wait(); } } } private int numRecord { get; set; } = 10; private double rLim { get; set; } = 0.4; private double yLim { get; set; } = 0.8; #endregion Private Properties #region Private Methods private async void AppMService_EA_SearchUpdated() { currSearch = AppMService.SearchVal; await ReloadData(); } /// /// Gestione messaggio proj /// /// /// /// private async void QueUpdProj_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 if (KeyNum > 0 && currArgs.newMessage == $"{KeyNum}") { await ReloadData(); await InvokeAsync(StateHasChanged); } } } private async Task ReloadData() { isLoading = true; ListRecords = null; if (KeyNum > 0) { MachineList = await MTService.MachineGetByCustId(CustomerId); SearchRecords = await TService.ProjectGetByMachine(KeyNum, MachineIdSel); // verifico SE filtrare attivi if (OnlyActive) { SearchRecords = SearchRecords.Where(x => x.IsActive).ToList(); } // verifico se filtrare archiviati if (NotArchived) { SearchRecords = SearchRecords.Where(x => !x.IsArchived).ToList(); } // verifico filtro per ricerca if (!string.IsNullOrEmpty(currSearch)) { SearchRecords = SearchRecords .Where(x => x.ProjDescription.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase) || x.Machine.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase) || x.BTLFileName.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase)) .ToList(); } totalCount = SearchRecords.Count; } SortTable(); isLoading = false; } private bool onlyActive = true; protected string actMessage { get => onlyActive ? "Solo Attivi" : "Attivi + Inattivi"; } protected bool OnlyActive { get => onlyActive; set { if (onlyActive != value) { onlyActive = value; DoSelect(null); var pUpd = Task.Run(async () => { await ForceReload(true); }); pUpd.Wait(); } } } private bool notArchived = true; protected string archMessage { get => notArchived ? "Nascondi Archiviati" : "Mostra Tutti (+ Archiviati)"; } protected bool NotArchived { get => notArchived; set { if (notArchived != value) { notArchived = value; DoSelect(null); var pUpd = Task.Run(async () => { await ForceReload(true); }); pUpd.Wait(); } } } 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.ProjExtId).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ProjExtId).ToList(); } break; case "ProjDescription": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.ProjDescription).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ProjDescription).ToList(); } break; case "Machine": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Machine).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Machine).ToList(); } break; case "DtCreated": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.DtCreated).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.DtCreated).ToList(); } break; case "DtSchedule": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.DtSchedule).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.DtSchedule).ToList(); } break; case "DtStartProd": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.DtStartProd).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.DtStartProd).ToList(); } break; case "DtLastAction": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.DtLastAction).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.DtLastAction).ToList(); } break; case "ListName": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.ListName).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ListName).ToList(); } break; case "IsActive": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.IsActive).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.IsActive).ToList(); } break; case "IsArchived": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.IsArchived).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.IsArchived).ToList(); } break; case "ProcTimeEst": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.ProcTimeEst).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ProcTimeEst).ToList(); } break; case "ProgPerc": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.ProgPerc).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ProgPerc).ToList(); } break; case "ProjDbId": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.ProjDbId).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.ProjDbId).ToList(); } break; default: break; } } // filtro x display ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); } else { ListRecords = new List(); } } private string textCss(bool isActive) { return isActive ? "text-dark" : "text-secondary text-decoration-line-through"; } #endregion Private Methods } }