Files
mapo-core/MP.SPEC/Pages/Articoli.razor.cs
T
2026-05-25 17:14:38 +02:00

345 lines
9.5 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DbModels;
using MP.SPEC.Data;
namespace MP.SPEC.Pages
{
public partial class Articoli : ComponentBase, IDisposable
{
#region Public Methods
public string CheckSelect(string codArticolo)
{
return currRecord?.CodArticolo == codArticolo
? "table-info"
: "";
}
private SelectArticoliParams currFilter = new SelectArticoliParams();
public void Dispose()
{
currRecord = null;
ListTipoArt = null;
ListAziende = null;
SearchRecords = null;
ListRecords = null;
GC.Collect();
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
currPage = 1;
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MpDataService MDService { get; set; } = null!;
[Inject]
protected NavigationManager NavManager { get; set; } = null!;
protected int totalCount
{
get
{
int answ = 0;
if (SearchRecords != null)
{
answ = SearchRecords.Count;
}
return answ;
}
}
#endregion Protected Properties
#region Protected Methods
private bool isNewArt = false;
/// <summary>
/// Crea nuovo record e va in editing...
/// </summary>
/// <returns></returns>
protected async Task addNew()
{
isNewArt = true;
currRecord = new AnagArticoliModel()
{
CodArticolo = $"_NEW_{DateTime.Now:yyyyMMdd.HHmmss}",
DescArticolo = "Nuovo articolo",
Azienda = selAzienda != "*" ? selAzienda : "MAPO",
Disegno = "",
Tipo = "ART",
CurrRev = "",
ProdRev = ""
};
await Task.Delay(1);
}
protected async Task resetSearch()
{
SearchVal = "";
await ReloadData();
}
protected string searchCss
{
get => string.IsNullOrEmpty(searchVal) ? "btn-secondary" : "btn-primary";
}
protected string SearchVal
{
get => searchVal;
set
{
// salvo solo se 3+ chars
if (value.Length > 2 || string.IsNullOrEmpty(value))
{
if (searchVal != value)
{
searchVal = value;
var pUpd = Task.Run(async () =>
{
ListRecords = null;
await Task.Delay(1);
await ReloadData();
});
pUpd.Wait();
}
}
}
}
private string searchVal { get; set; } = "";
private string chkDisabled => isNewArt ? "" : "disabled";
protected async Task cancel()
{
currRecord = null;
await ReloadData();
await Task.Delay(1);
}
/// <summary>
/// Eliminazione record selezionato (previa conferma)
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
protected async Task deleteRecord(AnagArticoliModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Eliminazione Articolo: sei sicuro di voler procedere?"))
return;
await Task.Delay(1);
var done = await MDService.ArticoliDeleteRecord(selRec);
currRecord = null;
await ReloadData();
await Task.Delay(1);
}
protected void ForceReload(int newNum)
{
numRecord = newNum;
}
protected void ForceReloadPage(int newNum)
{
currPage = newNum;
}
protected override async Task OnInitializedAsync()
{
numRecord = 10;
#if false
configData = await MDService.ConfigGetAll();
var currRec = configData.FirstOrDefault(x => x.Chiave == "AZIENDA");
if (currRec != null)
{
selAzienda = currRec.Valore;
}
#endif
selAzienda = await MDService.ConfigTryGetAsync("AZIENDA");
if (string.IsNullOrEmpty(selAzienda))
{
selAzienda = "*";
}
ListAziende = MDService.ElencoAziende();
ListTipoArt = await MDService.AnagTipoArtLV();
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected void ResetData()
{
isNewArt = false;
currRecord = null;
}
protected async Task resetSel()
{
isNewArt = false;
currRecord = null;
await Task.Delay(1);
}
protected async Task selRecord(AnagArticoliModel selRec)
{
isNewArt = false;
currRecord = selRec;
await Task.Delay(1);
}
protected async Task cloneRecord(AnagArticoliModel selRec)
{
isNewArt = true;
// creo record duplicato...
AnagArticoliModel newRec = new AnagArticoliModel()
{
Azienda = selRec.Azienda,
CodArticolo = $"clone-{selRec.CodArticolo}",
DescArticolo = $"CLONE - {selRec.DescArticolo}",
Disegno = selRec.Disegno,
Tipo = selRec.Tipo,
CurrRev = "",
ProdRev = ""
};
currRecord = newRec;
await Task.Delay(1);
}
protected async Task update(AnagArticoliModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Confermi di voler salvare le modifiche?"))
return;
await Task.Delay(1);
var done = await MDService.ArticoliUpdateRecord(selRec);
currRecord = null;
await ReloadData();
await Task.Delay(1);
}
protected async Task UpdateData()
{
currRecord = null;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private string _selAzienda = "*";
private AnagArticoliModel? currRecord = null;
private List<AnagGruppiModel>? ListAziende;
private List<AnagArticoliModel>? ListRecords;
private List<ListValuesModel>? ListTipoArt;
private List<AnagArticoliModel>? SearchRecords;
#endregion Private Fields
#region Private Properties
private int _currPage { get; set; } = 1;
private int _numRecord { get; set; } = 10;
private int currPage
{
get => _currPage;
set
{
if (_currPage != value)
{
_currPage = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private bool isLoading { get; set; } = false;
private int numRecord
{
get => _numRecord;
set
{
if (_numRecord != value)
{
_numRecord = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private string selAzienda
{
get => _selAzienda;
set
{
if (value != _selAzienda)
{
_selAzienda = value;
var pUpd = Task.Run(async () =>
{
// svuoto cache redis...
ConfigModel updRec = new ConfigModel()
{
Chiave = "AZIENDA",
Valore = value
};
MDService.ConfigUpdate(updRec);
await MDService.ConfigResetCache();
// ricarico
await Task.Delay(1);
await ReloadData();
});
pUpd.Wait();
}
}
}
private bool ShowCharts { get; set; } = false;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Seleziona record x editing
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
private bool ArticoloDelEnabled(string codArt)
{
bool answ = MDService.ArticoloDelEnabled(codArt);
return answ;
}
private async Task ReloadData()
{
isLoading = true;
SearchRecords = await MDService.ArticoliGetSearch(100000, selAzienda, SearchVal);
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
isLoading = false;
}
#endregion Private Methods
}
}