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

namespace GMW.WebUserControls
{
    public partial class mod_checkDelibLogNc : System.Web.UI.UserControl
    {
        /// <summary>
        /// caricamento pagina!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                mod_righePag.numRowPag = memLayer.ML.cdvi("_righeDataGridShort");
                grView.PageSize = mod_righePag.numRowPag;
                ddlSelTipo.DataBind();
                doUpdate();
            }
            // aggancio eventi
            mod_selPeriodo.eh_doUpdate += mod_selPeriodo_eh_doUpdate;
            mod_righePag.eh_newNum += mod_righePag_eh_newNum;
        }

        void mod_righePag_eh_newNum(object sender, EventArgs e)
        {
            grView.PageSize = mod_righePag.numRowPag;
        }

        void mod_selPeriodo_eh_doUpdate(object sender, EventArgs e)
        {
            doUpdate();
        }
        /// <summary>
        /// fa update...
        /// </summary>
        public void doUpdate()
        {
            grView.DataBind();
        }
        /// <summary>
        /// wrapper traduzione
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(object lemma)
        {
            return user_std.UtSn.Traduci(lemma.ToString());
        }
        /// <summary>
        /// selezionato particolare
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlSelPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            doUpdate();
        }
        protected void ddlSelTipo_SelectedIndexChanged(object sender, EventArgs e)
        {
            doUpdate();
        }
        /// <summary>
        /// seleziona/deseleziona le righe indicate...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelAll_Click(object sender, EventArgs e)
        {
            // seleziono tutti i valori visibili nel datagrid
            CheckBox chkbox = ((CheckBox)sender);
            bool isChecked = chkbox.Checked;
            if (!isChecked)
            {
                chkbox.ToolTip = traduci("btnSelAll");
            }
            else
            {
                chkbox.ToolTip = traduci("btnDeselAll");
            }
            foreach (GridViewRow riga in grView.Rows)
            {
                ((CheckBox)riga.FindControl("chkSelect")).Checked = isChecked;
            }
            showDelibera(true);
        }
        /// <summary>
        /// determina se mostrare buttons x delibera
        /// </summary>
        /// <param name="doShow"></param>
        private void showDelibera(bool doShow)
        {
            lbSaveSel.Visible = doShow;
            lbCancel.Visible = doShow;
        }


        protected void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            // mostro area delibera...
            showDelibera(true);
        }

        /// <summary>
        /// salva elenco selezioni UDC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbSaveSel_Click(object sender, EventArgs e)
        {
            // ciclo grView e salvo le righe selezionate, e cancello le EVENTUALI deselezionate
            foreach (GridViewRow riga in grView.Rows)
            {
                if (((CheckBox)riga.FindControl("chkSelect")).Checked && ((CheckBox)riga.FindControl("chkSelect")).Visible)
                {
                    // recupero codice UDC...
                    string UDC = ((Label)riga.FindControl("lblUDC")).Text;
                    if (UDC != "")
                    {
                        // chiamo toggle selezione...
                        MagClass.magazzino.taCartellini.setCheckDL(UDC, MagClass.magazzino.CodSoggCurrUser);
                    }
                }
            }
            grView.DataBind();
            showDelibera(false);
        }
        /// <summary>
        /// annulla modifiche selezione
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbCancel_Click(object sender, EventArgs e)
        {
            grView.SelectedIndex = -1;
            grView.DataBind();
            showDelibera(false);
        }
        /// <summary>
        /// converte a boolean (fase se vuoto)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool checkVal(object value)
        {
            bool answ = false;
            try
            {
                answ = Convert.ToBoolean(value);
            }
            catch
            { }
            return answ;
        }
    }
}