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 Admin_manageuserprofile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                divUserid.Visible = false;
                div_form.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void ddl_Usertype_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            divUserid.Visible = true;
            if (ddl_Usertype.SelectedItem.Text.Equals("White Label"))
            {
                BindDdlForMdWl();
                div_form.Visible = false;
            }
            else if (ddl_Usertype.SelectedItem.Text.Equals("Master Distributor"))
            {
                getMDS();
                div_form.Visible = false;
            }
            else if (ddl_Usertype.SelectedItem.Text.Equals("Distributor"))
            {
                BindDistributor();
                div_form.Visible = false;
            }
            else if (ddl_Usertype.SelectedItem.Text.Equals("Agent"))
            {
                BindAgent();
                div_form.Visible = false;
            }
            else if (ddl_Usertype.SelectedItem.Text.Equals("Select"))
            {
                divUserid.Visible = false;
                div_form.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    private void BindDdlForMdWl()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetWL", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_selectUserId.DataSource = ds;
                ddl_selectUserId.DataTextField = "username";
                ddl_selectUserId.DataValueField = "loginid";
                ddl_selectUserId.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    public void getMDS()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] 
            { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetMds", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_selectUserId.DataSource = ds;
                ddl_selectUserId.DataTextField = "username";
                ddl_selectUserId.DataValueField = "loginid";
                ddl_selectUserId.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    private void BindDistributor()
    {
        SqlParameter[] param = new SqlParameter[] { };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "uspGetDistributor", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddl_selectUserId.DataSource = ds;
            ddl_selectUserId.DataTextField = "username";
            ddl_selectUserId.DataValueField = "loginid";
            ddl_selectUserId.DataBind();
        }

    }

    private void BindAgent()
    {
        SqlParameter[] param = new SqlParameter[] { };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "uspGetAgent", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddl_selectUserId.DataSource = ds;
            ddl_selectUserId.DataTextField = "username";
            ddl_selectUserId.DataValueField = "loginid";
            ddl_selectUserId.DataBind();
        }

    }

    protected void ddl_selectUserId_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddl_selectUserId.SelectedItem.Text.Equals("select"))
            {
                div_form.Visible = false;
            }
            else
            {
                div_form.Visible = true;
                lbl_usertype.Text = ddl_Usertype.SelectedItem.Text;
                getuserdetails(ddl_selectUserId.SelectedValue.Trim().ToString());
                BindDropdown("select * from StateTable", ddl_State_OA, "StateID", "StateName", null, null);
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void ddl_State_OA_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BindDropdown("select * from DistrictName where StateID='" + ddl_State_OA.SelectedValue + "'", ddl_Distric_OA, "CityId", "CityName", null, null);
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    public void BindDropdown(string sql, DropDownList InputDropDown, string ColValToBind, string ColTextToBind, string DropdownSelItem, string DropdownSelValue)
    {
        string ReturnString;
        string strQuery;
        strQuery = sql;
        try
        {
            SqlParameter[] state = new SqlParameter[]
            {

            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.Text, strQuery, state);
            InputDropDown.DataSource = ds;
            InputDropDown.DataTextField = ColTextToBind;
            InputDropDown.DataValueField = ColValToBind;
            InputDropDown.Items.Clear();
            InputDropDown.Items.Insert(0, "Select");

            InputDropDown.DataBind();
            if (DropdownSelValue != "")
            {
                foreach (ListItem LstItem in InputDropDown.Items)
                {
                    if (DropdownSelItem == LstItem.Value)
                    {
                        LstItem.Selected = true;
                    }
                }
            }

        }
        catch (Exception objException)
        {
            throw (objException);
        }
    }

    private void getuserdetails(string loginid)
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",loginid)
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GETUSERDETAILSAdmin", get_user_details);
            if (dr.Read())
            {
                ViewState["username"] = Convert.ToString(dr["USERNAME"]);                
                txt_companyfirmname.Text = Convert.ToString(dr["CORPORATENAME"]);
                txt_Auth_Pers_Name.Text = Convert.ToString(dr["FIRSTNAME"]);
                txt_Lastname.Text = Convert.ToString(dr["LASTNAME"]);
                txt_EMail_ID.Text = Convert.ToString(dr["EMAILID"]);
                txt_Authorized_Mobile_No.Text = Convert.ToString(dr["MOBILENO"]);
                txt_Pan_Tin_Number.Text = Convert.ToString(dr["PANNUMBER"]);
                txt_Office_Address.Text = Convert.ToString(dr["OFFICEADDRESSLINE1"]);
                ddl_State_OA.SelectedItem.Text = Convert.ToString(dr["OFFICESTATE"]);
                ddl_Distric_OA.SelectedItem.Text = Convert.ToString(dr["OFFICECITY"]);
                txt_City_OA.Text = Convert.ToString(dr["RESIDCITY"]);                
                txt_Pin_Code_OA.Text = Convert.ToString(dr["OFFICEPIN"]);
                lbl_category.Text = Convert.ToString(dr["Category"]);
                ViewState["AutEmailId"] = Convert.ToString(dr["EMAILID"]);
                ViewState["MobileNo"] = Convert.ToString(dr["MOBILENO"]); 
		ddlBoard.SelectedValue= Convert.ToString(dr["Board"]);
                txtDeviceId.Text= Convert.ToString(dr["DeviceId"]);
                txtPineDeviceId.Text= Convert.ToString(dr["EzyTapId"]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txt_EMail_ID.Text == ViewState["AutEmailId"].ToString())
        {
            if (txt_Authorized_Mobile_No.Text == ViewState["MobileNo"].ToString())
            {
                registration();
            }
            else
            {
                int CheckStatusMobileNo = checkuseravailability(ViewState["username"].ToString(), txt_EMail_ID.Text, txt_Authorized_Mobile_No.Text, "MOBILENO");
                if (CheckStatusMobileNo == 0)
                {
                    registration();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('This Mobile number already exist');"), true);         
                }
            }
        }
        else if (txt_Authorized_Mobile_No.Text == ViewState["MobileNo"].ToString())
        {
            if (txt_EMail_ID.Text == ViewState["AutEmailId"].ToString())
            {
                registration();
            }
            else
            {
                int CheckStatusEmail = checkuseravailability(ViewState["username"].ToString(), txt_EMail_ID.Text, txt_Authorized_Mobile_No.Text, "EMAIL");
                if (CheckStatusEmail == 0)
                {
                    registration();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('This EmailId already exist');"), true);         
                }
            }
        }
        else
        {
            int CheckStatusEmail = checkuseravailability(ViewState["username"].ToString(), txt_EMail_ID.Text, txt_Authorized_Mobile_No.Text,"EMAIL");
            if (CheckStatusEmail == 0)
            {
                int CheckStatusMobileNo = checkuseravailability(ViewState["username"].ToString(), txt_EMail_ID.Text, txt_Authorized_Mobile_No.Text,"MOBILENO");
                if (CheckStatusMobileNo == 0)
                {
                    registration();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('This Mobile number already exist');"), true);                    
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('This EmailId  already exist');"), 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;
        }
    }

    private void registration()
    {
        try
        {
            SqlParameter[] udp_updateinformation = new SqlParameter[]
            {
                new SqlParameter("@loginid",ddl_selectUserId.SelectedValue.Trim()),
                new SqlParameter("@companyfirmname",txt_companyfirmname.Text),
                new SqlParameter("@OFFICEADDRESSLINE1",txt_Office_Address.Text.Trim()),
                new SqlParameter("@OFFICESTATE",ddl_State_OA.SelectedItem.Text.Trim()),
                new SqlParameter("@OFFICECITY",ddl_Distric_OA.SelectedItem.Text.Trim()),
                new SqlParameter("@OFFICEPIN",txt_Pin_Code_OA.Text),
                new SqlParameter("@FirstName",txt_Auth_Pers_Name.Text),
                new SqlParameter("@LastName",txt_Lastname.Text),
                new SqlParameter("@residCity",txt_City_OA.Text.Trim()),
                new SqlParameter("@PANNUMBER",txt_Pan_Tin_Number.Text.Trim()),
                new SqlParameter("@EMAILID",txt_EMail_ID.Text.Trim()),
                new SqlParameter("@MOBILENO",txt_Authorized_Mobile_No.Text),
                new SqlParameter("@CategoryName",ddl_Category.SelectedItem.Text.Trim()),
                new SqlParameter("@CategoryValue",ddl_Category.SelectedValue.Trim()),
                new SqlParameter("@Board",ddlBoard.SelectedValue),
                new SqlParameter("@DeviceId",txtDeviceId.Text) ,
                new SqlParameter("@EzyTapId",txtPineDeviceId.Text)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updateinformationAdmin", udp_updateinformation);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Your Details have been updated successfully');"), true);
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    
}
