using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;


public partial class UserProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            txtEMailID.Visible = false;
            txt_Aut_Mob_No.Visible = false;
            btnSave.Visible = false;
            if (!IsPostBack)
            {
                divProfile.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Distributor"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Distributor"), 
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributorAgentOfMDS", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                }
            }
            else
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Agent"),
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributorAgentOfMDS", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                }
            }
        }
        catch(Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            divProfile.Visible = true;
            getuserdetails();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
    private void getuserdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",ddl_partner_id.SelectedValue.ToString())
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GETUSERDETAILS_TEST", get_user_details);
            if (dr.Read())
            {                
                ViewState["username"] = Convert.ToString(dr["USERNAME"]);
                lbl_userid.Text = Convert.ToString(dr["USERNAME"]);
                lblCorporateName.Text = Convert.ToString(dr["CORPORATENAME"]);
                lbl_FirstName.Text = Convert.ToString(dr["firstname"]);
                lbl_LastName.Text = Convert.ToString(dr["LASTNAME"]);
                lbl_Address.Text = Convert.ToString(dr["OFFICEADDRESSLINE2"]);
                lbl_State.Text = Convert.ToString(dr["OFFICESTATE"]);
                lbl_District.Text = Convert.ToString(dr["OFFICECITY"]);

                lbl_PanNumber.Text = Convert.ToString(dr["PANNUMBER"]);
                txtEMailID.Text = Convert.ToString(dr["EMAILID"]);
                lbl_EMailID.Text = Convert.ToString(dr["EMAILID"]);
                txt_Aut_Mob_No.Text = Convert.ToString(dr["MOBILENO"]);
                lbl_Aut_Mob_No.Text = Convert.ToString(dr["MOBILENO"]);
                lblPassword.Text=Convert.ToString(dr["PASSWORD"]);
                ViewState["MOBILENO"] = Convert.ToString(dr["MOBILENO"]);
                ViewState["AutEmailId"] = Convert.ToString(dr["EMAILID"]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        txtEMailID.Visible = true;
        txt_Aut_Mob_No.Visible = true;
        lbl_Aut_Mob_No.Visible = false;
        lbl_EMailID.Visible = false;
        btnSave.Visible = true;
        btnEdit.Visible = false;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtEMailID.Text == ViewState["AutEmailId"].ToString())
            {
                if (txt_Aut_Mob_No.Text != ViewState["MOBILENO"].ToString())
                {
                    int CheckStatusMobileNo = checkuseravailability(ViewState["username"].ToString(), txtEMailID.Text, txt_Aut_Mob_No.Text, "MOBILENO");
                    if (CheckStatusMobileNo == 0)
                    {
                        SqlParameter[] udp_updateinformation = new SqlParameter[]
                        {
                            new SqlParameter("@UserName",ViewState["username"].ToString()),
                            new SqlParameter ("@EmailId",ViewState["AutEmailId"].ToString()),
                            new SqlParameter ("@MobileNo", txt_Aut_Mob_No.Text),                
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updateinformation", udp_updateinformation);
                        lblsucess.Text = "Your Mobile Number have been updated successfully";
                        lblsucess.ForeColor = Color.Blue;
                        lbl_Aut_Mob_No.Text = txt_Aut_Mob_No.Text;
                    }
                    else
                    {
                        lblsucess.Text = "This Mobile Number is Already available";
                        lblsucess.ForeColor = Color.Red;
                    }

                }
            }
            else if (txt_Aut_Mob_No.Text == ViewState["MOBILENO"].ToString())
            {
                int CheckStatusEmail = checkuseravailability(ViewState["username"].ToString(), txtEMailID.Text, txt_Aut_Mob_No.Text, "EMAIL");
                if (CheckStatusEmail == 0)
                {
                    SqlParameter[] udp_updateinformation = new SqlParameter[]
                    {
                        new SqlParameter("@UserName",ViewState["username"].ToString()),
                        new SqlParameter ("@EmailId",txtEMailID.Text),
                        new SqlParameter ("@MobileNo",ViewState["MOBILENO"].ToString()),                
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updateinformation", udp_updateinformation);
                    lblsucess.Text = "Your Email Id have been updated successfully";
                    lblsucess.ForeColor = Color.Blue;
                    lbl_EMailID.Text = txtEMailID.Text;
                }
                else
                {
                    lblsucess.Text = "This Email Id is Already available";//NOT AVAILABIL(Duplicate)
                    lblsucess.ForeColor = Color.Red;
                }
            }
            else
            {
                int CheckStatusEmail = checkuseravailability(ViewState["username"].ToString(), txtEMailID.Text, txt_Aut_Mob_No.Text, "EMAIL");
                if (CheckStatusEmail == 0)
                {
                    int CheckStatusMobileNo = checkuseravailability(ViewState["username"].ToString(), txtEMailID.Text, txt_Aut_Mob_No.Text, "MOBILENO");
                    if (CheckStatusMobileNo == 0)
                    {
                        SqlParameter[] udp_updateinformation = new SqlParameter[]
                        {
                            new SqlParameter("@UserName",ViewState["username"].ToString()),
                            new SqlParameter ("@EmailId",txtEMailID.Text),
                            new SqlParameter ("@MobileNo", txt_Aut_Mob_No.Text),                
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updateinformation", udp_updateinformation);
                        lblsucess.Text = "Your Details have been updated successfully";
                        lblsucess.ForeColor = Color.Blue;
                    }
                    else
                    {
                        lblsucess.Text = "This Mobile Number is Already available";
                        lblsucess.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblsucess.Text = "This Email Id is Already available";//NOT AVAILABIL(Duplicate)
                    lblsucess.ForeColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
        btnSave.Visible = false;
        lbl_Aut_Mob_No.Visible = true;
        lbl_EMailID.Visible = true;
        btnEdit.Visible = true;
    }
    private int checkuseravailability(string username, string emailid, string mobileno, string uniquekey)
    {
        try
        {
            SqlParameter[] param_checkavailibility = new SqlParameter[]
            {  
                new SqlParameter("@Username",username),   
                new SqlParameter("@Email",emailid),
                new SqlParameter("@MobileNo",mobileno),
                new SqlParameter("@Unique_Key_Name",uniquekey),
                new SqlParameter("@ReturnMessage",SqlDbType.Int),
            };
            param_checkavailibility[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHECK_USERNAME_ETC_AVAILABILITY", param_checkavailibility);
            int Resultregstatus = Convert.ToInt32(param_checkavailibility[4].Value);           
            if (Resultregstatus == 0)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        catch (Exception EX)
        {
            return 2;
        }
    }
}
