using EgwCoreLib.Razor; using GPW.CORE.Data.DbModels; using GPW.CORE.Data.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.DataProtection; using Microsoft.JSInterop; using NLog; using static EgwCoreLib.Razor.Toggler; namespace GPW.CORE.ADM.Components.Compo { public partial class ProgettiMan : IDisposable { #region Public Methods public void Dispose() { AppMServ.EA_SearchUpdated -= AppMServ_EA_SearchUpdated; } #endregion Public Methods #region Protected Properties [Inject] protected MessageService AppMServ { get; set; } = null!; [Inject] protected AppAuthService AuthServ { get; set; } = null!; [Inject] protected GpwDataService GDataServ { get; set; } = null!; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected List ListClienti { get; set; } = new List(); protected List ListGruppi { get; set; } = new List(); #endregion Protected Properties #region Protected Methods protected string CheckSel(CalcOreProgettiModel curItem) { string answ = ""; if (RecordEdit != null) { answ = curItem.IdxProgetto == RecordEdit.IdxProgetto ? "table-info" : ""; } // verifico stato attivo answ += !(curItem.Attivo ?? false) ? " striked" : ""; return answ; } protected async Task CreateNew() { FullEdit = true; RecordSel = null; RecordEdit = new AnagProgettiModel() { NomeProj = "__Nuovo Progetto", DescrProj = "Descrizione nuovo progetto", Gruppo = "", IdxCliente = 0, Attivo = true }; await InvokeAsync(StateHasChanged); } protected async void DoDelete(CalcOreProgettiModel selItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record?")) return; isLoading = true; await Task.Delay(1); await InvokeAsync(StateHasChanged); RecordSel = null; RecordEdit = GDataServ.AnagProjByKey(selItem.IdxProgetto); bool fatto = await GDataServ.AnagProjDelete(RecordEdit); if (fatto) { await Task.Delay(1); await GDataServ.AnagProjForceUpdate(); } RecordEdit = null; await ReloadData(); await InvokeAsync(StateHasChanged); } protected void DoEdit(CalcOreProgettiModel? selItem) { FullEdit = true; ShowFasi = false; RecordSel = null; RecordEdit = GDataServ.AnagProjByKey(selItem.IdxProgetto); } protected async Task DoRecalc() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler forzare il ricalcolo dei record?")) return; isLoading = true; await Task.Delay(1); await InvokeAsync(StateHasChanged); await GDataServ.AnagProjForceUpdate(); await Task.Delay(1); await ReloadData(); } protected void DoSelect(CalcOreProgettiModel? selItem) { FullEdit = false; ShowFasi = false; RecordSel = selItem; RecordEdit = null; } protected async Task DoShowFasi(CalcOreProgettiModel? selItem) { FullEdit = false; // carico le fasi e le asso al controllo! ListFasiSel = await GDataServ.AnagFasiExplByProj(selItem.IdxProgetto); ShowFasi = true; RecordSel = selItem; RecordEdit = null; } protected async Task ForceReload() { await salvaFiltToggle(); await salvaFiltGrpCli(); FullEdit = false; ShowFasi = false; RecordEdit = null; RecordSel = null; currPage = 1; await ReloadData(); } /// /// init valori da config /// /// protected async Task initConf() { // leggo conf standard controllo RegAtt var sWarningRatioPerc = await GDataServ.ConfigGetKeyAsync("WarningRatioPerc"); if (sWarningRatioPerc != null) { double.TryParse(sWarningRatioPerc.valore, out warnRatio); } } protected override async Task OnInitializedAsync() { await initConf(); CurrSearch = ""; AppMServ.SearchVal = ""; AppMServ.EA_SearchUpdated += AppMServ_EA_SearchUpdated; // recupero preferenze utente... await InitUserPref(); } protected override async Task OnParametersSetAsync() { await ReloadData(); } protected async Task ReloadFasi() { // rileggo fasi associate... ListFasiSel = new List(); await Task.Delay(1); //await InvokeAsync(StateHasChanged); // carico le fasi e le asso al controllo! ListFasiSel = await GDataServ.AnagFasiExplByProj(RecordSel.IdxProgetto); await Task.Delay(1); //await InvokeAsync(StateHasChanged); } protected async Task SetNumRec(int newNum) { numRecord = newNum; currPage = 1; await AppMServ.NumRowGridSet(gridKey, newNum); await InvokeAsync(ReloadData); } protected async Task SetPage(int newNum) { currPage = newNum; await InvokeAsync(ReloadData); } protected async Task SortRequested(Sorter.SortCallBack e) { sortField = e.ParamName; sortAsc = e.IsAscending; await ReloadData(); } protected string UserLang { get => AppMServ.UserLang; } protected string Traduci(string lemma) { return AuthServ.Traduci(lemma, UserLang); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private bool FullEdit = false; private string gridKey = "ProgettiMan"; private AnagProgettiModel? RecordEdit = null; private CalcOreProgettiModel? RecordSel = null; private bool ShowFasi = false; private bool sortAsc = true; private string sortField = ""; private double warnRatio = 50; #endregion Private Fields #region Private Properties private int currPage { get; set; } = 1; private string CurrSearch { get; set; } = ""; private string Gruppo { get; set; } = ""; private int IdxCliente { get; set; } = 0; private bool isLoading { get; set; } = false; private List? ListFasiSel { get; set; } = null; private List? ListRecords { get; set; } = null; private string modalSize { get => FullEdit ? "modal-xl" : ""; } private int numRecord { get; set; } = 10; private List? SearchRecords { get; set; } = null; private string SelCliText { get => ShowCli ? "Elimina Filt." : "Filt.Clienti"; } private string SelGrpText { get => ShowGruppo ? "Elimina Filt." : "Filt.Gruppo"; } private bool ShowCli { get; set; } = false; private bool ShowGruppo { get; set; } = false; private bool ShowPrjArc { get; set; } = false; private bool ShowPrjStr { get; set; } = false; private bool ShowPrjZH { get; set; } = true; private int totalCount { get; set; } = 0; #endregion Private Properties #region Private Methods private async void AppMServ_EA_SearchUpdated() { CurrSearch = AppMServ.SearchVal; await ReloadData(); await InvokeAsync(StateHasChanged); } /// /// restituisce una classe css a seconda dei valori passati: /// green: bdgt > real /// orange: real > bdgt*warning /// red: real > bdgt /// std: errore... /// /// /// /// private string ColorByVal(object real, object bdgt) { string specClass = "default"; try { double valoreReal = Convert.ToDouble(real); double valoreBdget = Convert.ToDouble(bdgt); double valoreWarn = Convert.ToDouble(bdgt) * warnRatio / 100; if (valoreReal >= valoreBdget) { specClass = "danger"; } else if (valoreReal >= valoreWarn) { specClass = "warning"; } else { specClass = "success"; } } catch { } return $" bg-{specClass} bg-opacity-50 bg-gradient border border-{specClass} rounded"; } private async Task FixFilt() { if (!ShowGruppo) { Gruppo = ""; } if (!ShowCli) { IdxCliente = 0; } currPage = 1; await salvaFiltGrpCli(); await ReloadData(); } /// /// Init preferenze utente /// /// private async Task InitUserPref() { try { numRecord = await AppMServ.NumRowGridGet(gridKey); ShowPrjArc = await AppMServ.UserPrefGet("ShowPrjArc"); ShowPrjStr = await AppMServ.UserPrefGet("ShowPrjStr"); ShowPrjZH = await AppMServ.UserPrefGet("ShowPrjZH"); ShowGruppo = await AppMServ.UserPrefGet("ShowGruppo"); ShowCli = await AppMServ.UserPrefGet("ShowCli"); Gruppo = await AppMServ.UserPrefGet("Gruppo") ?? ""; IdxCliente = await AppMServ.UserPrefGet("IdxCliente"); } catch (Exception exc) { Log.Error($"Eccezione in InitUserPref{Environment.NewLine}{exc}"); } } private async Task ReloadData() { isLoading = true; ListRecords = null; ListGruppi = await GDataServ.AnagGruppiAll(); ListClienti = await GDataServ.AnagClientiAllAsync(); try { SearchRecords = await GDataServ.AnagProjCalcFilt(Gruppo, IdxCliente, ShowPrjArc, ShowPrjZH, ShowPrjStr); // verifico filtro per ricerca if (!string.IsNullOrEmpty(CurrSearch)) { SearchRecords = SearchRecords .Where(x => x.NomeProj.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase) || x.DescrProj.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase)) .ToList(); } } catch (Exception ex) { Log.Error($"Eccezione in recupero dati{Environment.NewLine}{ex}"); } totalCount = SearchRecords.Count; SortTable(); isLoading = false; } private async Task salvaFiltGrpCli() { // salvo await AppMServ.UserPrefSet("ShowGruppo", $"{ShowGruppo}"); await AppMServ.UserPrefSet("ShowCli", $"{ShowCli}"); await AppMServ.UserPrefSet("Gruppo", $"{Gruppo}"); await AppMServ.UserPrefSet("IdxCliente", $"{IdxCliente}"); } private async Task salvaFiltToggle() { // salvo le preferenze... await AppMServ.UserPrefSet("ShowPrjArc", $"{ShowPrjArc}"); await AppMServ.UserPrefSet("ShowPrjStr", $"{ShowPrjStr}"); await AppMServ.UserPrefSet("ShowPrjZH", $"{ShowPrjZH}"); } private void SortTable() { if (SearchRecords != null) { // se ho ordinamento riordino... if (!string.IsNullOrEmpty(sortField)) { switch (sortField) { case "Gruppo": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Gruppo).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Gruppo).ToList(); } break; case "RagSoc": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.RagSociale).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.RagSociale).ToList(); } break; case "NomeProj": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.NomeProj).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.NomeProj).ToList(); } break; case "OreTot": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.totOre).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.totOre).ToList(); } break; case "Money": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.budgetMoney).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.budgetMoney).ToList(); } break; default: break; } } // filtro x display ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); } else { ListRecords = new List(); } } #endregion Private Methods } }