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.Net;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;

public partial class Editt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                BindDropdown("select * from StateTable", ddlState, "StateID", "StateName", null, null);
                getuserdetails();
            }
        }
        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[] deductmoney = new SqlParameter[]
            {                
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.Text, strQuery, deductmoney);
            InputDropDown.DataSource = ds;
            InputDropDown.DataTextField = ColTextToBind;
            InputDropDown.DataValueField = ColValToBind;
            InputDropDown.DataBind();
            if (DropdownSelValue != "")
            {
                foreach (ListItem LstItem in InputDropDown.Items)
                {
                    if (DropdownSelItem == LstItem.Value)
                    {
                        LstItem.Selected = true;
                    }
                }
            }

        }
        catch (Exception objException)
        {
            //ReturnString = "Error " + objException;
            throw(objException);
        }
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDropdown("select * from DistrictName where StateID='" + ddlState.SelectedValue + "'", ddlDistric, "CityId", "CityName", null, null);
    }
    private void getuserdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GETUSERDETAILS_TEST", get_user_details);
            if (dr.Read())
            {
                //txt_Corporate_Name.Text = Convert.ToString(dr["CORPORATENAME"]);
                //txt_FirstName.Text = Convert.ToString(dr["firstname"]);
                //txt_LastName.Text = Convert.ToString(dr["LASTNAME"]);
                //txt_Gender.Text = Convert.ToString(dr["GENDER"]);
                //txt_MaritalStattus.Text = Convert.ToString(dr["MARITALSTATUS"]);
                //txt_Occupation.Text = Convert.ToString(dr["OCCUPATION"]);
                //txt_Country.Text = Convert.ToString(dr["OFFICECOUNTRY"]);
                ViewState["username"] = Convert.ToString(dr["USERNAME"]);
                lblCorporateName.Text = Convert.ToString(dr["CORPORATENAME"]);
                lbl_FirstName.Text = Convert.ToString(dr["firstname"]);
                lbl_LastName.Text = Convert.ToString(dr["LASTNAME"]);
                lblGender.Text = Convert.ToString(dr["GENDER"]);
                lblMaritalStattus.Text = Convert.ToString(dr["MARITALSTATUS"]);
                lblOccupation.Text = Convert.ToString(dr["OCCUPATION"]);
                lblCountry.Text = Convert.ToString(dr["OFFICECOUNTRY"]);

                txtAddress1.Text = Convert.ToString(dr["OFFICEADDRESSLINE1"]);
                txtAddress2.Text = Convert.ToString(dr["OFFICEADDRESSLINE2"]);
                ddlState.SelectedItem.Text = Convert.ToString(dr["OFFICESTATE"]);
                ddlDistric.SelectedItem.Text = Convert.ToString(dr["OFFICECITY"]);
                txtCity.Text = Convert.ToString(dr["RESIDCITY"]);
                txtPhonNumber.Text = Convert.ToString(dr["OFFICEPHONE"]);
                txtMobileNumber.Text = Convert.ToString(dr["OFFICEMOBILE"]);
                txtFaxNumber.Text = Convert.ToString(dr["OFFICEFAX"]);

                txtPanNumber.Text = Convert.ToString(dr["PANNUMBER"]);
                txtEMailID.Text = Convert.ToString(dr["EMAILID"]);
                txtAlternateEmailID.Text = Convert.ToString(dr["EMAIL"]);
                lblAutMobNum.Text = Convert.ToString(dr["MOBILENO"]);
                ViewState["AutEmailId"] = Convert.ToString(dr["EMAILID"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtEMailID.Text == ViewState["AutEmailId"].ToString())
            {
                registration();
            }
            else
            {
                SqlParameter[] CheckEmailID = new SqlParameter[]
            {
                new SqlParameter("@AutEmailID",txtEMailID.Text.Trim()),
                new SqlParameter("@Mobilenumber","0"),
                new SqlParameter ("@Status","User"),
                new SqlParameter("@ResultOutput",SqlDbType.Int), 
            };
                CheckEmailID[3].Direction = ParameterDirection.Output;
                SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CheckEmailID_MobileNumber", CheckEmailID);
                int Count = Convert.ToInt32(CheckEmailID[3].Value);
                if (Count == 0)
                {
                    registration();
                }
                else
                {
                    lblsucess.Text = "This E-Mail ID  Already Exist";
                    lblsucess.ForeColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
        
    }
    private void registration()
    {
        SqlParameter[] udp_updateinformation = new SqlParameter[]
        {
            //new SqlParameter ("@CORPORATENAME",txt_Corporate_Name.Text.Trim()),
            //new SqlParameter ("@FIRSTNAME",txt_FirstName.Text.Trim()),
            //new SqlParameter ("@LASTNAME", txt_LastName.Text.Trim()),
            //new SqlParameter ("@GENDER",txt_Gender.Text.Trim()),
            //new SqlParameter ("@MARITALSTATUS",txt_MaritalStattus.Text.Trim()),
            //new SqlParameter ("@OCCUPATION",txt_Occupation.Text.Trim()),
            //new SqlParameter ("@OFFICECOUNTRY",txt_Country.Text.Trim()),

            new SqlParameter("@loginid",Session["LOGINID"].ToString()),
            new SqlParameter("@OFFICEADDRESSLINE1",txtAddress1.Text.Trim()),
            new SqlParameter("@OFFICEADDRESSLINE2",txtAddress2.Text.Trim()),
            new SqlParameter("@OFFICESTATE",ddlState.SelectedItem.Text.Trim()),
            new SqlParameter("@OFFICECITY",ddlDistric.SelectedItem.Text.Trim()),
            new SqlParameter("@residCity",txtCity.Text.Trim()),
            new SqlParameter("@OFFICEPHONE",txtPhonNumber.Text.Trim()),
            new SqlParameter("@OFFICEMOBILE",txtMobileNumber.Text.Trim()),
            new SqlParameter("@OFFICEFAX",txtFaxNumber.Text.Trim()),

            new SqlParameter("@PANNUMBER",txtPanNumber.Text.Trim()),
            new SqlParameter("@EMAILID",txtEMailID.Text.Trim()),
            new SqlParameter("@ALTEREMAILID",txtAlternateEmailID.Text.Trim()),
            //new SqlParameter("@MOBILENO",lblAutMobNum.Text),
        };
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updateinformation_TEST", udp_updateinformation);
        lblsucess.Text = "Your Details have been updated successfully";
        lblsucess.ForeColor = Color.Blue;
    }
    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;
            //param_checkavailibility[5].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHECK_USERNAME_ETC_AVAILABILITY", param_checkavailibility);
            int Resultregstatus = Convert.ToInt32(param_checkavailibility[4].Value);
            //int errorresult = Convert.ToInt32(param_checkavailibility[5].Value);            
            if (Resultregstatus == 0)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        catch (Exception EX)
        {
            return 2;
        }
    }
}

