using Core; using Core.DTO; using LiMan.DB.DBModels; using LiMan.DB.DTO; using LiMan.UI.Data; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using static Core.Enum; namespace LiMan.UI.Components { public partial class Tickets { #region Private Fields private List ListRecords; private List SearchRecords; private List ListInstall; private List ListActivations; private List ListApp; protected int totalCount = 0; protected TipologiaTicket currTipo = TipologiaTicket.ND; private List FileAttached; private bool showTickets = false; #endregion Private Fields #region Protected Fields protected SubLicenzaModel _currRecord = new SubLicenzaModel(); protected LicenzaModel _masterLic = new LicenzaModel(); protected int idxTicketSel = 0; protected StatoRichiesta StatusSel = StatoRichiesta.ND; protected bool showKey = false; #endregion Protected Fields #region Private Properties private SubLicenzaModel currRecord { get { return _currRecord; } set { _currRecord = value; } } protected override async Task OnInitializedAsync() { await ReloadAllData(); } private bool isLoading { get; set; } = false; [Inject] private IJSRuntime JSRuntime { get; set; } #endregion Private Properties #region Protected Properties [Inject] protected LiManDataService DataService { get; set; } #endregion Protected Properties #region Public Properties //[Parameter] //public EventCallback DataReset { get; set; } //[Parameter] //public EventCallback DataUpdated { get; set; } //[Parameter] //public LicenzaModel MasterLicence //{ // get // { // return _masterLic; // } // set // { // _masterLic = value; // var pUpd = Task.Run(async () => await ReloadAllData()); // pUpd.Wait(); // } //} #endregion Public Properties #region Private Methods [Inject] protected MessageService AppMService { get; set; } private async Task fullReload() { await DataService.InvalidateAllCache(); await ReloadAllData(); } protected string mainCss { get => idxTicketSel == 0 ? "col-12" : "col-8"; } private async Task ReloadAllData() { isLoading = true; idxTicketSel = 0; await Task.Delay(1); showTickets = false; ListApp = await DataService.ApplicazioniNextGetAll(); ListInstall = await DataService.InstallazioniNextGetAll(); //bool StatoRichiesta = true; //SearchRecords = await LMDService.LicenzeNextGetFilt(AppMServ.DetailDBFilter); SearchRecords = await DataService.TicketsGetFilt(true, SelApp, SelInst); //totalCount = SearchRecords.Count(); //SearchRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); //SearchRecords = await LMDService.TicketsGetAll(); ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); totalCount = SearchRecords.Count(); isLoading = false; } 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 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 int numRecord { get { return AppMService.PageSize; } set { AppMService.PageSize = value; } } private int currPage { get { return AppMService.PageNum; } set { AppMService.PageNum = value; } } protected async Task PagerReloadNum(int newNum) { numRecord = newNum; await ReloadAllData(); isLoading = false; } protected async Task PagerReloadPage(int newNum) { currPage = newNum; await ReloadAllData(); isLoading = false; } //private async void DownloadFileFromURL(string fileName, string rawUrl) //{ // rawUrl = rawUrl.Replace("\\", "/"); // var fileURL = $"unsafe_uploads/"+rawUrl; // //var fileName = "log-0001.txt"; // //var fileName = FileAttached; // await JS.InvokeVoidAsync("triggerFileDownload", fileName, fileURL); //} #endregion Private Methods #region Protected Methods protected async void closeDet() { await ReloadAllData(); } /// /// formatta testo secondo scadenza: /// scadenza < oggi --> verde /// scadenza > oggi --> rosso /// /// /// protected string cssScadenza(DateTime scadenza) { string answ = "text-dark"; double periodo = scadenza.Subtract(DateTime.Today).TotalDays; if (periodo <= 0) { answ = "text-success"; } else { answ = "text-danger"; } return answ; } protected async Task ResetData() { await fullReload(); } protected void showDecrypt() { showKey = !showKey; } protected async Task showDet(TicketDTO currTicket) { // salvo ticket sel idxTicketSel = currTicket.IdxTicket; StatusSel = currTicket.Status; currTipo = currTicket.TType; FileAttached = await DataService.FileGetFilt(idxTicketSel); showTickets = true; // mostro SOLO le attivazioni di cui ho ticket attivi... //ListActivations = MasterLicence //.Attivazioni //.Where(a => a.IdxSubLic == currTicket.IdxSubLic) //.ToList(); } protected async Task UnLock(SubLicenzaModel selRecord) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il blocco? la data di scadenza verrà impostata a ieri liberando la licenza per cancellazioni o rimozioni.")) return; // chiamo procedura sblocco... await DataService.AttivazioneUnlock(selRecord.IdxSubLic); //await DataUpdated.InvokeAsync(0); } protected async void updateStato(StatoRichiesta nuovoStato) { if (nuovoStato == StatoRichiesta.Approvata || nuovoStato == StatoRichiesta.Rifiutata) { if (!await JSRuntime.InvokeAsync("confirm", "Confermi l'azione richiesta? Ricorda che in caso di approvazione/rifiuto va modificato preventivamente il record dell'Attivazione coinvolta.")) return; } if (idxTicketSel > 0) { // salvo presa in carico ticket await DataService.TicketUpdateState(idxTicketSel, nuovoStato); //await DataUpdated.InvokeAsync(0); } } #endregion Protected Methods #region Public Methods public string checkSelect(int IdxSubLic) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.IdxSubLic == IdxSubLic) ? "table-info" : ""; } catch { } } return answ; } public string decryptAuthKey(string authKey) { string answ = authKey; try { answ = SteamCrypto.DecryptString(authKey, "AuthGPW"); } catch { } return answ; } public string selTick(int idxTicket) { string answ = " "; if (idxTicketSel == idxTicket) { answ = "table-primary"; } return answ; } #endregion Public Methods } }