using EgwCoreLib.Razor; using GPW.CORE.Data.DbModels; using GPW.CORE.Data.Services; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using Microsoft.VisualBasic.FileIO; using NLog; using Radzen; using System; using static EgwCoreLib.Razor.Toggler; namespace GPW.CORE.ADM.Components.Compo { public partial class ClientiMan : 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!; #endregion Protected Properties #region Protected Methods protected string CheckSel(AnagClientiModel curItem) { string answ = ""; if (SelItem != null) { answ = curItem.IdxCliente == SelItem.IdxCliente ? "table-info" : ""; } // verifico stato attivo answ += !curItem.Attivo ? " striked" : ""; return answ; } protected async Task CreateNew() { SelItem = new AnagClientiModel() { RagSociale = "__Nuovo cliente", PIva = "0000000000000000", Cf = "0000000000000000", Nota = $"Nuovo cliente - {DateTime.Now}", Attivo = true }; await InvokeAsync(StateHasChanged); } protected async void DoDelete(AnagClientiModel selItem) { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record?")) return; isLoading = true; bool fatto = await GDataServ.AnagClientiDelete(selItem); await ReloadData(); await InvokeAsync(StateHasChanged); } protected void DoEdit(AnagClientiModel? selItem) { SelItem = selItem; } protected async Task ForceReload(bool force) { SelItem = null; await ReloadData(); } protected override async Task OnInitializedAsync() { CurrSearch = ""; AppMServ.SearchVal = ""; AppMServ.EA_SearchUpdated += AppMServ_EA_SearchUpdated; numRecord = await AppMServ.NumRowGridGet(gridKey); ToggleData = new SelectGlobalToggle() { leftString = "Tutti", rightString = "Solo Attivi", placardCss = "border-dark" }; } protected override async Task OnParametersSetAsync() { await ReloadData(); } 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 Traduci(string lemma) { return AuthServ.Traduci(lemma, AppMServ.UserLang); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private string gridKey = "ClientiMan"; private AnagClientiModel? SelItem = null; private bool sortAsc = true; private string sortField = ""; #endregion Private Fields #region Private Properties private int currPage { get; set; } = 1; private string CurrSearch { get; set; } = ""; private bool isLoading { get; set; } = false; private List? ListRecords { get; set; } = null; private int numRecord { get; set; } = 10; private List? SearchRecords { get; set; } = null; private SelectGlobalToggle ToggleData { get; set; } = new SelectGlobalToggle(); 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); } private async Task evToggled(SelectGlobalToggle newTogData) { ToggleData = newTogData; await ReloadData(); } private async Task ReloadData() { isLoading = true; ListRecords = null; try { SearchRecords = await GDataServ.AnagClientiAllAsync(); // verifico condizioni filtro if (ToggleData.isActive) { SearchRecords = SearchRecords.Where(x => x.Attivo).ToList(); } // verifico filtro per ricerca if (!string.IsNullOrEmpty(CurrSearch)) { SearchRecords = SearchRecords.Where(x => x.RagSociale.Contains(CurrSearch, StringComparison.InvariantCultureIgnoreCase)).ToList(); } } catch (Exception ex) { Log.Error($"Eccezione in recupero dati{Environment.NewLine}{ex}"); } totalCount = SearchRecords.Count; SortTable(); isLoading = false; } private void SortTable() { if (SearchRecords != null) { // se ho ordinamento riordino... if (!string.IsNullOrEmpty(sortField)) { switch (sortField) { case "RagSoc": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.RagSociale).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.RagSociale).ToList(); } break; case "Indirizzo": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Prov).ThenBy(x => x.Indirizzo).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Prov).ThenBy(x => x.Indirizzo).ToList(); } break; case "Contatti": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Email).ThenBy(x => x.Tel).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Email).ThenBy(x => x.Tel).ToList(); } break; case "PICF": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.PIva).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.PIva).ToList(); } break; case "Note": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Nota).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Nota).ToList(); } break; case "Attivo": if (sortAsc) { SearchRecords = SearchRecords.OrderBy(x => x.Attivo).ToList(); } else { SearchRecords = SearchRecords.OrderByDescending(x => x.Attivo).ToList(); } break; default: break; } } // filtro x display ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); } else { ListRecords = new List(); } } #endregion Private Methods } }