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

namespace GMW.WebUserControls
{
    public partial class mod_elencoNC : System.Web.UI.UserControl
    {
        /// <summary>
        /// evento selezione/deselezione
        /// </summary>
        public event EventHandler eh_selValore;
        /// <summary>
        /// sollevo evento selezione /deselezione
        /// </summary>
        protected void raiseEvent()
        {
            if (eh_selValore != null)
            {
                eh_selValore(this, new EventArgs());
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            fixVisibility();
            if (!Page.IsPostBack)
            {
                mod_righePag.numRowPag = memLayer.ML.cdvi("_righeDataGrid");
                grView.PageSize = mod_righePag.numRowPag;
            }
            mod_righePag.eh_newNum += mod_righePag_eh_newNum;
        }

        void mod_righePag_eh_newNum(object sender, EventArgs e)
        {
            grView.PageSize = mod_righePag.numRowPag;
        }
        /// <summary>
        /// verifica se edit della NC sia permesso, controllando lo stato da DB (SE NON in sessione)
        /// </summary>
        /// <param name="_idxNC"></param>
        /// <returns></returns>
        public bool editEnabNC(object _idxNC)
        {
            int idxNC = 0;
            bool answ = false;
            try
            {
                idxNC = Convert.ToInt32(_idxNC);
                DS_Applicazione.ElencoNCRow riga = DataProxy.obj.taENC.getByKey(idxNC)[0];
                answ = !riga.IsCompl;
            }
            catch
            { }
            return answ;
        }
        /// <summary>
        /// idxNC selezionato
        /// </summary>
        public int idxNcSel
        {
            get
            {
                return memLayer.ML.QSI("idxNC");
                //return memLayer.ML.IntSessionObj("idxNcSel");
            }
            set
            {
                Response.Redirect(string.Format("{0}?idxNC={1}", user_std.pagCorrente, value));
                //memLayer.ML.setSessionVal("idxNcSel", value);
            }
        }
        /// <summary>
        /// Sistemazione visibilità controllo
        /// </summary>
        private void fixVisibility()
        {
            bool showElenco = false;
            // se ho un valore NC selezionato...
            if (idxNcSel > 0)
            {
                try
                {
                    int idxNC = Convert.ToInt32(grView.SelectedDataKey["IdxNC"]);
                    DS_Applicazione.ElencoNCRow riga = DataProxy.obj.taENC.getByKey(idxNC)[0];
                    lblSel.Text = string.Format("[{3}] Cod: {0} - {1} ({2:yyyy/MM/dd HH:mm})", riga.CodNC, riga.DescrNC, riga.DtCreaz, riga.IdxNC);
                }
                catch
                {
                    lblSel.Text = string.Format("IdxNC: {0}", idxNcSel);
                }
                showElenco = false;
                fixVisibDiv(showElenco);
            }
            // mostro elenco x selezionare
            else
            {
                showElenco = true;
                fixVisibDiv(showElenco);
            }
        }

        private void fixVisibDiv(bool showElenco)
        {
            divSelezione.Visible = !showElenco;
            divElenco.Visible = showElenco;
        }
        /// <summary>
        /// wrapper traduzione
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(object lemma)
        {
            return user_std.UtSn.Traduci(lemma.ToString());
        }

        protected void lbAdd_Click(object sender, EventArgs e)
        {
            DateTime adesso = DateTime.Now;
            DataProxy.obj.taENC.insertQuery(txtSearch.Text.Trim(), txtSearch.Text.Trim(), "", adesso.AddMonths(-1), adesso, "", "", adesso);
            Response.Redirect(user_std.pagCorrente);
            //idxNcSel = 0;
            //grView.SelectedIndex = 0;
            //grView.DataBind();
        }

        protected void lbReset_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            resetSelezione();
        }
        /// <summary>
        /// reset della selezione
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            resetSelezione();
        }
        /// <summary>
        /// faccio ricerca...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            //idxNcSel = 0;
            //grView.SelectedIndex = -1;
            //grView.DataBind();
            //fixVisibility();
            //raiseEvent();
        }
        /// <summary>
        /// resetto selezione
        /// </summary>
        private void resetSelezione()
        {
            Response.Redirect(user_std.pagCorrente);
        }
        /// <summary>
        /// selezionato valore IdxNC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // salvo chiave IdxNC
            idxNcSel = Convert.ToInt32(grView.SelectedDataKey["IdxNC"]);
            //fixVisibility();
            //raiseEvent();
        }

    }
}