Files
2026-05-11 12:41:33 +02:00

190 lines
4.9 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Core.DTO;
using MP.IOC.Services;
namespace MP.IOC.Components.Pages
{
public partial class RouteConf
{
#region Protected Properties
protected string CssReset
{
get => string.IsNullOrEmpty(SearchVal) ? "btn-outline-secondary" : "btn-primary";
}
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected string SearchVal
{
get => _searchVal;
set
{
if (_searchVal != value)
{
_searchVal = value;
UpdateTable();
}
}
}
#endregion Protected Properties
#region Protected Methods
protected string CheckSelect(WeightDTO currRec)
{
return SelRecord != null && SelRecord.Method == currRec.Method ? "table-info" : "";
}
protected override async Task OnInitializedAsync()
{
//return base.OnInitializedAsync();
await ReloadData();
UpdateTable();
}
protected void ResetSearch()
{
SearchVal = "";
//UpdateTable();
}
#endregion Protected Methods
#region Private Fields
private string _searchVal = "";
private List<WeightDTO> ListComplete = new();
private List<WeightDTO> ListPaged = new();
private List<WeightDTO> ListSearch = new();
private int numRecPage = 10;
private int pageNum = 1;
private WeightDTO? SelRecord = null;
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
[Inject]
private IWeightProvider WService { get; set; } = null!;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Edit record selezionato
/// </summary>
/// <param name="curRec"></param>
private void DoEdit(WeightDTO curRec)
{
SelRecord = curRec;
}
private void DoReset()
{
SelRecord = null;
}
private void DoSave(WeightDTO updRec)
{
// salvo e resetto...
SelRecord = null;
}
private async Task ReloadData()
{
ListComplete = await WService.GetAllWeightsAsync();
}
private void SaveNumRec(int newNum)
{
numRecPage = newNum;
UpdateTable();
}
private void SavePage(int newNum)
{
pageNum = newNum;
UpdateTable();
}
/// <summary>
/// Imposta globalmente weight x tutte le chiavi...
/// </summary>
/// <param name="newWeight"></param>
/// <returns></returns>
private async Task SetAllWeight(int newWeight)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Confermi di voler impostare questo peso per TUTTE le chiamate?"))
return;
newWeight = Math.Clamp(newWeight, 0, 100);
foreach (var curRec in ListComplete)
{
curRec.NewWeight = newWeight;
curRec.OldWeight = 100 - newWeight;
WService.UpsertWeight(curRec);
}
// rileggo!
await ReloadData();
UpdateTable();
}
/// <summary>
/// Imposta il peso del metodo "New" come richiesto, con eventuale check sui limiti 0-100
/// </summary>
/// <param name="curRec"></param>
/// <param name="newWeight"></param>
private async Task SetNewWeight(WeightDTO curRec, int newWeight)
{
newWeight = Math.Clamp(newWeight, 0, 100);
curRec.NewWeight = newWeight;
curRec.OldWeight = 100 - newWeight;
// salvo!
WService.UpsertWeight(curRec);
// rileggo!
await ReloadData();
UpdateTable();
}
private void UpdateTable()
{
// effettuo eventuale ricerca + conteggio...
if (string.IsNullOrEmpty(SearchVal))
{
ListSearch = ListComplete;
}
else
{
ListSearch = ListComplete
.Where(x => x.Method.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
.ToList();
}
totalCount = ListSearch.Count();
// esegue paginazione
if (totalCount > numRecPage)
{
ListPaged = ListSearch.Skip((pageNum - 1) * numRecPage).Take(numRecPage).ToList();
}
else
{
ListPaged = ListSearch;
}
}
#endregion Private Methods
}
}