using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace QW2021C.Resultate
{
    public partial class Report : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            string report = Request.QueryString["rpt"].ToString();


            DataSet ds = new DataSet();
            ds.ReadXml(Session["Reportdaten"].ToString());
            string rptfilename = Server.MapPath("~\\Resultate\\Reports\\" + report.ToString());
            ReportDocument cr = new ReportDocument();
            cr.Load(rptfilename);

            int i;
            //FormulaFieldDefinitions crFormulaFieldDefinitions;
            //FormulaFieldDefinition crFormulaFieldDefinition;
            //FormulaFieldDefinition=crim
            //Section section;

            //int LoopFormula;
            //string FormulaName;
            //crFormulaFieldDefinitions = cr.DataDefinition.

            //for (LoopFormula = 0; LoopFormula <= crFormulaFieldDefinitions.Count - 1; LoopFormula++)
            //{
            //    crFormulaFieldDefinition = crFormulaFieldDefinitions[LoopFormula];
            //    FormulaName = crFormulaFieldDefinition.Name;
            //    Console.WriteLine(FormulaName);
            //}

            DataTable dt = new DataTable();
            switch (report)
            {
                case "Vierkampf.rpt":
 
                    foreach (DataColumn c in ds.Tables[1].Columns)
                    {
                        if (c.ColumnName.ToString() == "Rang" || c.ColumnName.ToString() == "BestPunkte")
                        {
                            dt.Columns.Add(c.ColumnName, System.Type.GetType("System.Int32"));
                        }
                        else
                        {
                            dt.Columns.Add(c.ColumnName);
                        }
                    }

                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        dt.ImportRow(dr);
                    }
                    CrystalDecisions.CrystalReports.Engine.TextObject MyTextObject;
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtheader0"] as TextObject;
                    MyTextObject.Text = Session["Kopfzeile1"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtheader1"] as TextObject;
                    MyTextObject.Text = Session["Kopfzeile1"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txttitel"] as TextObject;
                    MyTextObject.Text = Session["Titel"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtstadion"] as TextObject;
                    MyTextObject.Text = Session["Stadion"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtorganisator"] as TextObject;
                    MyTextObject.Text = Session["Organisator"].ToString();

                    MyTextObject = cr.ReportDefinition.ReportObjects["txtdatum"] as TextObject;
                    MyTextObject.Text = Session["Datum"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtwettkampfnr"] as TextObject;
                    MyTextObject.Text = Session["Wettkampfnr"].ToString();
                    cr.SetDataSource(dt);
                    CrystalReportViewer1.ReportSource = cr;
                    CrystalReportViewer1.RefreshReport();

                    CrystalDecisions.Shared.ConnectionInfo ci = new CrystalDecisions.Shared.ConnectionInfo();
                    break;
                case "ukc.rpt":
                   
                    foreach (DataColumn c in ds.Tables[1].Columns)
                    {
                        if (c.ColumnName.ToString() == "Rang" || c.ColumnName.ToString() == "BestPunkte")
                        {
                            dt.Columns.Add(c.ColumnName, System.Type.GetType("System.Int32"));
                        }
                        else
                        {
                            dt.Columns.Add(c.ColumnName);
                        }
                    }

                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        dt.ImportRow(dr);
                    }
                    //CrystalDecisions.CrystalReports.Engine.TextObject MyTextObject;
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtheader0"] as TextObject;
                    MyTextObject.Text = Session["Kopfzeile1"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtheader1"] as TextObject;
                    MyTextObject.Text = Session["Kopfzeile1"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txttitel"] as TextObject;
                    MyTextObject.Text = Session["Titel"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtstadion"] as TextObject;
                    MyTextObject.Text = Session["Stadion"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtorganisator"] as TextObject;
                    MyTextObject.Text = Session["Organisator"].ToString();

                    MyTextObject = cr.ReportDefinition.ReportObjects["txtdatum"] as TextObject;
                    MyTextObject.Text = Session["Datum"].ToString();
                    MyTextObject = cr.ReportDefinition.ReportObjects["txtwettkampfnr"] as TextObject;
                    MyTextObject.Text = Session["Wettkampfnr"].ToString();
                    cr.SetDataSource(dt);
                    CrystalReportViewer1.ReportSource = cr;
                    CrystalReportViewer1.RefreshReport();

                    //CrystalDecisions.Shared.ConnectionInfo ci = new CrystalDecisions.Shared.ConnectionInfo();
                    break;
                default:

                    string ortliste = "";
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        if (ortliste != "") ortliste = ortliste + ";";
                        ortliste = ortliste + dr[0].ToString();

                    }

                    
                    foreach (DataColumn c in ds.Tables[1].Columns)
                    {
                        if (c.ColumnName.ToString() == "Rang" || c.ColumnName.ToString() == "BestPunkte")
                        {
                            dt.Columns.Add(c.ColumnName, System.Type.GetType("System.Int32"));
                        }
                        else
                        {
                            dt.Columns.Add(c.ColumnName);
                        }
                    }

                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        dt.ImportRow(dr);
                    }


                    string[] splitter = ortliste.Split(';');

                    //int i;
                    for (i = 1; i < 9; i++)
                    {
                        try
                        {
                            MyTextObject = cr.ReportDefinition.ReportObjects["Text" + i.ToString()] as TextObject;
                            MyTextObject.Text = "";
                        }
                        catch
                        {

                        }

                    }

                    for (i = 1; i <= splitter.Length; i++)

                    {
                        // CrystalDecisions.CrystalReports.Engine.TextObject MyTextObject;
                      
                        try
                        {
                            MyTextObject = cr.ReportDefinition.ReportObjects["Text" + i.ToString()] as TextObject;
                            MyTextObject.Text = "";
                            MyTextObject.Text = splitter[i - 1].ToString();
                        }
                        catch
                        {

                        }

                    }
                    cr.SetDataSource(dt);
                    CrystalReportViewer1.ReportSource = cr;
                    CrystalReportViewer1.RefreshReport();

                    //CrystalDecisions.Shared.ConnectionInfo ci = new CrystalDecisions.Shared.ConnectionInfo();
                    break;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true) return;
        }

    }
}