using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Stats.Data; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace MP.Stats.Pages { public partial class Diario : ComponentBase, IDisposable { #region Private Fields private MP.Data.DbModels.DdbTurni currRecord = null; private string fileName = "Diario.csv"; private List ListRecords; private List SearchRecords; #endregion Private Fields #region Private Properties private SelectData currFilter { get { return MessageService.DDB_Filter; } set { MessageService.DDB_Filter = value; } } private int currPage { get { return currFilter.PageNum; } set { currFilter.PageNum = value; } } private string fullPath { get => $"{Directory.GetCurrentDirectory()}\\temp\\{fileName}"; } private bool isLoading { get; set; } = false; private int numRecord { get { return currFilter.PageSize; } set { currFilter.PageSize = value; } } #endregion Private Properties #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected MessageService MessageService { get; set; } [Inject] protected NavigationManager NavManager { get; set; } [Inject] protected MpStatsService StatService { get; set; } protected int totalCount { get; set; } = 0; #endregion Protected Properties #region Private Methods private async void clearFile() { await Task.Run(() => File.Delete(fullPath)); } private async Task ExportCsv() { isLoading = true; // recupero TUTTI i dati var allRecords = await StatService.StatDdbGetAllExport(currFilter, MessageService.SearchVal); // salvo davvero! await Egw.Core.Utils.SaveToCsv(allRecords, fullPath, ';'); isLoading = false; } private async Task reloadData() { isLoading = true; totalCount = await StatService.StatDdbGetCount(currFilter, MessageService.SearchVal); SearchRecords = await StatService.StatDdbGetAll(currFilter, MessageService.SearchVal); // faccio paginazione SOLO NELLA DECINA attuale... (quindi non tutte le pagine ma solo subset) ListRecords = SearchRecords.Skip(numRecord * (currPage % 10 - 1)).Take(numRecord).ToList(); isLoading = false; } #endregion Private Methods #region Protected Methods protected async Task DoFilter(SelectData newFilter) { clearFile(); SearchRecords = null; ListRecords = null; currFilter = newFilter; await reloadData(); } protected async Task ForceReload(int newNum) { numRecord = newNum; await reloadData(); } protected async Task ForceReloadPage(int newNum) { currPage = newNum; await reloadData(); } protected override async Task OnInitializedAsync() { clearFile(); numRecord = 10; MessageService.ShowSearch = false; MessageService.PageName = "Diario Produzione"; MessageService.PageIcon = "fa fa-clipboard"; MessageService.EA_SearchUpdated += OnSeachUpdated; await reloadData(); } protected void ResetData() { clearFile(); StatService.rollBackEdit(currRecord); currRecord = null; } protected async Task ResetFilter(SelectData newFilter) { clearFile(); currRecord = null; SearchRecords = null; ListRecords = null; currFilter = SelectData.Init(5, 3); await reloadData(); } protected async Task UpdateData() { currRecord = null; await reloadData(); } #endregion Protected Methods #region Public Methods public string checkSelect(string IdxMacchina, string CodArticolo, DateTime InizioStato) { string answ = ""; if (currRecord != null) { try { answ = (currRecord.IdxMacchina == IdxMacchina && currRecord.CodArticolo == CodArticolo && currRecord.InizioStato == InizioStato) ? "table-info" : ""; } catch { } } return answ; } public void Dispose() { MessageService.EA_SearchUpdated -= OnSeachUpdated; } public async void OnSeachUpdated() { await InvokeAsync(() => { Task task = UpdateData(); StateHasChanged(); }); } #endregion Public Methods } }