using EgwCoreLib.Razor; using k8s.Models; 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 { #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 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(); } 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 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 = 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 MachineIdSel { get => machIdSel; set { if (machIdSel != value) { machIdSel = value; InvokeAsync(ReloadData); } } } 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; if (KeyNum > 0) { MachineList = await MTService.MachineGetByCustId(CustomerId); SearchRecords = await TService.ProjectGetByMachine(KeyNum, MachineIdSel); // verifico filtro per ricerca if (!string.IsNullOrEmpty(currSearch)) { SearchRecords = SearchRecords.Where(x => x.ProjDescription.Contains(currSearch, StringComparison.InvariantCultureIgnoreCase) || x.BTLFileName.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.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; 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 } }