using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.AppAuth.DTO; using MP.AppAuth.Models; using MP.AppAuth.Services; using MP.Land.Data; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MP.Land.Components { public partial class CompareConfig { #region Public Properties [Parameter] public EventCallback EC_reqAct { get; set; } [Parameter] public EventCallback EC_reqReload { get; set; } [Parameter] public List ListConfigLoc { get; set; } = new List(); [Parameter] public List ListConfigRem { get; set; } = new List(); [Parameter] public bool ShowDetail { get; set; } = false; #endregion Public Properties #region Protected Fields protected List ListCompare = new List(); protected List ListCompareAll = new List(); #endregion Protected Fields #region Protected Properties protected CmpCompType.Display currFilt { get; set; } = new CmpCompType.Display(); [Inject] protected AppAuthService DataService { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected int numLoc { get; set; } = 0; protected int numRem { get; set; } = 0; #endregion Protected Properties #region Protected Methods protected string genClass(CompConfig currItem) { string answ = currItem.IsEqual ? "text-dark" : currItem.SrcExist && currItem.DestExist ? "text-warning" : currItem.SrcExist ? "text-success" : "text-danger"; return answ; } protected async Task LocalAdd(CompConfig currItem) { //if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler aggiungere il record?")) // return; bool fatto = false; if (currItem != null) { // modifico Source: valore = valStd... currItem.Source.Valore = currItem.Source.ValoreStd; fatto = await DataService.ConfigAdd(currItem.Source); } if (fatto) { await ReqReload(); } } protected async Task LocalDelete(CompConfig currItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record?")) return; bool fatto = false; if (currItem != null) { fatto = await DataService.ConfigDelete(currItem.Dest.Chiave); } if (fatto) { await ReqReload(); } } protected async Task LocalUpd(CompConfig currItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler aggiungere il record?")) return; bool fatto = false; if (currItem != null) { // attenzione: preservo il valore LOCALE currItem.Source.Valore = currItem.Dest.Valore; fatto = await DataService.ConfigUpd(currItem.Source); } if (fatto) { await ReqReload(); } } protected override void OnParametersSet() { ForceReload(); } /// /// Report richiesta confronto x Config /// protected async Task ReqCompare() { await EC_reqAct.InvokeAsync("Config"); } /// /// Report evento richiesta confronto AnagKeyVal /// protected async Task ReqReload() { await EC_reqReload.InvokeAsync("Config"); } /// /// Report evento richiesta reset confronto /// protected async Task ResetCompare() { await EC_reqAct.InvokeAsync(""); } protected void SetFilter(CmpCompType.Display filtDisplay) { currFilt = filtDisplay; ForceReload(); } protected async Task SetNumRec(int newNum) { numRecord = newNum; currPage = 1; await InvokeAsync(UpdPaging); await Task.Delay(1); isLoading = false; } protected async Task SetPage(int newNum) { isLoading = true; currPage = newNum; await InvokeAsync(UpdPaging); await Task.Delay(1); isLoading = false; } #endregion Protected Methods #region Private Properties private int currPage { get; set; } = 1; private bool isLoading { get; set; } = false; private int numRecord { get; set; } = 10; private int totalCount { get; set; } = 0; #endregion Private Properties #region Private Methods private void ForceReload() { numLoc = ListConfigLoc != null ? ListConfigLoc.Count : 0; numRem = ListConfigRem != null ? ListConfigRem.Count : 0; // predispongo lista comparazione... ListCompareAll = new List(); int cIdx = 0; // filtro EQUAL if (currFilt.Equal) { foreach (var item in ListConfigRem) { CompConfig newRec = new CompConfig() { Idx = cIdx++, NomeVar = item.Chiave, Source = item, Dest = ListConfigLoc.Find(x => x.Chiave == item.Chiave) }; if (newRec.IsEqual) { ListCompareAll.Add(newRec); } } } // ciclo valori src... if (currFilt.Diff) { foreach (var item in ListConfigRem) { CompConfig newRec = new CompConfig() { Idx = cIdx++, NomeVar = item.Chiave, Source = item, Dest = ListConfigLoc.Find(x => x.Chiave == item.Chiave) }; if (!newRec.IsEqual && currFilt.Diff && newRec.SrcExist && newRec.DestExist) { ListCompareAll.Add(newRec); } } } if (currFilt.Sx) { foreach (var item in ListConfigRem) { // se non c'è... if (ListConfigLoc.Where(x => x.Chiave == item.Chiave).Count() == 0) { CompConfig newRec = new CompConfig() { Idx = cIdx++, NomeVar = item.Chiave, Source = item, Dest = ListConfigLoc.Find(x => x.Chiave == item.Chiave) }; if (!newRec.IsEqual) { ListCompareAll.Add(newRec); } } } } if (currFilt.Dx) { // cerco eventuali record SOLO locali... foreach (var item in ListConfigLoc) { // se non c'è... if (ListConfigRem.Where(x => x.Chiave == item.Chiave).Count() == 0) { CompConfig newRec = new CompConfig() { Idx = cIdx++, NomeVar = item.Chiave, Source = null, Dest = item }; ListCompareAll.Add(newRec); } } } // filtro sui dati chiave e note if (!string.IsNullOrEmpty(searchVal)) { ListCompareAll = ListCompareAll .Where(x => x.Source.Chiave.Contains(searchVal, StringComparison.InvariantCultureIgnoreCase) || x.Source.Note.Contains(searchVal, StringComparison.InvariantCultureIgnoreCase)) .ToList(); } totalCount = ListCompareAll.Count; // riordino... ListCompareAll = ListCompareAll.OrderBy(x => x.NomeVar).ToList(); // ciclo eventuali record locali rimasti UpdPaging(); } [Inject] protected LMessageService AppMServ { get; set; } = null!; public string searchVal { get => AppMServ.SearchVal; } private void UpdPaging() { ListCompare = ListCompareAll.Skip((currPage - 1) * numRecord).Take(numRecord).ToList(); } #endregion Private Methods } }