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 ViewUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                getdetails();
                if (Request.QueryString.AllKeys.Contains("var"))
                {
                    // The query string parameter "parameterName" exists.
                    string value = Request.QueryString["var"];
                    // Further processing with 'value'
                    if (value.ToLower() == "agent")
                    {
                        ddlUserInfo.SelectedIndex = 4;
                    }
                }
                //NameValueCollection queryString = stringhelpers.DecryptQueryString(Request.QueryString.ToString());
                //if (queryString != null)
                //{
                //    if (queryString["var"].Equals("Agent"))
                //    {
                //        ddlUserInfo.SelectedIndex = 4;
                //        trAdv.Visible = true;
                //    }
                //}
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    private void getdetails()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserType","AG"),
                new SqlParameter("@Date","")
            };            
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getregisteredMDSdetailsNew", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ViewState["Result"] = ds;
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "No Data Availabel";
                Label1.ForeColor = Color.Red;
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getdetails();
    }

    protected void btnForWeb_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","password"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    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 btnForSms_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","Sms"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    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 ddlUserInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlUserInfo.SelectedItem.Text.Equals("White Label"))
        {
            Response.Redirect("RegisteredWL.aspx?var=White Label");
        }
        else if (ddlUserInfo.SelectedItem.Text.Equals("Master Distributor"))
        {
            Response.Redirect("RegisteredMDS.aspx?var=Master Distributor");
        }
        else if (ddlUserInfo.SelectedItem.Text.Equals("Distributor"))
        {
            Response.Redirect("registereddistributor.aspx?var=Distributor");
        }
        else if (ddlUserInfo.SelectedItem.Text.Equals("Agent"))
        {
            Response.Redirect("ViewAgents.aspx?var=Agent") ;
        }
        else if (ddlUserInfo.SelectedItem.Text.Equals("Advance Search"))
        {
            Response.Redirect("AdvanceSearch.aspx?var=Advance Search");
        }
    }
    protected void btnForPanCard_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","PanCard"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Working Status Changed For PanCard Service');"), 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 btnForDMR_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","DMR"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    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 btnForFlight_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","Status"),
                        new SqlParameter("@loginid",lbl_loginid.Text)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHANGEUSERWORKINGSTATUS", paramChangeLoginStatus);
                    getdetails();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Login Status has been updated');"), 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 btnSearch_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)ViewState["Result"];
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataTable  data = (from a in ds.Tables[0].AsEnumerable()
                               where a.Field<string>("USERNAME").Contains(txtAdv.Text.ToString().Trim())
                                      select a).CopyToDataTable();
            if (data.Rows.Count > 0)
            {
                GridView1.DataSource = data;
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
    }
}
