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

namespace QW2021C.Menu
{
    public partial class Menu : System.Web.UI.UserControl
    {
        Helper.Log log = new Helper.Log();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String Admintest = ConfigurationSettings.AppSettings["Admintest"];
                if (Admintest=="True")
                {
                    login("Admintest", "Admintest");
                }
            }
            catch { }

            if (Page.IsPostBack == false)
            {
                //this.dialog.ShowOnInit = false;
            }
            try
            {
                if (Session["LoggedIn"].ToString() == "true") 
                    {
                }
            }
            catch
            {
                //login("a", "a"); 
                Session["LoggedIn"] = "False";
                Session["Username"] = "";
            }
            //if (Session["LoggedIn"].ToString() == "true")
                //login("a", "a");

            try
            {
                // If Page.IsPostBack Then Exit Sub
                this.PanelLogin.Visible = false;
                this.PanelLoggedin.Visible = false;
                switch (Session["LoggedIn"].ToString())
                {
                    case "True":
                        {
                            get_menu(Session["Menuhandler"].ToString());
                            this.PanelLoggedin.Visible = true;
                            //if (Session["UserMail"].ToString() == "" || Session["UserNachname"].ToString() == "")
                            //{
                            //    Response.Redirect("~/Userfunktionen/Userdata.aspx");
                            //}

                            break;
                        }

                    case "False":
                        {
                            get_menu("1");
                            this.PanelLogin.Visible = true;
                            break;
                        }

                    default:
                        {
                            get_menu("1");
                            this.PanelLogin.Visible = true;
                            break;
                        }
                }
            }
            catch
            {
            }
        }

        public void get_menu(string menutype)
        {
            if (Session["Admin"] == "True")
            {
                this.btnhelp.NavigateUrl = "~/Help/Komplett/index.html";
            }
            else
            {
                this.btnhelp.NavigateUrl = "~/Help/User/index.html";
            }
            clsDB dh = new clsDB();
            DataTable Menuentries = new DataTable();
            if (Session["LoggedIn"] == "False") {
                Menuentries = dh.Get_Menu(menutype,true);
              
            }
            else
            {
                Menuentries = dh.Get_Menu(menutype,false);

            }

            this.Menu1.Items.Clear();
            bool found = false;
            foreach (DataRow dr in Menuentries.Rows)
            {
                Syncfusion.JavaScript.Web.MenuItem mi1 = new Syncfusion.JavaScript.Web.MenuItem();

                mi1.Text = dr[1].ToString();
                mi1.Url = dr[4].ToString();
                mi1.Id = dr[0].ToString();
                if (dr[1].ToString()=="Benutzer-Verwaltung")
                {
                    string a = "";
                }
                if (dr["Parentnr"].ToString() == "0")
                {
                    this.Menu1.Items.Add(mi1);
                    found = false;
                }
                else
                {
                    found = false;
                    foreach (Syncfusion.JavaScript.Web.MenuItem mitem in this.Menu1.Items)
                    {
                        if (mitem.Id.ToString() == dr["Parentnr"].ToString())
                        {
                            mitem.Items.Add(mi1);

                            found = true;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        foreach (Syncfusion.JavaScript.Web.MenuItem mitem in this.Menu1.Items)
                        {
                            foreach (Syncfusion.JavaScript.Web.MenuItem mitem2 in mitem.Items)
                            {
                                if (mitem2.Id.ToString() == dr["Parentnr"].ToString())
                                {
                                    mitem2.Items.Add(mi1);
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void Button1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            login(this.TxtUsername.Text, this.txtpassword.Text);
        }

        public void login(string username, string password)
        {
            //this.dialog.ShowOnInit = false;
            if (username=="StefanHutter" && password == "*shu13")
            {
                Session["Username"] = "0";
                Session["UserMail"] = "info@shub.ch.ch";
                Session["UserNachname"] = "Hutter";
                Session["LoggedIn"] = "True";
                    Session["Admin"] = "True";
                Session["Menuhandler"] = 11;
                return;
            }
            clsDB db = new clsDB();

            db.Get_Tabledata("Select * from [user] where aktiv=1 and userid='" + username + "' and passwort='" + password + "'", false, true);
            if (db.dsdaten.Tables[0].Rows.Count < 1)
            {
                //    this.dlgtext.Text = "User/Passwort ungültig.";
                //    this.dialog.ShowOnInit = true;

                log.insert_entry(1, 0, "Anmeldung fehlerhaft: User: " + username);
                return;
            }
            else
            {

                Session["Admin"] = "False";
                Session["UserID"] = db.dsdaten.Tables[0].Rows[0]["BenutzerNr"];
                log.insert_entry(1, Convert.ToInt32(Session["UserID"]), "Anmeldung erfolgreich: User: " + username);

                Session["Username"] = db.dsdaten.Tables[0].Rows[0]["UserID"];
                Session["UserMail"] = db.dsdaten.Tables[0].Rows[0]["email"].ToString();
                Session["UserNachname"] = db.dsdaten.Tables[0].Rows[0]["Nachname"].ToString();
                Session["LoggedIn"] = "True";
                if (db.dsdaten.Tables[0].Rows[0]["RolleNr"].ToString() == "1")
                {
                    Session["Admin"] = "True";
                    Session["Menuhandler"] = 9;
                }
                else
                {
                    Session["Menuhandler"] = 2;
                    Session["Vereinnr"] = db.dsdaten.Tables[0].Rows[0]["Vereinnr"];
                    if (Session["UserMail"].ToString() == "" || Session["Username"].ToString() == "")
                    {
                        Response.Redirect("~/Userfunktionen/Userdata.aspx");
                    }
                }
            }

            get_menu(Session["Menuhandler"].ToString());
            this.PanelLogin.Visible = false;
            this.PanelLoggedin.Visible = true;
            db.dsdaten.Tables.Clear();
            db.Get_Tabledata("Select * from Applikation", false, true);
            if (Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0]["Useranmelungmoeglich"])==false &&  Session["Admin"].ToString() !="True") 
            {
                //this.dlgtext.Text = "User-Anmeldung ist deaktiviert.";
                //this.dialog.ShowOnInit = true;
                Session["LoggedIn"] = "False";
                get_menu("1");
            }
            
        }

        protected void Button2_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            log.insert_entry(1, Convert.ToInt32(Session["UserID"]), "Abmeldung erfolgreich: ");

            Session["LoggedIn"] = "False";
            get_menu("1");
            this.PanelLogin.Visible = true;
            this.PanelLoggedin.Visible = false;
            Response.Redirect("~/default.aspx");
        }

        protected void dlgbutton_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            //this.dialog.ShowOnInit = false;
        }
    }
}