using GMW_data;
using SteamWare;
using System;
using System.Web.UI;

namespace GMW.WebUserControls
{
    public partial class mod_operazioniUdc : System.Web.UI.UserControl
    {
        protected int idxCella { get; set; }
        protected string CodCella { get; set; }


        /// <summary>
        /// caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                traduciObj();
            }
            mod_UdcCella1.eh_nuovoValore += new EventHandler(mod_UdcCella1_eh_nuovoValore);
            mod_UdcCella1.eh_nuovaRicerca += new EventHandler(mod_UdcCella1_eh_nuovaRicerca);
            disegnaControlli();
        }

        void mod_UdcCella1_eh_nuovaRicerca(object sender, EventArgs e)
        {
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }

        void mod_UdcCella1_eh_nuovoValore(object sender, EventArgs e)
        {
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }

        #region eventi esposti

        /// <summary>
        /// Evento al cambio di quantità UDC
        /// </summary>
        public event EventHandler eh_updated;
        /// <summary>
        /// crea evento x nascondere il controllo
        /// </summary>
        public event EventHandler eh_hidden;

        #endregion

        /// <summary>
        /// aggiorno i controlli
        /// </summary>
        private void disegnaControlli()
        {
            lodaSessionData();
            lblCella.Text = string.Format("{0}: <b>{1}</b>", traduci("CodCella"), CodCella);
            setupButtons(idxCella, CodCella);
        }
        /// <summary>
        /// ridisegna dettagli UDC x cella selezionata
        /// </summary>
        private void resetUdcDetail()
        {
            mod_UdcCella1.resetSelezione();
        }
        /// <summary>
        /// carica dati da sessione
        /// </summary>
        private void lodaSessionData()
        {
            // cerco i dati della cella indicata
            idxCella = memLayer.ML.IntSessionObj("IdxCellaMag");
            CodCella = memLayer.ML.StringSessionObj("CodCellaMag");
        }
        /// <summary>
        /// traduzioni
        /// </summary>
        private void traduciObj()
        {
            // sistemo labels e buttons
            btnBlocca.Text = traduci("btnBloccaCella");
            btnSblocca.Text = traduci("btnSbloccaCella");
            btnCompleta.Text = traduci("btnCompletaCella");
            btnDisponibile.Text = traduci("btnDisponibileCella");
            btnClose.Text = traduci("btnClose");
        }
        /// <summary>
        /// determina abilitazioni bottoni x cella
        /// </summary>
        /// <param name="idxCella"></param>
        /// <param name="CodCella"></param>
        private void setupButtons(int idxCella, string CodCella)
        {
            // solo se ho valori...
            if (idxCella > 0 && CodCella != "")
            {
                DS_magazzino.CelleRow rigaCella = MagClass.magazzino.taCelle.getByCodCella(CodCella)[0];
                btnBlocca.Enabled = rigaCella.Attiva;
                btnSblocca.Enabled = !rigaCella.Attiva;
                btnCompleta.Enabled = !rigaCella.Piena;
                btnDisponibile.Enabled = rigaCella.Piena;
                btnBlocca.Visible = true;
                btnSblocca.Visible = true;
                btnCompleta.Visible = true;
                btnDisponibile.Visible = true;
            }
            else
            {
                btnBlocca.Visible = false;
                btnSblocca.Visible = false;
                btnCompleta.Visible = false;
                btnDisponibile.Visible = false;
            }
        }
        /// <summary>
        /// forza update controllo
        /// </summary>
        public void doUpdate()
        {
            disegnaControlli();
        }
        /// <summary>
        /// wrapper traduzione
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(object lemma)
        {
            return user_std.UtSn.Traduci(lemma.ToString());
        }


        #region metodi update cella

        /// <summary>
        /// blocca la cella indicata
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBlocca_Click(object sender, EventArgs e)
        {
            MagClass.magazzino.taCelle.stp_celle_updAttiva(memLayer.ML.IntSessionObj("IdxCellaMag"), false);
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }
        /// <summary>
        /// sblocca la cella indicata
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSblocca_Click(object sender, EventArgs e)
        {
            MagClass.magazzino.taCelle.stp_celle_updAttiva(memLayer.ML.IntSessionObj("IdxCellaMag"), true);
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }
        /// <summary>
        /// indica come completa la cella indicata
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCompleta_Click(object sender, EventArgs e)
        {
            MagClass.magazzino.taCelle.stp_celle_updPiena(memLayer.ML.IntSessionObj("IdxCellaMag"), true);
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }
        /// <summary>
        /// indica come disponibile la cella indicata
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDisponibile_Click(object sender, EventArgs e)
        {
            MagClass.magazzino.taCelle.stp_celle_updPiena(memLayer.ML.IntSessionObj("IdxCellaMag"), false);
            if (eh_updated != null)
            {
                eh_updated(this, new EventArgs());
            }
        }
        /// <summary>
        ///  resetta e chiude...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            // elimina da session...
            memLayer.ML.emptySessionVal("IdxCellaMag");
            resetUdcDetail();
            if (eh_hidden != null)
            {
                eh_hidden(this, new EventArgs());
            }
        }

        #endregion


    }
}