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

namespace QW2021C.UserFunktionen
{
    public partial class Userdata : System.Web.UI.Page
    {
        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;

            }
            if (Page.IsPostBack==true) return;
            clsDB db = new clsDB();
            db.Get_Tabledata("Select * from [user] where Benutzernr="+ Session["UserID"].ToString(), false, true);
            string account_code = db.dsdaten.Tables[0].Rows[0]["Vereinnr"].ToString();
            if (db.dsdaten.Tables[0].Rows[0]["Nachname"].ToString() != "") this.txtname.Text = db.dsdaten.Tables[0].Rows[0]["Nachname"].ToString();
            if (db.dsdaten.Tables[0].Rows[0]["Vorname"].ToString() != "") this.txtvorname.Text = db.dsdaten.Tables[0].Rows[0]["Vorname"].ToString();
            if (db.dsdaten.Tables[0].Rows[0]["Email"].ToString() != "") this.txtemail.Text = db.dsdaten.Tables[0].Rows[0]["email"].ToString();

            db.dsdaten.Tables.Clear();
            db.Get_Tabledata("Select * from base_account where account_code='"+account_code+"'", false, true);
            this.lblverein.Text = db.dsdaten.Tables[0].Rows[0]["account_name"].ToString();

        }

      
        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            this.lblerror.Text = "";
            if (this.txtname.Text == "" || this.txtemail.Text == "")
            {
                this.lblerror.Text = "Bitte mindestens Name und E-Mail ausfüllen.";
                return;
            }
            clsDB db = new clsDB();
            db.Get_Tabeldata_for_Update("Select * from [user] where benutzernr=" + Session["UserID"].ToString(), false, true);
            db.dsdaten.Tables[0].Rows[0]["Nachname"] = this.txtname.Text;
            db.dsdaten.Tables[0].Rows[0]["Vorname"] = this.txtvorname.Text;
            db.dsdaten.Tables[0].Rows[0]["EMail"] = this.txtemail.Text;
            db.Update_Tabeldata();
            Helper.Log log = new Helper.Log();

            log.insert_entry(2, Convert.ToInt32(Session["UserID"]), "Userdaten gespeichert");
            Response.Redirect("~/default.aspx");

        }
    }
}