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;

public partial class ManageUser : System.Web.UI.Page
{
    string status;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                c1.Visible = false;
                c2.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void BindDdlForMdWl()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetWlMd", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlMdWlFrm.DataSource = ds;
                ddlMdWlFrm.DataTextField = "username";
                ddlMdWlFrm.DataValueField = "loginid";
                ddlMdWlFrm.DataBind();

                ddlMdWlTo.DataSource = ds;
                ddlMdWlTo.DataTextField = "username";
                ddlMdWlTo.DataValueField = "loginid";
                ddlMdWlTo.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }

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

                ddlDistributorTo.DataSource = ds;
                ddlDistributorTo.DataTextField = "username";
                ddlDistributorTo.DataValueField = "loginid";
                ddlDistributorTo.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }

    private void BindGrid()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@Upperlevel",ddlMdWlFrm.SelectedValue)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_getDsAccToUpperLevel", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvManageDistributors.DataSource = ds;
                gvManageDistributors.DataBind();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Data not available');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    protected void ddlMdWlFrm_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BindGrid();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void gvManageDistributors_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlMdWlTo.SelectedItem.Text != "Select")
            {
                foreach (GridViewRow gvdr in this.gvManageDistributors.Rows)
                {
                    if (((CheckBox)gvdr.FindControl("chkbox")).Checked == true)
                    {
                        Label lowerlevel = (Label)gvdr.FindControl("lbl_username");
                        SqlParameter[] param_del = new SqlParameter[]
                        {
                            new SqlParameter("@status",SqlDbType.Int,4),
                            new SqlParameter("@upperlevel",ddlMdWlTo.SelectedItem.Text.Trim()),
                            new SqlParameter("@lowerlevel",lowerlevel.Text.Trim()),
                        };
                        param_del[0].Direction = ParameterDirection.Output;

                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_managepartnersNew", param_del);                        
                        status = Convert.ToString(param_del[0].Value);
                        if (status == "1")
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('User Mapped Successfully');"), true);                            
                        }
                        else if (status == "2")
                        { }
                        else
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Mapping Not Successfull');"), true);                            
                        }
                    }
                }
                BindGrid();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select White Latel to whome you want to map the selected Distributor');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    protected void ddlManageType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlManageType.SelectedItem.Text.Equals("Manage Distributor"))
        {
            c1.Visible = true;
            c2.Visible = false;
            BindDdlForMdWl();
        }
        else if (ddlManageType.SelectedItem.Text.Equals("Manage Agent"))
        {
            c2.Visible = true;
            c1.Visible = false;
            BindDdlForDistributorFrmTo();
        }
        else if (ddlManageType.SelectedItem.Text.Equals("Select"))
        {
            c2.Visible = false;
            c2.Visible = false;
        }
    }
    protected void ddlDistributorFrm_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            BindGvAgent();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void BindGvAgent()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@Upperlevel",ddlDistributorFrm.SelectedValue)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "uspGetAgentsAccDistributors", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvAgent.DataSource = ds;
                gvAgent.DataBind();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Data not available');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void btnSubmitAgent_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlDistributorTo.SelectedItem.Text != "Select")
            {
                foreach (GridViewRow gvdr in this.gvAgent.Rows)
                {
                    if (((CheckBox)gvdr.FindControl("chkbox")).Checked == true)
                    {
                        Label lowerlevel = (Label)gvdr.FindControl("lbl_username");
                        SqlParameter[] param_del = new SqlParameter[]
                        {
                            new SqlParameter("@status",SqlDbType.Int,4),
                            new SqlParameter("@upperlevel",ddlDistributorTo.SelectedItem.Text.Trim()),
                            new SqlParameter("@lowerlevel",lowerlevel.Text.Trim()),
                        };
                        param_del[0].Direction = ParameterDirection.Output;
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_managepartnersNew", param_del);
                   
                        status = Convert.ToString(param_del[0].Value);
                        if (status == "1")
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('User Mapped Successfully');"), true);
                            
                        }
                        else if (status == "2")
                        {

                        }
                        else
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Mapping Not Successfull');"), true);
                        
                        }
                    }
                }
                BindGvAgent();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select Distributor to whome you want to map the selected Agent');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem.Please try Again.');"), true);
            throw(ex);
        }
    }
}
