using AppData;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_orderSched : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fixSchedOrd();
            }
        }

        protected int numRows
        {
            get
            {
                int answ = 0;
                int.TryParse(hfNumRows.Value, out answ);
                return answ;
            }
            set
            {
                hfNumRows.Value = value.ToString();
            }
        }

        private void fixSchedOrd()
        {
            numRows = DLMan.taBL.getSched().Count * 10;
            DLMan.taBL.fixSched();
        }


        protected void lbtUp_Click(object sender, EventArgs e)
        {
            int BatchId = 0;
            LinkButton lbtBtn = (LinkButton)sender;
            if (lbtBtn != null)
            {
                int.TryParse(lbtBtn.CommandArgument, out BatchId);
                moveBatch(BatchId, -15);
            }
        }
        protected void lbtDown_Click(object sender, EventArgs e)
        {
            int BatchId = 0;
            LinkButton lbtBtn = (LinkButton)sender;
            if (lbtBtn != null)
            {
                int.TryParse(lbtBtn.CommandArgument, out BatchId);
                moveBatch(BatchId, 15);
            }
        }

        private void moveBatch(int BatchId, int step)
        {
            // aggiorno
            DLMan.taBL.updateSchedPrior(BatchId, step);
            // ricalcolo
            fixSchedOrd();
            grView.SelectedIndex = -1;
            grView.DataBind();
            lblTitle.Focus();
        }
        /// <summary>
        /// Verifica visibilità da posizione
        /// </summary>
        /// <param name="mode">first/last</param>
        /// <param name="currPrior">num priorità attuale</param>
        /// <returns></returns>
        public bool checkVisible(string mode, object currPrior)
        {
            bool answ = true;
            int numPrior = 0;
            int.TryParse(currPrior.ToString(), out numPrior);
            if (mode == "first")
            {
                answ = (numPrior != 10);
            }
            else
            {
                answ = (numPrior != numRows);
            }
            return answ;
        }
        /// <summary>
        /// comando reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            fixSchedOrd();
            grView.SelectedIndex = -1;
            grView.DataBind();
            raiseReset();
        }

        protected void grView_SelectedIndexChanged(object sender, EventArgs e)
        {
            raiseEvent();
        }
        /// <summary>
        /// BatchId selezionato
        /// </summary>
        public int selBatchId
        {
            get
            {
                int answ = 0;
                int.TryParse(grView.SelectedValue.ToString(), out answ);
                return answ;
            }
        }
    }
}