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

namespace QW2021C.Resultate
{
    public partial class ResultatEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string Update_SQL(string sql, string attribut, string value)
        {
            if (value == "") return sql;
            if (sql.IndexOf("where") > 1)
            {
                sql=sql+" and "+attribut+"='"+value+"'";
            }
            else
            {
                sql = sql + " where " + attribut + "='" + value + "'";
            }
            return sql;
        }
        protected void btndataselect_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            string sql;
            sql = "Select * from res_resultate ";
            if (this.cbboxOrt.Text.ToString() !="")
            {
                db.Get_Tabledata("Select * from res_ort where ort = '" + cbboxOrt.Text.ToString() + "'", false, true);
                if (db.dsdaten.Tables[0].Rows.Count < 1)
                {
                    Helper.MessageBox.Show("Eigegebener Ort ist ungültig!");
                    return;
                }
                sql = Update_SQL(sql, "Ortnr", db.dsdaten.Tables[0].Rows[0][0].ToString());
                
                
            }
            sql = Update_SQL(sql, "Disziplin", this.txtdisziplin.Text.ToString());
            sql = Update_SQL(sql, "Kategorie", this.txtkategorie.Text.ToString());
            sql = Update_SQL(sql, "Nachname", this.txtName.Text.ToString());
            Resultatdaten.SelectCommand = sql;
            this.hiddensql.Value = sql;
            this.GridEditor.DataBind();

        }

        protected void GridEditor_ServerBatchEditRow(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
        {
                            ArrayList changed = ((ArrayList)((Dictionary<String, Object>)e.Arguments["batchChanges"])["changed"]);
                if (changed.Count > 0)
                    GridEdit("change", changed, e);
            
        }
        protected void GridEdit(string eventType, ArrayList changed, Syncfusion.JavaScript.Web.GridEventArgs e)
        {
            clsDB db = new clsDB();
            if (eventType == "change")
            {
                clsRes_Resultate value = new clsRes_Resultate();

                for (var i = 0; i <= changed.Count - 1; i++)
                {
                    Dictionary<string, object> KeyVal = changed[i] as Dictionary<string, object>;

                    foreach (KeyValuePair<string, object> keyvals in KeyVal)
                    {
                        if (keyvals.Key == "EintragNr") value.EintragNr = Convert.ToInt32(keyvals.Value);
                        if (keyvals.Key == "OrtNr") value.OrtNr = Convert.ToInt32(keyvals.Value);
                        if (keyvals.Key == "Rang") value.Rang = keyvals.Value.ToString();
                        if (keyvals.Key == "Nachname") value.Nachname = keyvals.Value.ToString();
                        if (keyvals.Key == "Vorname") value.Vorname = keyvals.Value.ToString();
                        if (keyvals.Key == "Jahrgang") value.Jahrgang = keyvals.Value.ToString();
                        if (keyvals.Key == "Land") value.Land = keyvals.Value.ToString();
                        if (keyvals.Key == "Verein") value.Verein = keyvals.Value.ToString();
                        if (keyvals.Key == "Lizenz") value.Lizenz = keyvals.Value.ToString();
                        if (keyvals.Key == "Kategorie_kurzname") value.Kategorie_kurzname = keyvals.Value.ToString();
                        if (keyvals.Key == "Kategorie") value.Kategorie = keyvals.Value.ToString();
                        if (keyvals.Key == "Position") value.Position = keyvals.Value.ToString();
                        if (keyvals.Key == "Bahn") value.Bahn = keyvals.Value.ToString();
                        if (keyvals.Key == "Qualifikation") value.Qualifikation = keyvals.Value.ToString();
                        if (keyvals.Key == "Serie") value.Serie = keyvals.Value.ToString();
                        if (keyvals.Key == "Disziplin") value.Disziplin = keyvals.Value.ToString();
                        if (keyvals.Key == "Leistung") value.Leistung = keyvals.Value.ToString();
                        if (keyvals.Key == "Info") value.Info = keyvals.Value.ToString();
                        if (keyvals.Key == "Punkte") value.Punkte = Convert.ToSingle(keyvals.Value);
                        if (keyvals.Key == "Wind") value.Wind = keyvals.Value.ToString();
                        if (keyvals.Key == "Nicht_Beruecksichtigen") value.Nicht_Beruecksichtigen = Convert.ToBoolean(keyvals.Value);
                        if (keyvals.Key == "Nur_Erdgas") value.Nur_Erdgas = Convert.ToBoolean(keyvals.Value);
                        if (keyvals.Key == "Team") value.Team = keyvals.Value.ToString();
                    }
                    db.Get_Tabeldata_for_Update("Select * from dbo.res_resultate  where [EintragNr]=" + value.EintragNr.ToString(), false, true);
                    db.dsdaten.Tables[0].Rows[0]["EintragNr"] = value.EintragNr;
                    db.dsdaten.Tables[0].Rows[0]["OrtNr"] = value.OrtNr;
                    db.dsdaten.Tables[0].Rows[0]["Rang"] = value.Rang;
                    db.dsdaten.Tables[0].Rows[0]["Nachname"] = value.Nachname;
                    db.dsdaten.Tables[0].Rows[0]["Vorname"] = value.Vorname;
                    db.dsdaten.Tables[0].Rows[0]["Jahrgang"] = value.Jahrgang;
                    db.dsdaten.Tables[0].Rows[0]["Land"] = value.Land;
                    db.dsdaten.Tables[0].Rows[0]["Verein"] = value.Verein;
                    db.dsdaten.Tables[0].Rows[0]["Lizenz"] = value.Lizenz;
                    db.dsdaten.Tables[0].Rows[0]["Kategorie_kurzname"] = value.Kategorie_kurzname;
                    db.dsdaten.Tables[0].Rows[0]["Kategorie"] = value.Kategorie;
                    db.dsdaten.Tables[0].Rows[0]["Position"] = value.Position;
                    db.dsdaten.Tables[0].Rows[0]["Bahn"] = value.Bahn;
                    db.dsdaten.Tables[0].Rows[0]["Qualifikation"] = value.Qualifikation;
                    db.dsdaten.Tables[0].Rows[0]["Serie"] = value.Serie;
                    db.dsdaten.Tables[0].Rows[0]["Disziplin"] = value.Disziplin;
                    db.dsdaten.Tables[0].Rows[0]["Leistung"] = value.Leistung;
                    db.dsdaten.Tables[0].Rows[0]["Info"] = value.Info;
                    db.dsdaten.Tables[0].Rows[0]["Punkte"] = value.Punkte;
                    db.dsdaten.Tables[0].Rows[0]["Wind"] = value.Wind;
                    db.dsdaten.Tables[0].Rows[0]["Nicht_Beruecksichtigen"] = value.Nicht_Beruecksichtigen;
                    db.dsdaten.Tables[0].Rows[0]["Nur_Erdgas"] = value.Nur_Erdgas;
                    db.dsdaten.Tables[0].Rows[0]["Team"] = value.Team;
                    db.Update_Tabeldata();
                    //log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Anmeldedaten gespeichert");

                }

                // Dim update As Orders = order.Where(Function(ord) ord.code = value.code).FirstOrDefault()
                // update.isSelected = value.isSelected
                this.Resultatdaten.SelectCommand = this.hiddensql.Value;
                this.GridEditor.DataBind();
            }
        }
    }
    public class clsRes_Resultate
    {
        public int EintragNr { get; set; }

        public int? OrtNr { get; set; }

        public string Rang { get; set; }

        public string Nachname { get; set; }

        public string Vorname { get; set; }

        public string Jahrgang { get; set; }

        public string Land { get; set; }

        public string Verein { get; set; }

        public string Lizenz { get; set; }

        public string Kategorie_kurzname { get; set; }

        public string Kategorie { get; set; }

        public string Position { get; set; }

        public string Bahn { get; set; }

        public string Qualifikation { get; set; }

        public string Serie { get; set; }

        public string Disziplin { get; set; }

        public string Leistung { get; set; }

        public string Info { get; set; }

        public float? Punkte { get; set; }

        public string Wind { get; set; }

        public bool? Nicht_Beruecksichtigen { get; set; }

        public bool? Nur_Erdgas { get; set; }

        public string Team { get; set; }

    }
}