using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data.DatabaseModels; using MP.SPEC.Components; using MP.SPEC.Data; using MP.SPEC.Services; using NLog; using System.Reflection.PortableExecutable; namespace MP.SPEC.Pages { public partial class PODL { #region Protected Fields protected DataPager? pagerODL = null!; protected bool reqNew = false; #endregion Protected Fields #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected ILocalStorageService localStorage { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; [Inject] protected IOApiService MpIoApiCall { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; #endregion Protected Properties #region Protected Methods /// /// Crea nuovo record e va in editing... /// /// protected async Task addNew() { currRecord = new PODLExpModel() { CodArticolo = $"_NEW_{DateTime.Now:yyyyMMdd.HHmmss}" }; await Task.Delay(1); } protected async Task cancel() { currRecord = null; await reloadData(); await Task.Delay(1); } protected void resetFase() { StatoSel = "*"; } protected void resetMacchina() { macchina = "*"; } protected void resetReparto() { selReparto = "*"; } protected void ForceReload(int newNum) { numRecord = newNum; } protected void ForceReloadPage(int newNum) { currPage = newNum; } protected async Task getReparto() { string keyStor = "reparto"; string localReparto = await localStorage.GetItemAsync(keyStor); if (!string.IsNullOrEmpty(localReparto)) { selReparto = localReparto; } else { selReparto = "*"; await localStorage.SetItemAsync(keyStor, selReparto); } } protected override async Task OnInitializedAsync() { await getReparto(); ListAziende = await MDService.ElencoAziende(); var allGruppiData = await MDService.ElencoGruppiFase(); if (allGruppiData != null) { ListGruppiFase = allGruppiData.Where(x => x.SelEnabled).ToList(); } ListStati = await MDService.AnagStatiComm(); currAzienda = await MDService.tryGetConfig("AZIENDA"); padCodXdl = await MDService.tryGetConfig("padCodXdl"); } protected override async Task OnParametersSetAsync() { // carico dati await reloadData(); } protected async Task pgResetReq(bool doReset) { if (doReset) { await Task.Delay(1); if (pagerODL != null) { pagerODL.resetCurrPage(); } } } /// /// Crea nuovo record e va in editing... /// /// protected async Task reqNewPODL() { header = "Nuovo PODL"; #if false if (ListArticoli != null && ListArticoli.Count > 0) { var firstArt = ListArticoli.FirstOrDefault(x => x.Azienda.StartsWith(currAzienda)); if (firstArt != null) { currArticolo = firstArt.CodArticolo; } } #endif artSearch = ""; string codExt = $"{currFase}"; string codGruppo = ""; if (ListGruppiFase != null && ListGruppiFase.Count > 0) { var firstFase = ListGruppiFase.FirstOrDefault(x => x.CodGruppo.StartsWith(currAzienda)); if (firstFase != null) { codGruppo = firstFase.CodGruppo; } } string codMacc = ""; if (ListMacchine != null && ListMacchine.Count > 0) { var firstMacc = ListMacchine.FirstOrDefault(x => x.Nome.Contains(currAzienda)); if (firstMacc != null) { codMacc = firstMacc.IdxMacchina; } } currRecord = new PODLExpModel() { CodArticolo = currArticolo, KeyBCode = codExt, KeyRichiesta = codExt, CodGruppo = codGruppo, IdxMacchina = codMacc, NumPezzi = 1, DueDate = DateTime.Now.AddDays(30) }; await Task.Delay(1); } protected async Task selRecord(PODLExpModel selRec) { currRecord = selRec; await Task.Delay(1); } protected async Task toggleClosed() { hasOdl = !hasOdl; await Task.Delay(1); } protected async Task update(PODLExpModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) return; await Task.Delay(1); var clonedRec = MP.Data.Utils.POdlExt.convertToPOdl(selRec); clonedRec.CodGruppo = currGruppoSel.CodGruppo; var done = await MDService.POdlUpdateRecord(clonedRec); await callSyncDb(clonedRec); currRecord = null; NavManager.NavigateTo(NavManager.Uri, true); } protected void UpdateTotCount(int newTotCount) { totalCount = newTotCount; } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private PODLExpModel? _currRecord = null; private PODLExpModel currRecordControlli = new PODLExpModel(); private AnagGruppi currGruppoSel = new AnagGruppi(); private List? ListArticoli; private List? ListAziende; private List? ListGruppiFase; private List? ListMacchine; private List? ListStati; #endregion Private Fields #region Private Properties private string _artSearch { get; set; } = ""; private string _currAzienda { get; set; } = "*"; private bool addEnabled { get => currFase != "*"; } private string artSearch { get => _artSearch; set { if (!_artSearch.Equals(value)) { _artSearch = value; var pUpd = Task.Run(async () => { await reloadData(); }); pUpd.Wait(); } } } private string btnNewText { get => currArticolo == "" ? "Sel Articolo" : "Nuovo PODL"; } private string currArticolo { get; set; } = ""; private string currAzienda { get => _currAzienda; set { if (!_currAzienda.Equals(value)) { _currAzienda = value; var pUpd = Task.Run(async () => { await reloadData(); }); pUpd.Wait(); } } } private string currFase { get => currFilter.CodFase; set { if (!currFilter.CodFase.Equals(value)) { currFilter.CodFase = value; currPage = 1; } } } private DateTime selDtStart { get => currFilter.DtStart; set { if (!currFilter.DtStart.Equals(value)) { currFilter.DtStart = value; currPage = 1; } } } private DateTime selDtEnd { get => currFilter.DtEnd; set { if (!currFilter.DtEnd.Equals(value)) { currFilter.DtEnd = value; currPage = 1; } } } private SelectXdlParams currFilter { get; set; } = new SelectXdlParams(); private int currPage { get => currFilter.CurrPage; set => currFilter.CurrPage = value; } private PODLExpModel? currRecord { get => _currRecord; set { _currRecord = value; #if false artSearch = value == null ? "" : value.CodArticolo.Substring(0, 2); #endif } } private bool hasOdl { get => currFilter.HasOdl; set { if (currFilter.HasOdl != value) { currFilter.HasOdl = value; } } } private bool isLoading { get; set; } = false; private string macchina { get => currFilter.IdxMacchina; set => currFilter.IdxMacchina = value; } private int numRecord { get => currFilter.NumRec; set => currFilter.NumRec = value; } private string padCodXdl { get; set; } = "00000"; private string selReparto { get => currFilter.CodReparto; set => currFilter.CodReparto = value; } private string StatoSel { get => currFilter.CodFase; set => currFilter.CodFase = value; } private int totalCount { get => currFilter.TotCount; set => currFilter.TotCount = value; } private bool filtActive { get => macchina != "*" || StatoSel != "*" || selReparto != "*"; } #endregion Private Properties #region Private Methods /// /// Chiama metodo x chiedere sync DB /// /// /// private async Task callSyncDb(PODLModel selRec) { // chiamo aggiunta task SyncDb... string idxMacc = selRec.IdxMacchina; string restUrl = $"IOB/addTask2Exe/{idxMacc}?taskName=syncDbData&taskVal="; try { var response = await MpIoApiCall.callMpIoUrlGet(restUrl); } catch (Exception exc) { Log.Error($"Errore durante chiamata: {Environment.NewLine}{exc}"); } } private async Task reloadData() { isLoading = true; await Task.Delay(1); ListMacchine = await MDService.MacchineGetFilt(selReparto); ListArticoli = await MDService.ArticoliGetSearch(100, currAzienda, artSearch); if (ListGruppiFase != null) { var firstGroup = ListGruppiFase.Where(x => x.CodGruppo == selReparto).FirstOrDefault(); if (firstGroup != null) { currGruppoSel = firstGroup; } } isLoading = false; } protected string header { get => currFilter.Header; set => currFilter.Header = value; } private async Task updateFilter(SelectXdlParams newParams) { isLoading = true; await Task.Delay(1); currPage = 1; // salvo comunque filtro reparto x utente await localStorage.SetItemAsync("reparto", selReparto); await reloadData(); await Task.Delay(1); await InvokeAsync(() => StateHasChanged()); currFilter = newParams; isLoading = false; } #endregion Private Methods } }