using Syncfusion.XlsIO;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QW2021C.Administration
{
    public partial class ExportAnmeldung : System.Web.UI.Page
    {
        Helper.Log log = new Helper.Log();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["LoggedIn"].ToString() == "False")
                {
                    Response.Redirect("~/default.aspx");
                    return;
                }
            }
            catch
            {
                Response.Redirect("~/default.aspx");
                return;

            }
            if (Page.IsPostBack == true) return;
            clsDB db = new clsDB();
            db.Get_Tabledata("Select * from wettkampf", false, true);
            this.lblwettkampf1.Text = db.dsdaten.Tables[0].Rows[0]["Wettkampf1"].ToString();
            this.lblwettkampf2.Text = db.dsdaten.Tables[0].Rows[0]["Wettkampf2"].ToString();
            this.lblwettkampf3.Text = db.dsdaten.Tables[0].Rows[0]["Wettkampf3"].ToString();
            this.lblwettkampf4.Text = db.dsdaten.Tables[0].Rows[0]["Wettkampf4"].ToString();

        }

        protected void btnexport_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            db.Get_Tabledata("Select sqlscript from sqlqueries where sqlscriptnumber=2", false, true);
            string sql;
            sql = db.dsdaten.Tables[0].Rows[0][0].ToString();
            string where = "";
            string op = " or ";
            if (this.chkwettkampf1.Checked==true)
            {
                if (where != "") where = where + op;
                where +=   " wettkampf1 = 1 ";
            }
            if (this.chkwettkampf2.Checked == true)
            {
                if (where != "") where = where + op;
                where +=  " wettkampf2 = 1 ";
            }
            if (this.chkwettkampf3.Checked == true)
            {
                if (where != "") where = where + op;
                where +=  " wettkampf3 = 1 ";
            }
            if (this.chkwettkampf4.Checked == true)
            {
                if (where != "") where = where + op;
                where += " wettkampf4 = 1 ";
            }
            if (where != "") where = " (" + where + ") ";

            if (this.chkallevereine.Checked != true)

            {
                if (where != "") where = where + " and ";
                where += " dbo.anmeldung.vereinnr='" + this.DropDownList1.Value.ToString() + "'";
            }

            if (where =="")
            {
                where = " where dbo.anmeldung.aktiv= 1";
            }
            else
            {
                where = " where dbo.anmeldung.aktiv= 1 and " + where;
            }
            //Helper.MessageBox.Show(where);
            sql += where;



            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Initialize Application
                IApplication application = excelEngine.Excel;

                //Set the default application version as Excel 2016
                application.DefaultVersion = ExcelVersion.Excel2016;

                //Create a new workbook
                IWorkbook workbook = application.Workbooks.Create(1);

                //Access first worksheet from the workbook instance
                IWorksheet worksheet = workbook.Worksheets[0];

                //Exporting DataTable to worksheet
                clsDB db1 = new clsDB();
                db1.Get_Tabledata(sql,false,true);

                DataTable dataTable = db1.dsdaten.Tables[0];
                worksheet.ImportDataTable(dataTable, true, 1, 1);
                worksheet.UsedRange.AutofitColumns();

                string excelname = "";
                excelname = DateTime.Now.ToString("ddMMyyyyHHmmSS") + "_Anmeldungen.xlsx";
                //Save the workbook to disk in xlsx format
                string targetFolder = HttpContext.Current.Server.MapPath("~/Downloads");
                this.Button1.Enabled = false;
                this.HiddenField1.Value = "Anmeldungen.xlsx";

                workbook.SaveAs(targetFolder + "\\"+excelname);
                workbook.Close();
                
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenExcel", "window.open(\"~/Downloads/Output.xlsx\");", true);
                //DownloadFile("Output.xlsx", targetFolder + "\\Output.xlsx");
                this.HyperLink1.NavigateUrl = "~/Administration/Downloader.aspx?fn="+excelname;
                this.HyperLink1.Target = "_blank";
                this.HyperLink1.Text = "Download";
                this.HyperLink1.Visible = true;
                log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Export Anmeldungen");
                log.insert_entry(2, Convert.ToInt32(Session["UserID"]), sql);

            }
        }
    


        protected void Button1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            db.Get_Tabledata("Select sqlscript from sqlqueries where sqlscriptnumber=3", false, true);
            string sql;
            sql = db.dsdaten.Tables[0].Rows[0][0].ToString();
            

            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Initialize Application
                IApplication application = excelEngine.Excel;

                //Set the default application version as Excel 2016
                application.DefaultVersion = ExcelVersion.Excel2016;

                //Create a new workbook
                IWorkbook workbook = application.Workbooks.Create(1);

                //Access first worksheet from the workbook instance
                IWorksheet worksheet = workbook.Worksheets[0];

                //Exporting DataTable to worksheet
                clsDB db1 = new clsDB();
                db1.Get_Tabledata(sql, false, true);

                DataTable dataTable = db1.dsdaten.Tables[0];
                worksheet.ImportDataTable(dataTable, true, 1, 1);
                worksheet.UsedRange.AutofitColumns();
                string excelname = "";
                excelname = DateTime.Now.ToString("ddMMyyyyHHmmSS") + "_AlleAnmeldungen.xlsx";

                //Save the workbook to disk in xlsx format
                string targetFolder = HttpContext.Current.Server.MapPath("~/Downloads");
                //this.Button1.Enabled = false;
                this.HiddenField1.Value = "Anmeldungen_AlleDaten.xlsx";

                workbook.SaveAs(targetFolder + "\\"+excelname);
                workbook.Close();

                //Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenExcel", "window.open(\"~/Downloads/Output.xlsx\");", true);
                //DownloadFile("Output.xlsx", targetFolder + "\\Output.xlsx");
                this.HyperLink2.NavigateUrl = "~/Administration/Downloader.aspx?fn="+excelname;
                this.HyperLink2.Target = "_blank";
                this.HyperLink2.Text = "Download";
                this.HyperLink2.Visible = true;
                log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Export alle Anmeldungen ");

            }

        }

 
    }
}