using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data.DatabaseModels; using MP.SPEC.Components; using MP.SPEC.Data; using MP.SPEC.Services; namespace MP.SPEC.Pages { public partial class PODL { #region Protected Fields protected DataPager pagerODL; protected bool reqNew = false; #endregion Protected Fields #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; [Inject] protected IOApiService MpIoApiCall { get; set; } = null!; [Inject] protected MessageService MsgService { 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 PODLModel() { 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 override async Task OnInitializedAsync() { // abilito ricerca... MsgService.ShowSearch = true; // resetto search MsgService.SearchVal = ""; ListAziende = await MDService.ElencoAziende(); ListGruppiFase = await MDService.ElencoGruppiFase(); ListMacchine = await MDService.MacchineGetAll(); ListStati = await MDService.AnagStatiComm(); // preselezione valori configData = await MDService.ConfigGetAll(); var currRec = configData.FirstOrDefault(x => x.Chiave == "AZIENDA"); if (currRec != null) { currAzienda = currRec.Valore; } // carico dati await reloadData(); } protected async Task pgResetReq(bool doReset) { if (doReset) { await pagerODL.resetCurrPage(); } } /// /// Crea nuovo record e va in editing... /// /// protected async Task reqNewPODL() { // aggiungo record articolo currArticolo = ""; if (ListArticoli != null && ListArticoli.Count > 0) { var firstArt = ListArticoli.FirstOrDefault(); currArticolo = firstArt != null ? firstArt.CodArticolo : ""; } string codExt = $"{selStato}"; 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 PODLModel() { 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(PODLModel selRec) { currRecord = selRec; await Task.Delay(1); } protected async Task update(PODLModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) return; await Task.Delay(1); var done = await MDService.POdlUpdateRecord(selRec); await callSyncDb(selRec); currRecord = null; await reloadData(); await Task.Delay(1); } protected void UpdateTotCount(int newTotCount) { totalCount = newTotCount; } #endregion Protected Methods #region Private Fields private PODLModel? _currRecord = null; 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 => selStato != "*"; } 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 List? configData { get; set; } = null; 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 int currPage { get => MsgService.currPage; set => MsgService.currPage = value; } private PODLModel? currRecord { get => _currRecord; set { _currRecord = value; artSearch = value == null ? "" : value.CodArticolo; } } private bool isLoading { get; set; } = false; private int numRecord { get => MsgService.numRecord; set => MsgService.numRecord = value; } private string selStato { get => MsgService.StateSel; set => MsgService.StateSel = value; } private int totalCount { get => MsgService.totalCount; set => MsgService.totalCount = 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="; var response = await MpIoApiCall.callMpIoUrlGet(restUrl); } 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; } #endregion Private Methods } }