Files
mapo-core/MP.Prog/Pages/Archive.razor.cs
T
2021-09-03 18:01:58 +02:00

303 lines
7.8 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.FileData.DatabaseModels;
using MP.Prog.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Prog.Pages
{
public partial class Archive : ComponentBase, IDisposable
{
#region Private Fields
private List<ArticoloModel> ArtList;
private FileModel currRecord = null;
private List<FileModel> ListRecords;
private List<MacchinaModel> MacList;
private List<FileModel> SearchRecords;
#endregion Private Fields
#region Protected Fields
protected string _SearchArt = "###";
#endregion Protected 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 SelCodArt
{
get
{
string answ = "";
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.CodArticolo;
}
return answ;
}
set
{
if (!AppMService.File_Filter.CodArticolo.Equals(value))
{
AppMService.File_Filter.CodArticolo = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private string SelIdxMacc
{
get
{
string answ = "";
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.IdxMacchina;
}
return answ;
}
set
{
if (!AppMService.File_Filter.IdxMacchina.Equals(value))
{
AppMService.File_Filter.IdxMacchina = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
#endregion Private Properties
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; }
[Inject]
protected FileArchDataService DataService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
protected string SearchArt
{
get
{
return _SearchArt;
}
set
{
isLoading = true;
_SearchArt = value;
// se son > 3 char... debounce...
if (string.IsNullOrEmpty(value))
{
_SearchArt = "###";
}
if (value.Length >= 3)
{
var pUpd = Task.Run(async () =>
{
ArtList = await DataService.ArticoliGetFilt(SearchArt);
});
pUpd.Wait();
}
isLoading = false;
}
}
protected int totalCount
{
get
{
int answ = 0;
if (SearchRecords != null)
{
answ = SearchRecords.Count;
}
return answ;
}
}
#endregion Protected Properties
#region Private Methods
private async Task ReloadData()
{
isLoading = true;
SearchRecords = await DataService.FileGetFilt(AppMService.File_Filter.IdxMacchina, AppMService.File_Filter.CodArticolo, AppMService.SearchVal);
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
isLoading = false;
}
#endregion Private Methods
#region Protected Methods
protected void Edit(FileModel selRecord)
{
#if false
// rileggo dal DB il record corrente...
var pUpd = Task.Run(async () => currRecord = await DataService.OrderGetByCode(selRecord.OrderCode));
pUpd.Wait();
#endif
}
protected async Task ForceCheck()
{
isLoading = true;
await DataService.updateAllArchive();
currRecord = null;
SearchRecords = null;
ListRecords = null;
AppMService.File_Filter = SelectData.Init(5, 10);
isLoading = false;
await ReloadAllData();
}
protected void ForceReload(int newNum)
{
numRecord = newNum;
}
protected void ForceReloadPage(int newNum)
{
currPage = newNum;
}
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
AppMService.ShowSearch = false;
AppMService.PageName = "Archivio File Programmi";
AppMService.PageIcon = "fas fa-folder pr-2";
AppMService.EA_SearchUpdated += OnSeachUpdated;
await ReloadAllData();
}
protected async Task ReloadAllData()
{
isLoading = true;
MacList = await DataService.MacchineGetAll();
SelIdxMacc = "0";
ArtList = await DataService.ArticoliGetFilt(SearchArt);
SelCodArt = "###";
isLoading = false;
await ReloadData();
}
protected void ResetData()
{
DataService.rollBackEdit(currRecord);
currRecord = null;
}
protected async Task ResetFilter()
{
currRecord = null;
SearchRecords = null;
ListRecords = null;
AppMService.File_Filter = SelectData.Init(5, 10);
SearchArt = "###";
await ReloadAllData();
}
protected void ResetSearchArt()
{
SearchArt = "###";
}
protected void Select(FileModel selRecord)
{
// applico filtro da selezione
currRecord = selRecord;
}
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await ReloadData();
}
#endregion Protected Methods
#region Public Methods
public string checkSelect(int FileId)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.FileId == FileId) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
AppMService.EA_SearchUpdated -= OnSeachUpdated;
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
}
}