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 Admin_User_CuttOff : System.Web.UI.Page
{
    int cutoffBalance;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                BindDdlForDistributorAgent();
            }
            cutOff.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    public void UserCutOffDetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",ddl_user.SelectedValue.Trim())
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getcutoffbalance", get_user_details);
            if (dr.Read())
            {
                cutoffBalance = Convert.ToInt32(dr["CuttOffAmount"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void BindDdlForDistributorAgent()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetDistributorAgent", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_user.DataSource = ds;
                ddl_user.DataTextField = "username";
                ddl_user.DataValueField = "loginid";
                ddl_user.DataBind();                
            }
        }
        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
        {
            if (ddl_user.SelectedItem.Text != "Select")
            {
                SqlParameter[] param_del = new SqlParameter[]
                {
                    new SqlParameter("@LoginId",ddl_user.SelectedValue.Trim()),
                    new SqlParameter("@CuttOffAmount",Convert.ToDecimal(txtAmount.Text)),
                };
                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_cutt_off_amoun_for_user", param_del);

                lblsucess.Text = "Cut Off Amount Set Successfully";
                lblsucess.ForeColor = Color.Blue;
                ddl_user.SelectedIndex = 0;
                txtAmount.Text = "";
            }
            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 ddl_user_SelectedIndexChanged(object sender, EventArgs e)
    {
        UserCutOffDetails();
        cutOff.Visible = true;
        lbl_cutoffamount.Text = Convert.ToString(cutoffBalance);
    }
}
