using Microsoft.AspNetCore.Components; using MP.Data.DbModels; using MP.Stats.Data; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MP.Stats.Components { public partial class ModalSearchMacc { #region Public Properties [Parameter] public List AllRecords { get; set; } = null!; [Parameter] public List CurrList { get; set; } = new List(); [Parameter] public EventCallback EC_CloseSearch { get; set; } [Parameter] public EventCallback> EC_ListUpdated { get; set; } [Parameter] public int MaxSelection { get; set; } = 6; #endregion Public Properties #region Protected Properties protected string CssClear { get => CurrList == null || CurrList.Count == 0 ? "btn-outline-secondary" : "btn-primary"; } protected string CssReset { get => string.IsNullOrEmpty(SearchVal) ? "btn-outline-secondary" : "btn-primary"; } protected List ListRecords { get; set; } = null!; protected string ListSelected { get => CurrList != null && CurrList.Count > 0 ? string.Join(", ", CurrList) : "-"; } protected int numSelected { get => CurrList.Count; } protected List SearchRecords { get; set; } = null!; protected string SearchVal { get => _searchVal; set { if (_searchVal != value) { _searchVal = value; ReloadData(); } } } #endregion Protected Properties #region Protected Methods protected string CheckSelect(AutocompleteModel currRec) { string answ = ""; if (CurrList.Contains(currRec.ValueField)) { answ = "table-info"; } else if (numSelected >= MaxSelection) { answ = "opacity-25"; } return answ; } protected void ClearSel() { CurrList.Clear(); } protected override void OnParametersSet() { ReloadData(); } protected void ReloadData() { // ricerca SearchRecords = AllRecords .Where(x => string.IsNullOrEmpty(SearchVal) || x.LabelField.Contains(SearchVal, System.StringComparison.InvariantCultureIgnoreCase)) .ToList(); totalCount = SearchRecords.Count; // paginazione ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); } protected void ResetSearch() { SearchVal = ""; ReloadData(); } protected void SetNumRec(int newNum) { numRecord = newNum; ReloadData(); } protected void SetPage(int newNum) { currPage = newNum; ReloadData(); } /// /// Esegue toggle selezione fino a numMax in aggiunta e rimuovendo tutti se era true /// protected async Task ToggleAllSel() { AllSelected = !AllSelected; if (AllSelected) { foreach (var record in ListRecords) { if (!CurrList.Contains(record.ValueField)) { CurrList.Add(record.ValueField); } // controllo superamento if (CurrList.Count() >= MaxSelection) { return; } } } else { CurrList.Clear(); } await EC_ListUpdated.InvokeAsync(CurrList); } protected void ToggleSel(string newVal) { if (!CurrList.Contains(newVal)) { CurrList.Add(newVal); } else { CurrList.Remove(newVal); } // riordino CurrList = CurrList.OrderBy(x => x).ToList(); // controllo se ho selezionato il max... if (CurrList.Count >= MaxSelection) { AllSelected = true; } } #endregion Protected Methods #region Private Fields private string _searchVal = ""; private bool AllSelected = false; private int currPage = 1; private int numRecord = 10; private int totalCount = 0; #endregion Private Fields #region Private Methods private async Task SearchMaccToggle() { await EC_ListUpdated.InvokeAsync(CurrList); await EC_CloseSearch.InvokeAsync(true); } #endregion Private Methods } }