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.Text;
using System.Net.Mail;
using System.Net;
using System.Drawing;
using System.IO;

public partial class Admin_WLCommission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                BindDdlForWl();
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = false;
                btnSubmit.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    
    private void BindDdlForWl()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetWl", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_selectUserId.DataSource = ds;
                ddl_selectUserId.DataTextField = "username";
                ddl_selectUserId.DataValueField = "loginid";
                ddl_selectUserId.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList3.SelectedItem.Text.Equals("Recharge"))
        {
            GVWlRechargeComm.Visible = true;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            BindGridRecharge();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Bill Payment"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = true;
            GVOtherServices.Visible = false;
            BindGridBillPay();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Other"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = true;
            BindGridOther();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Money Transfer"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = true;
            BindGridOther();
        }
         else if (DropDownList3.SelectedValue.Equals("ELECTRICITY"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = true;
            GVOtherServices.Visible = false;
            BindGridElectricityBillPay();
        }
        else
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
        }
    }
    private void BindGridElectricityBillPay()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
           new SqlParameter("@Services",DropDownList3.SelectedValue.ToString()),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            btnSubmit.Visible = true;
            GVWlBillPayComm.DataSource = ds;
            GVWlBillPayComm.DataBind();
        }
        else
        {
            btnSubmit.Visible = false;
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }
    private void BindGridRecharge()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            btnSubmit.Visible = true;
            GVWlRechargeComm.DataSource = ds;
            GVWlRechargeComm.DataBind();
        }
        else
        {
            btnSubmit.Visible = false;
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    protected void GVWlRechargeComm_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
            }
        }
        catch (Exception Ex)
        {
            //report error
        }
    }

    private void BindGridBillPay()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            btnSubmit.Visible = true;
            GVWlBillPayComm.DataSource = ds;
            GVWlBillPayComm.DataBind();
        }
        else
        {
            btnSubmit.Visible = false;
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    private void BindGridOther()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            btnSubmit.Visible = true;
            GVOtherServices.DataSource = ds;
            GVOtherServices.DataBind();
        }
        else
        {
            btnSubmit.Visible = false;
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (DropDownList3.SelectedItem.Text.Equals("Recharge"))
            {
                GVWlRechargeComm.Visible = true;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = false;
                UpdateRechargeComm();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Bill Payment"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = true;
                GVOtherServices.Visible = false;
                UpdateBillPayComm();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Other"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = true;
                UpdateOtherCommission();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Money Transfer"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = true;
                UpdateOtherCommission();
            }
            else if (DropDownList3.SelectedValue.Equals("ELECTRICITY"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = true;
                GVOtherServices.Visible = false;
                UpdateBillPayComm();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    public void UpdateRechargeComm()
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GVWlRechargeComm.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkSelect")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVWlRechargeComm.DataKeys[gvdr.RowIndex][0]);
                    TextBox txt_commission = (TextBox)gvdr.FindControl("txtCommission");
                    SqlParameter[] param_setcommission = new SqlParameter[]
                    {
                        new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
                        new SqlParameter("@OperatorID",operatorID),
                        new SqlParameter("@Comission",txt_commission.Text.Trim()),
                        new SqlParameter("@User","Whitelable")
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setWlComission", param_setcommission);
                }
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any Checkbox');"), true);               
            }
            else
            {
                BindGridRecharge();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);             
            }
        }
        catch (Exception ex)
        {}
    }

    public void UpdateBillPayComm()
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GVWlBillPayComm.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVWlBillPayComm.DataKeys[gvdr.RowIndex][0]);
                    TextBox txtWlCommInPercentag = (TextBox)gvdr.FindControl("txtWlCommInPercentag");
                    TextBox txtWlSerCharInPercentag = (TextBox)gvdr.FindControl("txtWlSerCharInPercentag");
                    TextBox txtWlSerChaInRS = (TextBox)gvdr.FindControl("txtWlSerChaInRS");
                    SqlParameter[] param_setcommission = new SqlParameter[]
                    {
                        new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
                        new SqlParameter("@OperatorID",operatorID),
                        new SqlParameter("@WlCommInPercentag",txtWlCommInPercentag.Text.Trim()),
                        new SqlParameter("@WlSerCharInPercentag",txtWlSerCharInPercentag.Text.Trim()),
                        new SqlParameter("@WlSerChaInRS",txtWlSerChaInRS.Text.Trim()),
                        new SqlParameter("@User","Whitelabel")
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setWlComissionForBill", param_setcommission);
                }
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any Checkbox');"), true);
            }
            else
            {
                BindGridOther();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);                
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please try again');"), true);
        }
    }

    public void UpdateOtherCommission()
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GVOtherServices.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVOtherServices.DataKeys[gvdr.RowIndex][0]);
                    TextBox txtWlCommInPercentag = (TextBox)gvdr.FindControl("txtWlCommInPercentag");
                    TextBox txtWlSerChaInRS = (TextBox)gvdr.FindControl("txtWlSerChaInRS");
                    SqlParameter[] param_setcommission = new SqlParameter[]
                    {
                        new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
                        new SqlParameter("@OperatorID",operatorID),
                        new SqlParameter("@WlCommInPercentag",txtWlCommInPercentag.Text.Trim()),
                        new SqlParameter("@WlSerCharInPercentag","0.00"),
                        new SqlParameter("@WlSerChaInRS",txtWlSerChaInRS.Text.Trim()),
                        new SqlParameter("@User","Whitelabel")
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setWlComissionForBill", param_setcommission);
                }
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any Checkbox');"), true);
            }
            else
            {
                BindGridBillPay();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);                
            }
        }
        catch (Exception ex)
        {

        }
    }

    protected void ddl_selectUserId_SelectedIndexChanged(object sender, EventArgs e)
    {
        GVWlRechargeComm.Visible = false;
        GVWlBillPayComm.Visible = false;
        GVOtherServices.Visible = false;
       // DropDownList3.SelectedValue = "0";
    }
}
