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

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

            }
            BindDataSource();
        }

        private List<Rolle> Rollen = new List<Rolle>();
        private List<Verein> Vereine = new List<Verein>();
        public void BindDataSource()
        {
            clsDB db = new clsDB();
            db.Get_Tabledata("Select irollenr, bezeichnung from rolle", false, true);
            foreach (DataRow r in db.dsdaten.Tables[0].Rows)
                Rollen.Add(new Rolle(r[0].ToString(), r[1].ToString()));

            var index = this.Grid1.Columns.FindIndex(col => col.Field == "RolleNr");
            this.Grid1.Columns.ElementAt(index).DataSource = Rollen;
            db.dsdaten.Tables.Clear();
            db.Get_Tabledata("Select account_code, account_code as account_code_bez from base_account", false, true);
            foreach (DataRow r in db.dsdaten.Tables[0].Rows)
                Vereine.Add(new Verein(r[0].ToString(), r[1].ToString()));
            var index1 = this.Grid1.Columns.FindIndex(col => col.Field == "Vereinnr");
            this.Grid1.Columns.ElementAt(index1).DataSource = Vereine;
        }

        [Serializable]
        public class Rolle
        {
            public Rolle()
            {
            }

            public Rolle(string RolleNr, string Bezeichnung)
            {
                this.RolleNr = RolleNr;
                this.Bezeichnung = Bezeichnung;
            }

            public string RolleNr { get; set; }
            public string Bezeichnung { get; set; }
        }

        public class Verein
        {
            public Verein()
            {
            }
            public Verein(string account_code, string account_code_bez)
            {
                this.account_code = account_code;
                this.account_code_bez = account_code_bez;
            }

            public string account_code { get; set; }
            public string account_code_bez { get; set; }
        }

        protected void Button1_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {

        }

        protected void btnGeneratePWk(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            db.generate_password(Session["Userid"].ToString(),0);
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "User für fehlende Vereine generiert");
            this.Grid1.DataBind();

        }

        protected void btnNewUser(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            db.add_user();
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "User hinzugefügt");
            this.Grid1.DataBind();

        }

        protected void Button3_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            clsDB db = new clsDB();
            db.generate_password(Session["Userid"].ToString(), 1);
            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Passworte für Benutzer generiert");

            this.Grid1.DataBind();
        }
    }

}