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;
using System.Drawing;

public partial class InactiveDistriburor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                getmy_network_details();
            }
            DivGvAgentReport.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getmy_network_details()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@loginid",Session["LOGINID"].ToString()),
                new SqlParameter("@ActiveINactive","InActive"),
                new SqlParameter ("@checked", "0"),
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setActiveInactiveAgent", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                gvActiveAgent.DataSource = ds;
                gvActiveAgent.DataBind();
            }
            else
            {
                
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void btnActive_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.gvActiveAgent.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string LoginID = Convert.ToString(gvActiveAgent.DataKeys[gvdr.RowIndex][0]);

                    SqlParameter[] param_setActiveInactive = new SqlParameter[]
                    {  
                        new SqlParameter("@ActiveINactive","InActive"),
                        new SqlParameter("@loginid",LoginID),
                        new SqlParameter ("@checked", "1"),                        
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setActiveInactiveDisrributor", param_setActiveInactive);                   
                }
            }
            if (flag)
            {
                lblerror.Text = "Please select any checkbox";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                lblerror.Text = "Distributor Active Successfully";
                lblerror.ForeColor = Color.Blue;
                getmy_network_details();
                // BindGrid();
            }          
        }
        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 btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddDistributor.aspx");
    }
    protected void btnNoOfTxn_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow rowitem = (GridViewRow)btn.Parent.Parent;
            Label DsCurrentLevel;
            DsCurrentLevel = (Label)rowitem.FindControl("lblDistributorCurrentLevel");
            ViewState["DsCurrentLevel"] = DsCurrentLevel.Text;
            DivGvAgentReport.Visible = true;
            DIV_gvIncomeReport.Visible = false;
            GvTransactionReport.Visible = true;
            GetDataAccOperators();
        }
        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)
            {
                GvTransactionReport.DataSource = ds;
                GvTransactionReport.DataBind();
                lblerror.Visible = false;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No transaction available";
                lblerror.ForeColor = Color.Red;
            }
        }
        catch (Exception ev)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    
    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";
            }

        }
    }
    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);
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
        
    }
}
