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

namespace GMW.WebUserControls
{
    public partial class mod_listAL : System.Web.UI.UserControl
    {
        public event EventHandler eh_resetSelezione;
        public event EventHandler eh_selValore;
        protected string _idxGridView;
        protected string _tabCache;
        protected string sessKey
        {
            get
            {
                return string.Format("{0}_sel", _idxGridView);
            }
        }

        /// <summary>
        /// wrapper traduzione
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(object lemma)
        {
            return user_std.UtSn.Traduci(lemma.ToString());
        }
        /// <summary>
        /// traduce gli header delle colonne
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_DataBound(object sender, EventArgs e)
        {
            if (grView.Rows.Count > 0)
            {
                LinkButton lb;
                // aggiorno gli headers        
                foreach (TableCell cella in grView.HeaderRow.Cells)
                {
                    try
                    {
                        lb = (LinkButton)cella.Controls[0];
                        lb.Text = traduci(lb.Text);
                    }
                    catch
                    { }
                }
                int totRecord = grView.Rows.Count + grView.PageSize * (grView.PageCount - 1);
                lblNumRec.Text = string.Format("{0} records of ~ {1}", grView.Rows.Count, totRecord);
            }
            else
            {
                lblNumRec.Text = "";
            }
        }
        /// <summary>
        /// reset della selezione
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            resetSelezione();
        }
        /// <summary>
        /// gestione cambio selezione valore
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // salvo in session il valore selezionato...
            memLayer.ML.setSessionVal(sessKey, grView.SelectedValue, true);
            // sollevo evento nuovo valore...
            if (eh_selValore != null)
            {
                eh_selValore(this, new EventArgs());
            }
        }
        /// <summary>
        /// effettua update del modulo
        /// </summary>
        public void doUpdate()
        {
            resetSelezione();
            checkFixOds();
        }
        /// <summary>
        /// resetta la selezione dei valori in caso di modifiche su altri controlli
        /// </summary>
        public void resetSelezione()
        {
            SteamWare.memLayer.ML.emptySessionVal(sessKey);
            grView.SelectedIndex = -1;
            lblWarning.Visible = false;
            if (eh_resetSelezione != null)
            {
                eh_resetSelezione(this, new EventArgs());
            }
        }
        /// <summary>
        /// caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _idxGridView = "AL";
            if (!Page.IsPostBack)
            {
                doRefresh();
            }
        }
        /// <summary>
        /// elenco colonne del datagrid
        /// </summary>
        /// <returns></returns>
        protected DataColumnCollection colonneObj()
        {
            DS_magazzino.ElencoAssiemiDataTable tabella = new DS_magazzino.ElencoAssiemiDataTable();
            DataColumnCollection colonne = tabella.Columns;
            return colonne;
        }
        
        /// <summary>
        /// verifica se attivo filtro company e imposta ods di conseguenza...
        /// </summary>
        private void checkFixOds()
        {
            // controllo il tipo di ricerca e setto ods...
            if (memLayer.ML.BoolSessionObj("SearchIsLikeBased"))
            {
                // svuoto select parameters
                ods.SelectParameters.Clear();
                // imposto i parametri...
                ods.SelectParameters.Add("searchVal", DbType.String, memLayer.ML.StringSessionObj("FullSearchValue"));
                // metto nuovo metodo select
                ods.SelectMethod = "getByLikeSearch";
            }
            else
            {
                // svuoto select parameters
                ods.SelectParameters.Clear();
                // imposto i parametri...
                ods.SelectParameters.Add("AL", DbType.String, memLayer.ML.StringSessionObj("FullSearchValue"));
                // metto nuovo metodo select
                ods.SelectMethod = "getByAL";
            }
        }
        /// <summary>
        /// aggiorno ods al binding..
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ods_DataBinding(object sender, EventArgs e)
        {
            // fix dell'ODS
            checkFixOds();
        }
        /// <summary>
        /// fix ODS al cambio pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_PageIndexChanged(object sender, EventArgs e)
        {
            // fix dell'ODS
            grView.SelectedIndex = -1;
            checkFixOds();
        }
        /// <summary>
        /// fix ODS al sorting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_Sorting(object sender, GridViewSortEventArgs e)
        {
            // fix dell'ODS
            grView.SelectedIndex = -1;
            checkFixOds();
        }

        protected void grView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (memLayer.ML.BoolSessionObj("RicercaParticolariAttiva"))
            { 
                // se riga di dati...
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // ottengo  la riga tipizzata...
                    System.Data.DataRowView _drv = (System.Data.DataRowView)e.Row.DataItem;
                    DS_magazzino.V_ParticolariOverviewRow riga = (DS_magazzino.V_ParticolariOverviewRow)_drv.Row;
                    bool cond1 = SteamWare.memLayer.ML.StringSessionObj(sessKey) == riga.Particolare.ToString();
                    if (cond1)
                    {
                        grView.SelectedIndex = e.Row.RowIndex;
                    }
                }
            }
        }
        /// <summary>
        /// effettua refresh 8senza reset) del modulo
        /// </summary>
        public void doRefresh()
        {
            checkFixOds();
            grView.SelectedIndex = -1;
            grView.DataBind();
        }

    }
}