using AppData;
using NKC_WF.WebUserControls;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.site
{
    public partial class PartMIA : BasePage
    {
        #region Protected Fields

        /// <summary>
        /// Codice macchina (HARD CODED x ora)
        /// </summary>
        protected string machine = "WRK001";

        #endregion Protected Fields

        #region Protected Properties

        /// <summary>
        /// Bin richiesto
        /// </summary>
        protected int BinID
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = memLayer.ML.QSI("BinID");
                }
                catch
                { }
                return answ;
            }
        }

        /// <summary>
        /// Cart richiesto
        /// </summary>
        protected int CartID
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = memLayer.ML.QSI("CartID");
                }
                catch
                { }
                return answ;
            }
        }

        /// <summary>
        /// PackListID richiesta
        /// </summary>
        protected int PLID
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = memLayer.ML.QSI("PLID");
                }
                catch
                { }
                return answ;
            }
        }

        protected int savedBatchId
        {
            get
            {
                return ComLib.getCurrBatchId(machine);
            }
        }

        #endregion Protected Properties

        #region Public Properties

        /// <summary>
        /// BatchId corrente...
        /// </summary>
        public int BatchId
        {
            set
            {
                hfBatchID.Value = value.ToString();
                cmp_partMIA.BatchId = value;
                lblBatchId.Text = $"({value})";
                lblBatchId.Visible = value > 0;
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchID.Value, out answ);
                return answ;
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void cmp_numRow_eh_doRefresh(object sender, EventArgs e)
        {
            cmp_partMIA.numRow = cmp_numRow.numRow;
            cmp_partMIA_byPLID.numRow = cmp_numRow.numRow;
            cmp_PartMIA_byCartID.numRow = cmp_numRow.numRow;
            cmp_PartMIA_byBinID.numRow = cmp_numRow.numRow;
        }

        /// <summary>
        /// Aggiorna componente principale e child components
        /// </summary>
        private void doUpdate()
        {
            if (PLID > 0)
            {
                cmp_partMIA.Visible = false;
                cmp_PartMIA_byBinID.Visible = false;
                cmp_PartMIA_byCartID.Visible = false;
                cmp_partMIA_byPLID.Visible = true;
                cmp_partMIA_byPLID.PLID = PLID;
                cmp_partMIA_byPLID.doUpdate();
            }
            else if (CartID > 0)
            {
                cmp_partMIA.Visible = false;
                cmp_PartMIA_byBinID.Visible = false;
                cmp_partMIA_byPLID.Visible = false;
                cmp_PartMIA_byCartID.Visible = true;
                cmp_PartMIA_byCartID.CartID = CartID;
                cmp_PartMIA_byCartID.doUpdate();
            }
            else if (BinID > 0)
            {
                cmp_partMIA.Visible = false;
                cmp_partMIA_byPLID.Visible = false;
                cmp_PartMIA_byCartID.Visible = false;
                cmp_PartMIA_byBinID.Visible = true;
                cmp_PartMIA_byBinID.BinID = BinID;
                cmp_PartMIA_byBinID.doUpdate();
            }
            else
            {
                cmp_PartMIA_byBinID.Visible = false;
                cmp_PartMIA_byCartID.Visible = false;
                cmp_partMIA_byPLID.Visible = false;
                cmp_partMIA.Visible = true;
                // recupero bunk corrente...
                DS_App.StackListRow currBunk = ComLib.getCurrBunk(PlaceCod);
                if (currBunk != null)
                {
                    bool chgBtLocal = BatchId != currBunk.BatchID;
                    bool chgBtCache = savedBatchId != currBunk.BatchID;
                    if (chgBtLocal || chgBtCache)
                    {
                        BatchId = currBunk.BatchID;
                    }
                }
                else
                {
                    BatchId = 0;
                }
                cmp_partMIA.doUpdate();
            }
        }

        #endregion Private Methods

        #region Protected Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((SiteMaster)this.Master).showSearch = true;
                doUpdate();
            }
            cmp_numRow.eh_doRefresh += cmp_numRow_eh_doRefresh;
        }

        /// <summary>
        /// Main timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void timerMain_Tick(object sender, EventArgs e)
        {
            // ora faccio verifiche
            doUpdate();
        }

        #endregion Protected Methods
    }
}