using GWMS.Data; using GWMS.Data.DatabaseModels; using GWMS.Data.DTO; using GWMS.UI.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace GWMS.UI.Pages { [Authorize(Roles = "SuperAdmin, Admin, User")] public partial class UserManager : ComponentBase, IDisposable { #region Private Fields private UserModel currRecord = null; private List ListRecords; private List PlantsList; private List SearchRecords; private List SuppliersList; private List TransportersList; #endregion Private Fields #region Private Properties private int _currPage { get; set; } = 1; private int _numRecord { get; set; } = 10; private UserLevel _selLevel { get; set; } = UserLevel.ND; private int _selPlantId { get; set; } = 0; private int _selSuppId { get; set; } = 0; private int _selTraspId { get; set; } = 0; private int currPage { get => _currPage; set { if (_currPage != value) { _currPage = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private bool isLoading { get; set; } = false; private int numRecord { get => _numRecord; set { if (_numRecord != value) { _numRecord = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private UserLevel SelLevel { get => _selLevel; set { if (_selLevel != value) { _selLevel = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int SelPlantId { get => _selPlantId; set { if (_selPlantId != value) { _selPlantId = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int SelSupplierId { get => _selSuppId; set { if (_selSuppId != value) { _selSuppId = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int SelTransporterId { get => _selTraspId; set { if (_selTraspId != value) { _selTraspId = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } #endregion Private Properties #region Protected Properties [Inject] protected MessageService AppMService { get; set; } [Inject] protected GWMSDataService DataService { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected NavigationManager NavManager { get; set; } protected int totalCount { get { int answ = 0; if (SearchRecords != null) { answ = SearchRecords.Count; } return answ; } } #endregion Protected Properties #region Private Methods private async Task ReloadData() { isLoading = true; SearchRecords = await DataService.UsersGetFilt(SelLevel, SelPlantId, SelSupplierId, SelTransporterId); ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); isLoading = false; } #endregion Private Methods #region Protected Methods protected void Edit(UserModel selRecord) { currRecord = selRecord; } protected void ForceReload(int newNum) { numRecord = newNum; } protected void ForceReloadPage(int newNum) { currPage = newNum; } protected override async Task OnInitializedAsync() { AppMService.ShowSearch = true; AppMService.PageName = "Utenti"; AppMService.PageIcon = "fas fa-users pr-2"; AppMService.EA_SearchUpdated += OnSeachUpdated; await ReloadAllData(); } protected async Task ReloadAllData() { PlantsList = await DataService.PlantsGetAll(); SuppliersList = await DataService.SuppliersGetAll(); TransportersList = await DataService.TransportersGetAll(); await ReloadData(); } protected void ResetData() { DataService.rollBackEdit(currRecord); currRecord = null; } protected void Select(UserModel selRecord) { // applico filtro da selezione currRecord = selRecord; } protected async Task UpdateData() { currRecord = null; await ReloadData(); } #endregion Protected Methods #region Public Methods public string checkSelect(int UserId) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.UserId == UserId) ? "table-info" : ""; } catch { } } return answ; } public void Dispose() { AppMService.EA_SearchUpdated -= OnSeachUpdated; } public async void OnSeachUpdated() { await InvokeAsync(() => { Task task = UpdateData(); StateHasChanged(); }); } #endregion Public Methods } }