using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.DataProtection; using Microsoft.JSInterop; using MP.Data.DbModels; using MP.SPEC.Data; namespace MP.SPEC.Pages { public partial class KIT : ComponentBase, IDisposable { #region Public Methods public void Dispose() { EditRecord = null; SearchRecords = null; ListRecords = null; GC.Collect(); } #endregion Public Methods #region Protected Fields protected string CodAzienda = "***"; protected int kitCount = 0; protected int totalCount = 0; #endregion Protected Fields #region Protected Properties [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; protected string sChildCss { get => string.IsNullOrEmpty(sCodChild) ? "btn-secondary" : "btn-primary"; } protected string SearchChild { get => sCodChild; set { // salvo solo se minChar+ chars if (value.Length >= minChar || string.IsNullOrEmpty(value)) { if (sCodChild != value) { sCodChild = value; currPage = 1; ListRecords = null; ReloadData(); } } } } protected string SearchParent { get => sCodParent; set { // salvo solo se minChar+ chars if (value.Length >= minChar || string.IsNullOrEmpty(value)) { if (sCodParent != value) { sCodParent = value; currPage = 1; ListRecords = null; ReloadData(); } } } } protected string sParentCss { get => string.IsNullOrEmpty(sCodParent) ? "btn-secondary" : "btn-primary"; } #endregion Protected Properties #region Protected Methods protected string checkSelect(TemplateKitModel currRec) { string answ = ""; if (EditRecord != null) { try { answ = (EditRecord.CodArtParent == currRec.CodArtParent && EditRecord.CodArtChild == currRec.CodArtChild) ? "table-info" : ""; } catch { } } return answ; } /// /// Crea nuovo record e va in editing... /// /// Genera codice numerico in automatico da counter /// protected async Task DoAddNew(bool AutoGen) { // imposto a ricerca x iniziare string codParent = SearchParent; // se deve essere autogenerato da counter... if (AutoGen) { var cntRec = MDService.AnagCountersGetNext("NumKitArt"); if (cntRec != null) { codParent = $"{cntRec.CntCode}{cntRec.LastNum:000000}"; } else { codParent = ""; } } EditRecord = new TemplateKitModel() { CodArtParent = codParent, CodArtChild = "", Qty = 1 }; SearchParent = codParent; await Task.Delay(1); } protected async Task DoCancel() { EditRecord = null; ReloadData(); await Task.Delay(1); } protected async Task DoClone(TemplateKitModel selRec) { // creo record duplicato... TemplateKitModel newRec = new TemplateKitModel() { CodArtParent = selRec.CodArtParent, CodArtChild = "",//selRec.CodArtParent, Qty = selRec.Qty }; EditRecord = newRec; SearchParent = selRec.CodArtParent; await Task.Delay(1); } /// /// Eliminazione record selezionato (previa conferma) /// /// /// protected async Task DoDelete(TemplateKitModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Eliminazione riga KIT: sei sicuro di voler procedere?")) return; await Task.Delay(1); var done = await MDService.TemplateKitDelete(selRec); EditRecord = null; ReloadData(); await Task.Delay(1); } /// /// Imposta filtro CodArtChild da record /// /// protected void DoFiltChild(TemplateKitModel selRec) { SearchChild = selRec.CodArtChild; } /// /// Imposta filtro CodArtParent da record /// /// protected void DoFiltParent(TemplateKitModel selRec) { SearchParent = selRec.CodArtParent; } protected async Task DoUpdate(TemplateKitModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) return; await Task.Delay(1); var done = await MDService.TemplateKitUpsert(selRec, CodAzienda); EditRecord = null; ReloadData(); await Task.Delay(1); } protected override void OnInitialized() { numRecord = 10; string rawVal = MDService.ConfigTryGet("SPEC_nArtSearch"); if (!string.IsNullOrEmpty(rawVal)) { int.TryParse(rawVal, out minChar); } rawVal = MDService.ConfigTryGet("AZIENDA"); if (!string.IsNullOrEmpty(rawVal)) { CodAzienda = rawVal; } rawVal = MDService.ConfigTryGet("SPEC_Kit_enabCount"); if (!string.IsNullOrEmpty(rawVal)) { bool.TryParse(rawVal, out enabKitCount); } rawVal = MDService.ConfigTryGet("SPEC_Kit_enabSearch"); if (!string.IsNullOrEmpty(rawVal)) { bool.TryParse(rawVal, out enabKitSearch); } rawVal = MDService.ConfigTryGet("SPEC_Kit_enabFreeCodArt"); if (!string.IsNullOrEmpty(rawVal)) { bool.TryParse(rawVal, out enabFreeCodArt); } } protected override void OnParametersSet() { ReloadData(); } protected void ResetChild() { SearchChild = ""; ReloadData(); } protected void ResetData() { EditRecord = null; } protected void ResetParent() { SearchParent = ""; ReloadData(); } protected void ResetSel() { EditRecord = null; } protected void SelRecord(TemplateKitModel selRec) { EditRecord = selRec; } protected void SetNumPage(int newNum) { currPage = newNum; } protected void SetNumRec(int newNum) { currPage = 1; numRecord = newNum; } protected void UpdateData() { EditRecord = null; ReloadData(); } #endregion Protected Methods #region Private Fields /// /// Boolear controllo visualizzazione ricerca articoli /// private bool doSearchArt = false; private TemplateKitModel? EditRecord = null; /// /// Abilita inserimento libero codice articolo /// private bool enabFreeCodArt = false; private bool enabKitCount = false; private bool enabKitSearch = false; private List? ListRecords; private int minChar = 2; private string sCodChild = ""; private List? SearchRecords; #endregion Private 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; ReloadData(); } } } private bool isLoading { get; set; } = false; private int numRecord { get => _numRecord; set { if (_numRecord != value) { _numRecord = value; ReloadData(); } } } private string sCodParent { get; set; } = ""; private bool ShowCharts { get; set; } = false; #endregion Private Properties #region Private Methods private void ReloadData() { isLoading = true; SearchRecords = MDService.TemplateKitFilt(SearchParent, SearchChild); totalCount = SearchRecords.Count; // conto i kit = distinct... kitCount = SearchRecords.GroupBy(x => x.CodArtParent).Count(); ListRecords = SearchRecords .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); isLoading = false; } /// /// Toggle modale ricerca articoli /// private void SearchArtToggle() { doSearchArt = !doSearchArt; } /// /// Salva selezione articolo /// /// private void SetCodArtChild(string codArt) { if (EditRecord != null) { EditRecord.CodArtChild = codArt; doSearchArt = false; } } #endregion Private Methods } }