using Core; using LiMan.DB.DBModels; using LiMan.UI.Data; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration; using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using static Core.Enum; //inject NavigationManager NavManager; namespace LiMan.UI.Components { public partial class ListLicenze : ComponentBase, IDisposable { #region Public Methods public string checkSelect(int IdxLic) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.IdxLic == IdxLic) ? "table-info" : ""; } catch { } } return answ; } public void Dispose() { AppMService.EA_UserNameUpd -= AppMessages_EA_UserNameUpd; aTimer.Stop(); aTimer.Dispose(); } public void ElapsedTimer(Object source, System.Timers.ElapsedEventArgs e) { var pUpd = Task.Run(async () => { await ReloadAllData(); await InvokeAsync(StateHasChanged); }); pUpd.Wait(); } public void StartTimer() { int tOutPeriod = 3000; int.TryParse(Configuration["ReloadStatusTimer"], out tOutPeriod); aTimer = new System.Timers.Timer(tOutPeriod); aTimer.Elapsed += ElapsedTimer; aTimer.Enabled = true; aTimer.Start(); } #endregion Public Methods #region Protected Fields protected int totalCount = 0; #endregion Protected Fields #region Protected Enums protected enum detailType { nd = 0, attivazioni, tickets } #endregion Protected Enums #region Protected Properties [Inject] protected MessageService AppMService { get; set; } protected string AppTipoSel { get => appTipoSel; set { if (appTipoSel != value) { currPage = 1; appTipoSel = value; var pUpd = Task.Run(async () => { await ReloadAllData(); }); pUpd.Wait(); } } } [Inject] protected LiManDataService DataService { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected NavigationManager NavManager { get; set; } protected bool showCamera { get; set; } = false; #endregion Protected Properties #region Protected Methods protected void AddNew() { LicenzaModel newRec = new LicenzaModel() { CodApp = "AAA", CodInst = "EgalWare", Descrizione = "Nuova Licenza", NumLicenze = 1, Enigma = "", Payload = "", Tipo = TipoLicenza.MasterKey, Scadenza = DateTime.Today.AddMonths(1) }; // calcolo chiave ed aggiungo... newRec.Chiave = newRec.ChiaveCalc; currRecord = newRec; resetShowDetail(); } /// formatta testo secondo scadenza: scadenza > 60gg --> verde 0 < scadenza < 60 /// gg --> giallo scadenza < 0 --> rosso protected string cssScadenza(DateTime scadenza) { string answ = "text-dark"; double periodo = scadenza.Subtract(DateTime.Today).TotalDays; if (periodo > 60) { answ = "text-success"; } else if (periodo > 0) { answ = "text-warning"; } else { answ = "text-danger"; } return answ; } protected void Edit(LicenzaModel selRecord) { currRecord = selRecord; resetShowDetail(); } protected override async Task OnInitializedAsync() { userName = AppMService.UserName; UserCanEdit = UserHasClaim("Admin") || UserHasClaim("SuperUser"); AppMService.EA_UserNameUpd += AppMessages_EA_UserNameUpd; StartTimer(); await ReloadAllData(); } protected async Task SetNumRec(int newNum) { currPage = 1; numRecord = newNum; await ReloadAllData(); isLoading = false; } protected async Task SetNumPage(int newNum) { currPage = newNum; await ReloadAllData(); isLoading = false; } protected async Task ResetData() { await FullReload(); } protected void resetShowDetail() { showActivations = false; showTickets = false; } protected void Select(LicenzaModel selRecord, detailType reqDetail) { currRecord = selRecord; resetShowDetail(); switch (reqDetail) { case detailType.attivazioni: showActivations = true; break; case detailType.tickets: showTickets = true; break; case detailType.nd: default: break; } } protected async Task UpdateActivData() { int idxLic = currRecord.IdxLic; await FullReload(); currRecord = ListRecords.Where(x => x.IdxLic == idxLic).FirstOrDefault(); resetShowDetail(); showActivations = true; } protected async Task UpdateTicketData() { int idxLic = currRecord.IdxLic; await FullReload(); currRecord = ListRecords.Where(x => x.IdxLic == idxLic).FirstOrDefault(); resetShowDetail(); showTickets = true; } protected bool UserHasClaim(string ruolo) { return DataService.UserHasClaim(userName, ruolo); } #endregion Protected Methods #region Private Fields private static System.Timers.Timer aTimer; private LicenzaModel currRecord = null; private List ListApp; private List ListInstall; private List ListRecords; private List SearchRecords; private bool showActivations = false; private bool showTickets = false; private Dictionary UserClaimsLUT = new Dictionary(); private string userName = ""; #endregion Private Fields #region Private Properties private string appTipoSel { get; set; } = ""; [Inject] private IConfiguration Configuration { get; set; } private int currPage { get { return AppMService.PageNum; } set { AppMService.PageNum = value; } } private bool isLoading { get; set; } = false; private Dictionary ListAppType { get => Core.Enum.AppType; } private int numRecord { get { return AppMService.PageSize; } set { AppMService.PageSize = value; } } private bool OnlyActive { get { bool answ = false; if (AppMService.DetailDBFilter != null) { answ = AppMService.DetailDBFilter.OnlyActive; } return answ; } set { if (!AppMService.DetailDBFilter.OnlyActive.Equals(value)) { AppMService.DetailDBFilter.OnlyActive = value; var pUpd = Task.Run(async () => await ReloadAllData()); pUpd.Wait(); } } } private string SelApp { get { string answ = ""; if (AppMService.DetailDBFilter != null) { answ = AppMService.DetailDBFilter.ApplicazioneSel; } return answ; } set { if (!AppMService.DetailDBFilter.ApplicazioneSel.Equals(value)) { AppMService.DetailDBFilter.ApplicazioneSel = value; var pUpd = Task.Run(async () => await ReloadAllData()); pUpd.Wait(); } } } private string SelInst { get { string answ = ""; if (AppMService.DetailDBFilter != null) { answ = AppMService.DetailDBFilter.InstallazioneSel; } return answ; } set { if (!AppMService.DetailDBFilter.InstallazioneSel.Equals(value)) { AppMService.DetailDBFilter.InstallazioneSel = value; var pUpd = Task.Run(async () => await ReloadAllData()); pUpd.Wait(); } } } private bool UserCanEdit { get; set; } = false; #endregion Private Properties #region Private Methods private void AppMessages_EA_UserNameUpd() { userName = AppMService.UserName; var pUpd = Task.Run(async () => await ReloadAllData()); pUpd.Wait(); } private async Task FullReload() { currRecord = null; resetShowDetail(); await DataService.FlushRedisCache(); await ReloadAllData(); } private async Task ReloadAllData() { isLoading = true; ListRecords = null; // se ho un tipoApp filtrato --> filtro applicazioni ListApp = await DataService.ApplicNextGetAll(false); if (!string.IsNullOrEmpty(AppTipoSel)) { ListApp = ListApp.Where(x => x.Tipo == AppTipoSel || string.IsNullOrEmpty(x.Tipo)).ToList(); } ListInstall = await DataService.InstallazioniNextGetAll(); await Task.Delay(1); SearchRecords = await DataService.LicenzeNextGetFilt(AppMService.DetailDBFilter); if (!string.IsNullOrEmpty(AppTipoSel)) { SearchRecords = SearchRecords.Where(x => x.ApplicativoNav.Tipo == AppTipoSel).ToList(); } totalCount = SearchRecords.Count(); ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); isLoading = false; } #endregion Private Methods } }