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; 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 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(); } ListMacchine = await MDService.MacchineGetAll(); ListStati = await MDService.AnagStatiComm(); SearchRecords = await MDService.ListPODLFilt(hasOdl, StatoSel, macchina, selReparto); currAzienda = await MDService.tryGetConfig("AZIENDA"); padCodXdl = await MDService.tryGetConfig("padCodXdl"); // 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() { // aggiungo record articolo //if (SearchRecords != null) //{ // currRecordControlli = SearchRecords.FirstOrDefault(); //} //currArticolo = ""; if (ListArticoli != null && ListArticoli.Count > 0) { var firstArt = ListArticoli.FirstOrDefault(); currArticolo = firstArt != null ? firstArt.CodArticolo : ""; } 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); 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 List? ListArticoli; private List? ListAziende; private List? ListGruppiFase; private List? ListMacchine; private List? ListStati; private List? SearchRecords; #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 SelectXdlParams currFilter { get; set; } = new SelectXdlParams(); private int currPage { get => currFilter.CurrPage; set => currFilter.CurrPage = value; } private PODLExpModel? currRecord { get => _currRecord; set { _currRecord = value; artSearch = value == null ? "" : value.CodArticolo; } } 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; } #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); if (currAzienda != "*") { ListArticoli = await MDService.ArticoliGetSearch(100, currAzienda, artSearch); } else { ListArticoli = new List(); } isLoading = false; } 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 Task.Delay(1); await InvokeAsync(() => StateHasChanged()); currFilter = newParams; isLoading = false; } #endregion Private Methods } }