using DevExpress.Export;
using DevExpress.Web;
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OlympiaIntranet
{
    public partial class Performance1Day : System.Web.UI.Page
    {
        List<string> listSuperUser = new List<string> {"fpisoni", "fcostalonga", "glicciardello"};
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            //{
            //    Response.Redirect("~/SignIn.aspx");
            //}
            //else
            //{
            //    //if (!IsPostBack)
            //    //{
            //    string loggedUser = HttpContext.Current.User.Identity.Name;

            //    loggedUser = "rcammarino"; // da togliere!!

            //    if (listSuperUser.Contains(loggedUser) == true) gridLookupGestore.Enabled = true;
            //    else
            //    {
            //        string Gestore = loggedUser.ToUpper().Substring(1);
            //        gridLookupGestore.Value = Gestore;
            //        gridLookupGestore.GridView.Selection.SelectRowByKey(Gestore);
            //        gridLookupGestore.Enabled = false;
            //    }

            //    Session["cod_ges"] = gridLookupGestore.Value; // serve per popolare gridlookupLinea!!
            //    Session["cod_adv"] = gridLookupAdvisor.Value; // serve per popolare gridlookupLinea!!
            //    Session["user"] = loggedUser; // serve per popolare gridlookupGestore!!
            //    //}
            //}

            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }
            else
            {
                string loggedUser = HttpContext.Current.User.Identity.Name;

                //if (!IsPostBack)
                //{
                //{
                    // Carica Gestori in gridLookupGestore
                    gridLookupGestore.DataSource = CommonClass.GetGestori();
                    gridLookupGestore.DataBind();

                    // Se il gestore è uno dei super user allora il gridlookup è sbloccato , altrimenti viene bloccato sul gestore stesso in modo che ogni gestore guarda i propri dati (da implementare questa soluzione anche in GPMDispRendRisk , GPMAssetAllocation perchè non funziona per come è implementata!)
                    if (listSuperUser.Contains(loggedUser) == true) gridLookupGestore.Enabled = true;
                    else
                    {
                        string Gestore = loggedUser.ToUpper().Substring(1);
                        gridLookupGestore.Value = Gestore;
                        gridLookupGestore.Enabled = false;
                    }

                    SetSessionGestore();
                    //SetSessionAdvisor();

                    // Carica Performance 1Day in gvPerformance1Day
                    BindGridView();
                //}
            }
        }

        //private DataTable GetAdvisor()
        //{
        //    string query = "GDN_GetAdvisor";
        //    ASPxGridView grid = gridLookupGestore.GridView;
        //    object objGestore = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Gestore" });
        //    if (objGestore == null) return null;

        //    using (SqlConnection conn = new SqlConnection(OlympiaIntranet.Properties.Settings.Default.SqlConnection))
        //    {
        //        using (SqlCommand cmd = new SqlCommand(query, conn))
        //        {
        //            cmd.CommandType = CommandType.StoredProcedure;
        //            cmd.Parameters.Add(new SqlParameter("@cod_ges", SqlDbType.VarChar) { Value = (string)objGestore });

        //            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        //            {
        //                DataTable dt = new DataTable();
        //                da.Fill(dt);
        //                return dt;
        //            }
        //        }
        //    }
        //}



        protected void gridLookupGestore_ValueChanged(object sender, EventArgs e)
        {
            // ref: https://supportcenter.devexpress.com/ticket/details/q340370/aspxgridlookup-how-to-refresh-the-control-after-some-data-is-changed
            // note: Per refreshare gridlookupLinea e gridlookupAdvisor sulla base del valore di gridLookupGestore, salvo il valore gestore in una variabile di sessione Session["cod_ges"] 
            //       e poi il refresh/rebind di SqlDSLinee e SqlDSAdvisor verrà effettuato nell'evento lato client EndCallBack

            SetSessionGestore();
        }

        private void SetSessionGestore()
        {
            ASPxGridView grid = gridLookupGestore.GridView;
            object objGestore = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Gestore" });

            if (objGestore != null)
            {
                Session["cod_ges"] = (string)objGestore;
            }
        }

        //protected void gridLookupAdvisor_ValueChanged(object sender, EventArgs e)
        //{
        //    SetSessionAdvisor();
        //}

        //private void SetSessionAdvisor()
        //{
        //    ASPxGridView grid = gridLookupAdvisor.GridView;
        //    object objAdvisor = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Advisor" });

        //    if (objAdvisor != null)
        //    {
        //        Session["cod_adv"] = (string)objAdvisor;
        //    }
        //}

        protected void buttonCaricaDati_Click(object sender, EventArgs e)
        {
            // Carica Performance 1Day in gvPerformance1Day
            //gvPerformance1Day.KeyFieldName = "Portafoglio";
            //gvPerformance1Day.DataSource = GetPerformance1Day();
            //gvPerformance1Day.DataBind();

            BindGridView();
        }

        private DataTable GetPerformance1Day()
        {
            string query = "GDN_GetPerformance1Day1";
            ASPxGridView grid = gridLookupGestore.GridView;

            object objGestore = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Gestore" });
            grid = gridLookupLinea.GridView;
            object objLinea = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Linea" });
            //grid = gridLookupAdvisor.GridView;
            //object objAdvisor = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "Advisor" });
            //if (!(objGestore != null && objLinea != null && objAdvisor != null)) return null;
            if (!(objGestore != null && objLinea != null)) return null;

            using (SqlConnection conn = new SqlConnection(OlympiaIntranet.Properties.Settings.Default.SqlConnection))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    // Aggiungi i parametri
                    cmd.Parameters.Add(new SqlParameter("@cod_ges", SqlDbType.VarChar) { Value = (string)objGestore });
                    //cmd.Parameters.Add(new SqlParameter("@cod_adv", SqlDbType.VarChar) { Value = (string)objAdvisor });
                    cmd.Parameters.Add(new SqlParameter("@cod_lin", SqlDbType.VarChar) { Value = (string)objLinea });
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        private void BindGridView()
        {
            DataTable dt = GetPerformance1Day();
            if (dt == null) return;
            gvPerformance1Day.DataSource = dt;

            gvPerformance1Day.Columns.Clear();

            foreach (DataColumn column in dt.Columns)
            {
                    GridViewDataTextColumn gridColumn = new GridViewDataTextColumn();
                    gridColumn.FieldName = column.ColumnName;
                    gridColumn.Caption = column.ColumnName;
                    gvPerformance1Day.Columns.Add(gridColumn);
            }

            gvPerformance1Day.DataBind();
        }

        protected void gvPerformance1Day_DataBound(object sender, EventArgs e)
        {

        }

        protected void buttonEsportaExcel_Click(object sender, EventArgs e)
        {
            BindGridView(); // Rieffettua il bind alla griglia altrimenti non visualizza niente nell'excel!!
            ASPxGridViewExporter1.ExportSelectedRowsOnly = false;
            ASPxGridViewExporter1.FileName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - Performance 1 Day";
            XlsxExportOptionsEx op = new XlsxExportOptionsEx() { ExportType = ExportType.WYSIWYG };
            ASPxGridViewExporter1.WriteXlsxToResponse(op);
        }
    }
}