using Syncfusion.JavaScript.Web;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QW2021C.Administration
{
    public partial class Kategorien : 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;

            }
            Grid1.DataBind();
        }

        protected void OnServer_Batcheditrow(object sender, GridEventArgs e)
        {
            ArrayList changed = ((ArrayList)((Dictionary<String, Object>)e.Arguments["batchChanges"])["changed"]);
            if (changed.Count > 0)
                GridAction("change", changed, e);
        }

        protected void GridAction(string eventType, ArrayList changed, Syncfusion.JavaScript.Web.GridEventArgs e)
        {
            clsDB db = new clsDB();
            if (eventType == "change")
            {
                Kategorie value = new Kategorie();

                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 == "Kategorie")
                            value.Kat = keyvals.Value.ToString();
                        if (keyvals.Key == "Aktiv")
                            value.aktiv = Convert.ToBoolean(keyvals.Value);
                        if (keyvals.Key == "KidCup")
                            value.KidCup = Convert.ToBoolean(keyvals.Value);
                    }
                    db.Get_Tabeldata_for_Update("Select * from kat where kategorie='" + value.Kat + "'", false, true);
                    db.dsdaten.Tables[0].Rows[0]["aktiv"] = value.aktiv;
                    db.dsdaten.Tables[0].Rows[0]["KidCup"] = value.KidCup;
                    db.Update_Tabeldata();
                }

                // Dim update As Orders = order.Where(Function(ord) ord.code = value.code).FirstOrDefault()
                // update.isSelected = value.isSelected
                this.Grid1.DataBind();
                log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Kategorien gespeichert");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var dataSource = this.Grid1.DataSource;
        }

        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void BatchUpdate(string key, List<Kategorie> changed, List<Kategorie> added, List<Kategorie> deleted)
        {
            clsDB db = new clsDB();



            if (changed != null && changed.Count() > 0)
            {
                foreach (var temp in changed)
                {
                    db.Get_Tabeldata_for_Update("Select * from kategorie where kategorie='" + temp.Kat + "'");
                    db.dsdaten.Tables[0].Rows[0]["aktiv"] = temp.aktiv;
                    db.Update_Tabeldata();
                }
            }
        }
    }

    public class Kategorie
    {
        public Kategorie()
        {
        }

        public Kategorie(string Kategorie, bool Aktiv, bool KidCup)
        {
            this.Kat = Kategorie;
            this.aktiv = Aktiv;
            this.KidCup = KidCup;
        }

        public string Kat { get; set; }
        public bool aktiv { get; set; }

        public bool KidCup { get; set; }
    }


}