using AppData;
using PdfSharp.Charting;
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.WebUserControls
{
    public partial class cmp_KS_BinCart : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fixVisibility();
            }
        }

        protected bool showBtns
        {
            get
            {
                return binOk && cartOk;
            }
        }
        protected bool binOk
        {
            get
            {
                return BinID > 0;
            }
        }
        protected bool cartOk
        {
            get
            {
                return CartID > 0;
            }
        }

        public int numMax
        {
            get
            {
                int answ = 0;
                int.TryParse(hfNumAll.Value, out answ);
                return answ;
            }
            set
            {
                hfNumAll.Value = value.ToString();
            }
        }

        private void fixVisibility()
        {
            // controllo visualizazione in base ai dati presenti
            cmp_KS_currBin.Visible = binOk;
            cmp_KS_currCart.Visible = showBtns;
            if (showBtns)
            {
                cmp_KS_OkibBin.Visible = false;
            }
            else
            {
                cmp_KS_OkibBin.Visible = binOk;
            }
            // se ho bin e cart --> mostro buttons calcolati!
            lbtAddAll.Visible = false;
            lbtAddOne.Visible = false;
            if (showBtns)
            {
                var tabOKIB = DLMan.taOKIB_Sum.getByKey(BinID, CartID);
                if (tabOKIB.Count > 0)
                {
                    numMax = tabOKIB[0].NumItems - tabOKIB[0].NumPicked;
                    lbtAddAll.Visible = true;
                    lbtAddOne.Visible = true;
                }
            }
        }

        public void doUpdate()
        {
            fixVisibility();
            if (binOk && !cartOk)
            {
                cmp_KS_OkibBin.doUpdate();
            }
        }

        /// <summary>
        /// Cart selezionato
        /// </summary>
        public int CartID
        {
            get
            {
                return cmp_KS_currCart.CurrId;
            }
            set
            {
                cmp_KS_currCart.CurrId = value;
            }
        }
        /// <summary>
        /// Matrix  Cart selezionato
        /// </summary>
        public string CartDtmx
        {
            get
            {
                return cmp_KS_currCart.Dtmx;
            }
            set
            {
                cmp_KS_currCart.Dtmx = value;
            }
        }
        /// <summary>
        /// Descrizione Cart selezionato (ord ext code)
        /// </summary>
        public string CartDesc
        {
            get
            {
                return cmp_KS_currCart.Description;
            }
            set
            {
                cmp_KS_currCart.Description = value;
            }
        }
        /// <summary>
        /// Bin selezionato
        /// </summary>
        public int BinID
        {
            get
            {
                return cmp_KS_currBin.CurrId;
            }
            set
            {
                cmp_KS_currBin.CurrId = value;
            }
        }
        /// <summary>
        /// Matrix Bin selezionato
        /// </summary>
        public string BinDtmx
        {
            get
            {
                return cmp_KS_currBin.Dtmx;
            }
            set
            {
                cmp_KS_currBin.Dtmx = value;
                cmp_KS_OkibBin.Dtmx = value;
            }
        }
        /// <summary>
        /// Descrizione BIN selezionato (ord ext code)
        /// </summary>
        public string BinDesc
        {
            get
            {
                return cmp_KS_currBin.Description;
            }
            set
            {
                cmp_KS_currBin.Description = value;
            }
        }

        protected void lbtAddOne_Click(object sender, EventArgs e)
        {
            DLMan.taPLog.acquirePaint(BinID, CartID, 1, currIpAddress, user_std.UtSn.userNameAD);
            lgInfo($"cmp_KS_BinCart | taPLog.acquirePaint | BinID : {BinID} | CartID: {CartID} | num: 1 | user: {user_std.UtSn.userNameAD} | IP: {currIpAddress}");
            // resetto
            CartID = 0;
            BinID = 0;
            // sollevo evento
            raiseEvent();
        }

        protected void lbtAddAll_Click(object sender, EventArgs e)
        {
            DLMan.taPLog.acquirePaint(BinID, CartID, numMax, currIpAddress, user_std.UtSn.userNameAD);
            lgInfo($"cmp_KS_BinCart | taPLog.acquirePaint | BinID : {BinID} | CartID: {CartID} | num: {numMax} | user: {user_std.UtSn.userNameAD} | IP: {currIpAddress}");
            // resetto
            CartID = 0;
            BinID = 0;
            // sollevo evento
            raiseEvent();
        }
    }
}