Files
limanapp/LiMan.UI/Components/ListLicenze.razor.cs
T

373 lines
9.7 KiB
C#

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 Private Fields
private static System.Timers.Timer aTimer;
private LicenzaModel currRecord = null;
private List<ApplicativoModel> ListApp;
private List<InstallazioneModel> ListInstall;
private List<LicenzaModel> ListRecords;
private List<LicenzaModel> SearchRecords;
private bool showActivations = false;
private bool showTickets = false;
#endregion Private Fields
#region Protected Fields
protected int totalCount = 0;
#endregion Protected Fields
#region Protected Enums
protected enum detailType
{
nd = 0,
attivazioni,
tickets
}
//protected async void gtticket()
//{
// if (detailType.tickets = 2)
// {
// Select(record, detailType.tickets)
// }
// else
// {
// NavManager.NavigateTo("ListaTicket");
// }
//}
#endregion Protected Enums
#region Private Properties
[Inject]
private IConfiguration Configuration { get; set; }
private int currPage
{
get
{
return AppMService.PageNum;
}
set
{
AppMService.PageNum = value;
}
}
private bool isLoading { get; set; } = false;
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();
}
}
}
#endregion Private Properties
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; }
[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 Private Methods
private async Task fullReload()
{
currRecord = null;
resetShowDetail();
await DataService.InvalidateAllCache();
await ReloadAllData();
}
private async Task ReloadAllData()
{
isLoading = true;
ListRecords = null;
ListApp = await DataService.ApplicazioniNextGetAll();
ListInstall = await DataService.InstallazioniNextGetAll();
await Task.Delay(1);
SearchRecords = await DataService.LicenzeNextGetFilt(AppMService.DetailDBFilter);
totalCount = SearchRecords.Count();
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
isLoading = false;
}
#endregion Private Methods
#region Protected Methods
protected void AddNew()
{
LicenzaModel newRec = new LicenzaModel()
{
CodApp = "AAA",
CodInst = "EgalWare",
Descrizione = "Nuova Licenza",
Chiave = "000",
NumLicenze = 1,
Enigma = "",
Payload = "",
Tipo = TipoLicenza.MasterKey,
Scadenza = DateTime.Today.AddMonths(1)
};
currRecord = newRec;
resetShowDetail();
}
/// <summary>
/// formatta testo secondo scadenza:
/// scadenza > 60gg --> verde
/// 0 < scadenza < 60 gg --> giallo
/// scadenza < 0 --> rosso
/// </summary>
/// <param name="scadenza"></param>
/// <returns></returns>
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()
{
StartTimer();
await ReloadAllData();
}
protected async Task PagerReloadNum(int newNum)
{
numRecord = newNum;
await ReloadAllData();
isLoading = false;
}
protected async Task PagerReloadPage(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;
}
#endregion Protected Methods
#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()
{
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
}
}