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;
using System.IO;
using RKLib.ExportData;

public partial class registereddistributor : System.Web.UI.Page
{
    string id_statusparam;
    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() == "distributor")
                    {
                        ddlUserInfo.SelectedIndex = 3;
                    }
                }
                //NameValueCollection queryString = stringhelpers.DecryptQueryString(Request.QueryString.ToString());
                //if (queryString != null)
                //{
                //    if (queryString["var"].Equals("Distributor"))
                //    {
                //        ddlUserInfo.SelectedIndex = 3;
                //    }
                //}
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getdetails()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserType","DS"),
                    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
            {
                Label1.Text = "No Data Available";
                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);
            throw (ex);

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            deleteenquiry_registered_distributor();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void deleteenquiry_registered_distributor()
    {
        try
        {
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked == true)
                {
                    Label lbl_LOGINID = (Label)gvdr.FindControl("lbl_LOGINID");
                    SqlParameter[] param_del = new SqlParameter[]
                    {
                        new SqlParameter("@status",SqlDbType.Int,4),
                        new SqlParameter("@loginid",lbl_LOGINID.Text)
                    };
                    param_del[0].Direction = ParameterDirection.Output;
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_delete_enquiry_agent", param_del);
                    id_statusparam = Convert.ToString(param_del[0].Value);
                    if (id_statusparam == "1")
                    {
                        Label1.Text = "Record Deleted Successfully";
                        Label1.ForeColor = Color.Blue;
                    }
                    else if (id_statusparam == "2")
                    {
                    }
                    else
                    {
                        Label1.Text = "Record not deleted";
                        Label1.ForeColor = Color.Red;
                    }
                }
            }
            getdetails();
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
        {          
        };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getregistereddistributordetails", param);
            DataTable dtEmployee = ds.Tables["Table"].Copy();
            int[] iColumns = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
            RKLib.ExportData.Export objExport = new
            RKLib.ExportData.Export("Web");
            objExport.ExportDetails(dtEmployee,
            iColumns, Export.ExportFormat.CSV,
            "C:\\distributordetails.csv");
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    
    protected void btn_flag_Command(object sender, CommandEventArgs e)
    {
        try
        {
            Button btn = (Button)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label loginid;
            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);
            getdetails();
            Label1.Text = "Working Status Changed";
            Label1.ForeColor = Color.Blue;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    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","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);
        }
    }
    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();
                    Label1.Text = "Working Status Changed For SMS 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);
        }
    }
    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 GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            getdetails();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
}

