using System;
using System.Collections;
using System.Collections.Specialized;
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 RegisteredWL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                getdetails();
                NameValueCollection queryString = stringhelpers.DecryptQueryString(Request.QueryString.ToString());
                if (queryString != null)
                {
                    if (queryString["var"].Equals("White Label"))
                    {
                        ddlUserInfo.SelectedIndex = 1;
                    }
                }
            }
            DivGvAgentReport.Visible = false;
            btnActiveDSForWeb.Visible = false;
            btnDeAcDSForSMS.Visible = false;
            btnActiveAgForWeb.Visible = false;
            btnDeAcAgForSMS.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnNoOfDS_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label DsCurrentLevel = (Label)rowitem.FindControl("lblDistributorCurrentLevel");
            ViewState["DsCurrentLevel"] = DsCurrentLevel.Text;
            DivGvAgentReport.Visible = true;
            WLDetail.Visible = false;
            btnActiveAgForWeb.Visible = false;
            btnDeAcAgForSMS.Visible = false;
            btnWLChangeStatus.Visible = false;
            GvTransactionReport.Visible = true;
            btnActiveDSForWeb.Visible = true;
            btnDeAcDSForSMS.Visible = true;
            GetDataAccOperators();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnNoOfAG_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label DsCurrentLevel = (Label)rowitem.FindControl("lblAgentCurrentLevel");
            ViewState["AgCurrentLevel"] = DsCurrentLevel.Text;
            DivGvAgentReport.Visible = true;
            WLDetail.Visible = false;
            btnActiveDSForWeb.Visible = false;
            btnDeAcDSForSMS.Visible = false;
            btnWLChangeStatus.Visible = false;
            btnActiveAgForWeb.Visible = true;
            btnDeAcAgForSMS.Visible = true;
            GvTransactionReport.Visible = true;
            GetDataAccOperatorsAG();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    private void GetDataAccOperators()
    {
        try
        {
            SqlParameter[] getdataaccessopertor = new SqlParameter[]
            {
                new SqlParameter("@DsCurrentLevel",ViewState["DsCurrentLevel"]),              
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLAGENT_DETAILOF_DISTRIBUTOR", getdataaccessopertor);
            if (ds.Tables[0].Rows.Count > 0)
            {
                btnActiveDSForWeb.Visible = true; 
                btnDeAcDSForSMS.Visible = true;
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();                
            }
            else
            {
                WLDetail.Visible = true;
                btnActiveAgForWeb.Visible = false;
                btnDeAcAgForSMS.Visible = false;
                btnWLChangeStatus.Visible = false;
                btnActiveDSForWeb.Visible = false;
                btnDeAcDSForSMS.Visible = false;
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data 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);
        }
    }

    private void GetDataAccOperatorsAG()
    {
        try
        {
            SqlParameter[] getdataaccessopertor = new SqlParameter[]
            {
                new SqlParameter("@DsCurrentLevel",ViewState["AgCurrentLevel"]),              
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLAGENT_DETAILOF_WL", getdataaccessopertor);
            if (ds.Tables[0].Rows.Count > 0)
            {
                btnActiveAgForWeb.Visible = true;
                btnDeAcAgForSMS.Visible = true;
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();
            }
            else
            {
                WLDetail.Visible = true;
                btnActiveAgForWeb.Visible = false;
                btnDeAcAgForSMS.Visible = false;
                btnWLChangeStatus.Visible = false;
                btnActiveDSForWeb.Visible = false;
                btnDeAcDSForSMS.Visible = false;
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data 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 btnActiveDSForWeb_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvdr in this.GvTransactionReport.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_loginid = (Label)gvdr.FindControl("lbl_loginid");

                    SqlParameter[] paramChangeLoginStatus = new SqlParameter[]
                    {
                        new SqlParameter("@Portal","Web"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    DivGvAgentReport.Visible = true;
                    GetDataAccOperators();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Working Status Changed For Web Application');"), 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 btnActiveAgForWeb_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvdr in this.GvTransactionReport.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_loginid = (Label)gvdr.FindControl("lbl_loginid");

                    SqlParameter[] paramChangeLoginStatus = new SqlParameter[]
                    {
                        new SqlParameter("@Portal","Web"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    DivGvAgentReport.Visible = true;
                    GetDataAccOperatorsAG();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Working Status Changed For Web Application');"), 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 btnDeAcDSForSMS_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvdr in this.GvTransactionReport.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_loginid = (Label)gvdr.FindControl("lbl_loginid");

                    SqlParameter[] paramChangeLoginStatus = new SqlParameter[]
                    {
                        new SqlParameter("@Portal","Sms"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    DivGvAgentReport.Visible = true;
                    GetDataAccOperators();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Working Status Changed For SMS Application');"), 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 btnDeAcAgForSMS_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvdr in this.GvTransactionReport.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_loginid = (Label)gvdr.FindControl("lbl_loginid");

                    SqlParameter[] paramChangeLoginStatus = new SqlParameter[]
                    {
                        new SqlParameter("@Portal","Sms"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    DivGvAgentReport.Visible = true;
                    GetDataAccOperatorsAG();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Working Status Changed For SMS Application');"), 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 btn_flag_Command(object sender, CommandEventArgs e)
    {
        try
        {
            Button btn = (Button)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label loginid = (Label)rowitem.FindControl("lbl_loginid");
            SqlParameter[] approverequest = new SqlParameter[]
        {
          new SqlParameter("@LOGINID",loginid.Text.Trim()),
        };
            DataSet objdataset = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", approverequest);
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void GvTransactionReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
        {
            Button btn = (Button)e.Row.FindControl("btn_flag");
            Label lblstatus = (Label)e.Row.FindControl("lbl_StatusSms");
            Label lbl_statusWeb = (Label)e.Row.FindControl("lblStatusWeb");
            string enabled = Convert.ToString(GvTransactionReport.DataKeys[e.Row.RowIndex][0]);
            string StatusSms = Convert.ToString(GvTransactionReport.DataKeys[e.Row.RowIndex][1]);
            if (enabled.Equals("1"))
            {
                lbl_statusWeb.Text = "Active";
            }
            else
            {
                lbl_statusWeb.Text = "Deactive";

            }
            if (StatusSms.Equals("0"))
            {
                lblstatus.Text = "Deactive";
            }
            else
            {
                lblstatus.Text = "Active";
            }
        }
    }

    private void getdetails()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserType","WL"),
                new SqlParameter("@Date","")
            };            
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getregisteredMDSdetailsNew", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data 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 ddlUserInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlUserInfo.SelectedItem.Text.Equals("White Label"))
            {
                Response.Redirect("RegisteredWL.aspx" + stringhelpers.EncryptQueryString("var=White Label") + "");
            }
            else if (ddlUserInfo.SelectedItem.Text.Equals("Master Distributor"))
            {
                Response.Redirect("RegisteredMDS.aspx" + stringhelpers.EncryptQueryString("var=Master Distributor") + "");
            }
            else if (ddlUserInfo.SelectedItem.Text.Equals("Distributor"))
            {
                Response.Redirect("registereddistributor.aspx" + stringhelpers.EncryptQueryString("var=Distributor") + "");
            }
            else if (ddlUserInfo.SelectedItem.Text.Equals("Agent"))
            {
                Response.Redirect("ViewAgents.aspx" + stringhelpers.EncryptQueryString("var=Agent") + "");
            }
            else if (ddlUserInfo.SelectedItem.Text.Equals("Advance Search"))
            {
                Response.Redirect("AdvanceSearch.aspx" + stringhelpers.EncryptQueryString("var=Advance Search"));
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnWLChangeStatus_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_loginid = (Label)gvdr.FindControl("lbl_loginid");

                    SqlParameter[] paramChangeLoginStatus = new SqlParameter[]
                    {
                        new SqlParameter("@Portal","Web"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    Label1.Text = "Working Status Changed For Web Application";
                    Label1.ForeColor = Color.Blue;
                }
            }
        }
        catch (Exception ex)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    
}
