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

namespace GMW.WebUserControls
{
    public partial class mod_paretoUdcBlocco : ApplicationUserControl
    {
        #region area da NON modificare

        #region area protected

        protected string _idxGridView;
        protected string _idxGridViewExt;
        protected string _tabCache;

        /// <summary>
        /// aggiorna controlli datagrid e numero righe in pagina
        /// </summary>
        protected override void aggiornaControlliDataGL()
        {
            base.aggiornaControlliDataGL();
            grView.PageSize = _righeDataGridMed;
        }
        /// <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>
        /// collega i controlli
        /// </summary>
        protected override void bindControlli()
        {
            base.bindControlli();
            caricaTabelle();
        }

        #endregion

        #region gestione eventi

        public event EventHandler eh_resetSelezione;
        //public event EventHandler eh_nuovoValore;
        public event EventHandler eh_selValore;

        #endregion

        #endregion

        #region area codice variabile

        /// <summary>
        /// carico le tabelle
        /// </summary>
        private void caricaTabelle()
        {
        }
        /// <summary>
        /// elenco colonne del datagrid
        /// </summary>
        /// <returns></returns>
        protected DataColumnCollection colonneObj()
        {
            DS_magazzino.V_DettMagPartDataTable tabella = new DS_magazzino.V_DettMagPartDataTable();
            DataColumnCollection colonne = tabella.Columns;
            return colonne;
        }
        /// <summary>
        /// inizializzazione valori di default
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _idxGridView = "Particolare";
        }

        void mod_filtroMag_eh_selValore(object sender, EventArgs e)
        {
            // chiamo procedura che aggiorna ods principale
            checkFixOds();
        }
        /// <summary>
        /// segnalo nuovo valore filtro attivo...
        /// </summary>
        private void checkFixOds()
        {
            // controllo se devo filtrare x magazzino logico e di conseguenza setto l'ods...
            if (memLayer.ML.isInSessionObject("CodMagLogico_sel"))
            {
                // svuoto select parameters
                ods.SelectParameters.Clear();
                // imposto i parametri...
                ods.SelectParameters.Add("CodCS", DbType.String, memLayer.ML.StringSessionObj("CodCS"));
                ods.SelectParameters.Add("CodMag", DbType.String, memLayer.ML.StringSessionObj("CodMag_sel"));
                ods.SelectParameters.Add("IdxBlocco", DbType.Int32, memLayer.ML.StringSessionObj("IdxBlocco_sel"));
                ods.SelectParameters.Add("CodMagLog", DbType.Int32, memLayer.ML.StringSessionObj("CodMagLogico_sel"));
                // metto nuovo metodo select
                ods.SelectMethod = "getParetoByMagBloccoMagLog";
            }
            else
            {
                // svuoto select parameters
                ods.SelectParameters.Clear();
                // imposto i parametri...
                ods.SelectParameters.Add("CodCS", DbType.String, memLayer.ML.StringSessionObj("CodCS"));
                ods.SelectParameters.Add("CodMag", DbType.String, memLayer.ML.StringSessionObj("CodMag_sel"));
                ods.SelectParameters.Add("IdxBlocco", DbType.Int32, memLayer.ML.StringSessionObj("IdxBlocco_sel"));
                // metto nuovo metodo select
                ods.SelectMethod = "getParetoByMagBlocco";
            }
            grView.DataBind();
        }
        /// <summary>
        /// aggiorno visualizzazione
        /// </summary>
        private void updateOdsDaFiltro()
        {
            grView.DataBind();
        }
        /// <summary>
        /// chiamata post page change gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_PageIndexChanged(object sender, EventArgs e)
        {
            // sistemazione dell'ods post evento cambio pagina
            checkFixOds();
        }
        /// <summary>
        /// chiamata post sort gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_Sorted(object sender, EventArgs e)
        {
            // sistemazione dell'ods post evento sort
            checkFixOds();
        }

        /// <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(string.Format("{0}_sel", _idxGridView), grView.SelectedValue, true);
            // salvo anche codBlocco...
            string codMag = MagClass.magazzino.taBlocchi.getByIdx(memLayer.ML.IntSessionObj("IdxBlocco_sel"))[0].CodMag;
            memLayer.ML.setSessionVal("CodMag_sel", codMag, true);

            // sollevo evento nuovo valore...
            if (eh_selValore != null)
            {
                eh_selValore(this, new EventArgs());
            }
        }
        /// <summary>
        /// resetta la selezione dei valori in caso di modifiche su altri controlli
        /// </summary>
        public void resetSelezione()
        {
            memLayer.ML.emptySessionVal(string.Format("{0}_sel", _idxGridView));
            grView.SelectedIndex = -1;
            checkFixOds();
            grView.DataBind();
            lblWarning.Visible = false;
            if (eh_resetSelezione != null)
            {
                eh_resetSelezione(this, new EventArgs());
            }
        }
        /// <summary>
        /// effettua update del modulo
        /// </summary>
        public void doUpdate()
        {
            grView.SelectedIndex = -1;
            checkFixOds();
        }
        /// <summary>
        /// controllo se è da selezionare la riga...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // 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_DettMagPartRow riga = (DS_magazzino.V_DettMagPartRow)_drv.Row;
                if (SteamWare.memLayer.ML.StringSessionObj("Particolare_sel") == riga.Particolare.ToString())
                {
                    grView.SelectedIndex = e.Row.RowIndex;
                }
            }
        }

        #endregion
    }
}