Files
mapo-core/MP.Stats/Components/ModalSearchMacc.razor.cs
T
2026-04-09 17:49:38 +02:00

200 lines
5.2 KiB
C#

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<AutocompleteModel> AllRecords { get; set; } = null!;
[Parameter]
public List<string> CurrList { get; set; } = new List<string>();
[Parameter]
public EventCallback<bool> EC_CloseSearch { get; set; }
[Parameter]
public EventCallback<List<string>> 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<AutocompleteModel> ListRecords { get; set; } = null!;
protected string ListSelected
{
get => CurrList != null && CurrList.Count > 0 ? string.Join(", ", CurrList) : "-";
}
protected int numSelected
{
get => CurrList.Count;
}
protected List<AutocompleteModel> 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();
}
/// <summary>
/// Esegue toggle selezione fino a numMax in aggiunta e rimuovendo tutti se era true
/// </summary>
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
}
}