using DevExpress.XtraReports.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// [02/02/2021] : Ho dovuto aggiungere sqlDataSource2 a ReportPortafoglio.repx perchè riferiva vecchia string connection altrimenti non funzionava sul server in produzione
///                non c'è modo di cancellare il vecchio sqlDataSource1
/// </summary>
namespace OlympiaIntranet
{
    public partial class ReportPortfoglioApi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }

            string cod_rap = Request.QueryString["cod_rap"];

            if (!String.IsNullOrEmpty(cod_rap))
            {
                // Esegue report
                string PdfFileName = string.Format("{0}_{1}.pdf", DateTime.Today.ToString("yyyyMMdd"),cod_rap);
                string serverPdfFileName = Server.MapPath("~/Reports/pdf/olympia/" + PdfFileName);

                XtraReport report = new XtraReport();
                report.LoadLayout(Server.MapPath("~/Reports/template/olympia/ReportPortafoglio.repx"));
                report.Parameters["cod_rap"].Value = cod_rap;
                report.CreateDocument(true);
                report.ExportToPdf(serverPdfFileName);

                // Legge report in byte[] array
                byte[] bytePDF = System.IO.File.ReadAllBytes(serverPdfFileName);

                // Restituisce pdf
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + PdfFileName);
                Response.BinaryWrite(bytePDF);
                Response.Flush();
                Response.End();
            }
        }
    }
}