using DevExpress.Web;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace OlympiaIntranet
{
    public partial class DashboardControlli : System.Web.UI.Page
    {
        private string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager
                    .ConnectionStrings["OlympiaIntranet.Properties.Settings.SqlConnection"]
                    .ConnectionString;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/SignIn.aspx");
                return;
            }

            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        // ---------------------------------------------------------------
        // Necessario per i callback DevExpress (filtro, ordinamento, ecc.)
        // ---------------------------------------------------------------
        protected void gvDashboardAlert_DataBinding(object sender, EventArgs e)
        {
            gvDashboardAlert.DataSource = GetData();
        }

        // ---------------------------------------------------------------
        // Carica i dati e li assegna alla griglia
        // ---------------------------------------------------------------
        private void BindGrid()
        {
            gvDashboardAlert.DataSource = GetData();
            gvDashboardAlert.DataBind();
        }

        // ---------------------------------------------------------------
        // Recupera i dati dalla SP GDN_DashboardAlert
        // ---------------------------------------------------------------
        private DataTable GetData()
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(ConnectionString))
            using (SqlCommand cmd = new SqlCommand("GDN_DashboardAlert", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }

            return dt;
        }

        // ---------------------------------------------------------------
        // Export Excel - usa ASPxGridViewExporter dichiarato nel markup
        // ---------------------------------------------------------------
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            gvDashboardAlert.DataSource = GetData();
            gridExporter.WriteXlsxToResponse("DashboardAlert_" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx");
        }
    }
}
