using GenericParsing;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QW2021C.Resultate
{
    public partial class Resultat_Upload : System.Web.UI.Page
    {
        Helper.Log log = new Helper.Log();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Upload1_Complete(object sender, Syncfusion.JavaScript.Web.UploadBoxCompleteEventArgs e)
        {

            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Resultate hochgeladen " + e.Name);
            this.Tab1.SelectedItemIndex = 2;
            //this.target.Visible = true;
            this.hiddenfilename.Value= e.Name;

        }

        protected void UploadBox2_Init(object sender, EventArgs e)
        {
            this.lblError1.Text = "";
        }

        protected void btnweiter1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            try
            {
                if (this.cbboxOrt.Text == "")
                {
                    this.lblError1.Text = "Bitte ein Ort auswählen.";
                    return;
                }
            }
            catch
            {
                this.lblError1.Text = "Bitte ein Ort auswählen.";
                return;
            }
            
            clsDB db = new clsDB();
            db.Get_Tabledata("Select * from res_ort where ort = '" + cbboxOrt.Text.ToString() + "'", false, true);
            if (db.dsdaten.Tables[0].Rows.Count < 1)
            {
                this.lblError1.Text = "Eigegebener Ort ist ungültig!";
                return;
                        }
            this.hiddenortnr.Value =  db.dsdaten.Tables[0].Rows[0][0].ToString();
            this.hiddenortname.Value= db.dsdaten.Tables[0].Rows[0][1].ToString();
            this.hiddendeletedata.Value = this.chkdelresdata.Checked.ToString();
            this.Tab1.SelectedItemIndex = 1;
        }

        protected void btnweiter2_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            string filename = this.hiddenfilename.Value;
            string targetFolder = HttpContext.Current.Server.MapPath("~/Upload");
            filename = targetFolder + "\\" + filename;
            string fn;
            GenericParserAdapter Parser = new GenericParserAdapter(filename,System.Text.Encoding.GetEncoding("Windows-1252"));
            DataSet ds = new DataSet();

            Parser.ColumnDelimiter = Convert.ToChar(";");
            Parser.FirstRowHasHeader = true;
            
            ds = Parser.GetDataSet();

            clsDB db = new clsDB();

           
            db.truncate_table("dbo.res_taf_import");
            db.dsdaten.Tables.Clear();
            db.Get_Tabeldata_for_Update("Select * from dbo.res_taf_import", false, true);
            foreach (System.Data.DataRow r in ds.Tables[0].Rows )
            {
                System.Data.DataRow rn = db.dsdaten.Tables[0].NewRow();
                for (int i = 0; i < 24; i++)
                {
                   

                    rn[i] = r[i];
                   
                }
                rn[24] = this.hiddenortnr.Value.ToString();
                rn[25] = 0;
                db.dsdaten.Tables[0].Rows.Add(rn);
            }
            db.Update_Tabeldata();
            db.migrate_taf_resultate(this.hiddenortnr.Value, this.hiddendeletedata.Value);
            this.Tab1.SelectedItemIndex = 3;


        }

        protected byte[] GetCSVFileContent(string fileName)
        {
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(fileName, Encoding.Default, true))
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
            }
            string allines = sb.ToString();


            UTF8Encoding utf8 = new UTF8Encoding();


            var preamble = utf8.GetPreamble();

            var data = utf8.GetBytes(allines);


            return data;
        }
    }
}