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

public partial class calogin_UserCommission : System.Web.UI.Page
{
    float UserCommInPercentag;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = false;
                GVDMR.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList3.SelectedItem.Text.Equals("Recharge"))
        {
            GVWlRechargeComm.Visible = true;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            GVTravel.Visible = false;
            GVDMR.Visible = false; 
            BindGridRecharge();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Bill Payment"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = true;
            GVOtherServices.Visible = false;
            GVTravel.Visible = false;
            GVDMR.Visible = false; 
            BindGridBillPay();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Money Transfer"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            GVTravel.Visible = false;
            GVDMR.Visible = true; 
            BindGridDMR();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Flight"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = false;
            GVDMR.Visible = false;
            GVTravel.Visible = true;
            BindGridTravel();
        }
        else if (DropDownList3.SelectedItem.Text.Equals("Other"))
        {
            GVWlRechargeComm.Visible = false;
            GVWlBillPayComm.Visible = false;
            GVOtherServices.Visible = true;
            GVTravel.Visible = false;
            GVDMR.Visible = false; 
            BindGridOther();
        }
    }
    private void BindGridRecharge()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",Session["LOGINID"].ToString()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GVWlRechargeComm.DataSource = ds;
            GVWlRechargeComm.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    private void BindGridBillPay()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@userid",Session["LOGINID"].ToString()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        //DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetWLCommissionDetails", param);
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_whitelabeledusercommiondetail", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GVWlBillPayComm.DataSource = ds;
            GVWlBillPayComm.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    protected void GVWlBillPayComm_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
        {
            string MarginInPersantage = Convert.ToString(GVWlBillPayComm.DataKeys[e.Row.RowIndex][1]);
            string ChargeINPersantage = Convert.ToString(GVWlBillPayComm.DataKeys[e.Row.RowIndex][2]);
            string ChargeINRs = Convert.ToString(GVWlBillPayComm.DataKeys[e.Row.RowIndex][3]);
            if (Convert.ToDecimal(MarginInPersantage) > 0)
            {
                (e.Row.FindControl("txtUserSerCharInPercentag") as TextBox).Enabled = false;
                (e.Row.FindControl("txtUserSerChaInRS") as TextBox).Enabled = false;
            }
            else if (Convert.ToDecimal(ChargeINPersantage) > 0)
            {
                (e.Row.FindControl("txtUserCommInPercentag") as TextBox).Enabled = false;
                (e.Row.FindControl("txtUserSerChaInRS") as TextBox).Enabled = false;
            }
            else if (Convert.ToDecimal(ChargeINRs) > 0)
            {
                (e.Row.FindControl("txtUserCommInPercentag") as TextBox).Enabled = false;
                (e.Row.FindControl("txtUserSerCharInPercentag") as TextBox).Enabled = false;
            }
            else
            {
                (e.Row.FindControl("txtUserCommInPercentag") as TextBox).Enabled = false;
                (e.Row.FindControl("txtUserSerCharInPercentag") as TextBox).Enabled = false;
                (e.Row.FindControl("txtUserSerChaInRS") as TextBox).Enabled = false;
            }
        }
    }

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

    private void BindGridDMR()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",Session["LOGINID"].ToString()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_whitelabeledusercommiondetail_for_DMR", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GVDMR.DataSource = ds;
            GVDMR.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    private void BindGridTravel()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",Session["LOGINID"].ToString()),
           new SqlParameter("@Services",DropDownList3.SelectedItem.Text),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_whitelabeledusercommiondetail_for_DMR", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GVTravel.DataSource = ds;
            GVTravel.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Data Availabel');"), true);
        }
    }

    protected void GVOtherServices_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
        {
            string enabled = Convert.ToString(GVOtherServices.DataKeys[e.Row.RowIndex][0]);
            if (Convert.ToDecimal(enabled) > 0)
            {
                (e.Row.FindControl("txtUserSerCharInRs") as TextBox).Enabled = false;
            }
            else
            {
                (e.Row.FindControl("txtUserCommInPercentag") as TextBox).Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (DropDownList3.SelectedItem.Text.Equals("Recharge"))
            {
                GVWlRechargeComm.Visible = true;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = false;
                GVTravel.Visible = false;
                GVDMR.Visible = false;
                UpdateRechargeComm();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Bill Payment"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = true;
                GVOtherServices.Visible = false;
                GVTravel.Visible = false;
                GVDMR.Visible = false;
                UpdateBillPayComm();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Other"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = true;
                GVTravel.Visible = false;
                GVDMR.Visible = false;
                UpdateOtherCommission();
            }

            else if (DropDownList3.SelectedItem.Text.Equals("Money Transfer"))
            {
                GVWlRechargeComm.Visible = false;
                GVWlBillPayComm.Visible = false;
                GVOtherServices.Visible = false;
                GVTravel.Visible = false;
                GVDMR.Visible = true;
                UpdateDMRCommission();
            }
            else if (DropDownList3.SelectedItem.Text.Equals("Flight"))
            {
              
                    GVWlRechargeComm.Visible = false;
                    GVWlBillPayComm.Visible = false;
                    GVOtherServices.Visible = false;
                    GVDMR.Visible = false;
                    GVTravel.Visible = true;
                    UpdateTravelCommission();
               
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    public void UpdateRechargeComm()
    {
        try
        {
            bool flag = true;
            bool flag1 = true;
            foreach (GridViewRow gvdr in this.GVWlRechargeComm.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVWlRechargeComm.DataKeys[gvdr.RowIndex][0]);
                    Label Wlcommission = (Label)gvdr.FindControl("lblWlCommission");
                    TextBox UserCommission = (TextBox)gvdr.FindControl("UserCommission");
                    TextBox DSCommission = (TextBox)gvdr.FindControl("DsCommission");
                    if (Convert.ToDouble(DSCommission.Text) < 0)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Only Positive integer allowed');"), true);
                    }
                    else
                    {
                        if (Convert.ToDouble(Wlcommission.Text) >= Convert.ToDouble(Convert.ToDouble(UserCommission.Text) + Convert.ToDouble(DSCommission.Text)))
                        {
                            flag1 = false;
                            SqlParameter[] param_setcommission = new SqlParameter[]
                            {
                                new SqlParameter("@Userid",Session["LOGINID"].ToString()),
                                new SqlParameter("@OperatorID",operatorID),
                                new SqlParameter("@Comission",UserCommission.Text.Trim()),
                                new SqlParameter("@DSComission",DSCommission.Text.Trim()),
                                new SqlParameter("@User","WLUser")
                            };
                            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 if (flag1)
            {
                BindGridRecharge();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Enter less then or equal value');"), 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]);
                    Label WlCommPer = (Label)gvdr.FindControl("WlCommInPercentag");
                    Label WlSerCharInPer = (Label)gvdr.FindControl("WlSerCharInPercentag");
                    Label WlSerChaInRS = (Label)gvdr.FindControl("WlSerChaInRS");
                    TextBox userCommInPer = (TextBox)gvdr.FindControl("txtUserCommInPercentag");
                    TextBox userSerCharInPer = (TextBox)gvdr.FindControl("txtUserSerCharInPercentag");
                    TextBox UserSerChaInRS = (TextBox)gvdr.FindControl("txtUserSerChaInRS");
                    if (Convert.ToDecimal(WlCommPer.Text) < Convert.ToDecimal(userCommInPer.Text))
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please entere less then or equal value');"), true);
                    }
                    else if (Convert.ToDecimal(WlSerCharInPer.Text) <= Convert.ToDecimal(userSerCharInPer.Text) && Convert.ToDecimal(WlSerChaInRS.Text) <= Convert.ToDecimal(UserSerChaInRS.Text))
                    {
                        SqlParameter[] param_setcommission = new SqlParameter[]
                        {
                            new SqlParameter("@Userid",Session["LOGINID"].ToString()),
                            new SqlParameter("@OperatorID",operatorID),
                            new SqlParameter("@WlCommInPercentag",userCommInPer.Text.Trim()),
                            new SqlParameter("@WlSerCharInPercentag",WlSerCharInPer.Text.Trim()),
                            new SqlParameter("@WlSerChaInRS",UserSerChaInRS.Text.Trim()),
                            new SqlParameter("@User","WLUser")
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setWlComissionForBill", param_setcommission);
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please entere Greater then or equal value');"), true);
                    }
                }
            }
            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)
        {

        }
    }

    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][2]);
                    Label WlCommPer = (Label)gvdr.FindControl("WlCommInPercentag");
                    Label WlSerChaInRS = (Label)gvdr.FindControl("WlSerChaInRS");
                    TextBox UserCommInPer = (TextBox)gvdr.FindControl("txtUserCommInPercentag");
                    TextBox UserSerCharInRs = (TextBox)gvdr.FindControl("txtUserSerCharInRs");
                    if (Convert.ToDecimal(WlCommPer.Text) >= Convert.ToDecimal(UserCommInPer.Text) && Convert.ToDecimal(WlSerChaInRS.Text) >= Convert.ToDecimal(UserSerCharInRs.Text))
                    {
                        SqlParameter[] param_setcommission = new SqlParameter[]
                        {
                            new SqlParameter("@Userid",Session["LOGINID"].ToString()),
                            new SqlParameter("@OperatorID",operatorID),
                            new SqlParameter("@WlCommInPercentag",UserCommInPer.Text.Trim()),
                            new SqlParameter("@WlSerCharInPercentag","0.00"),
                            new SqlParameter("@WlSerChaInRS",UserSerCharInRs.Text.Trim()),
                            new SqlParameter("@User","WLUser")
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_setWlComissionForBill", param_setcommission);
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please entere less then or equal value');"), true);
                    }
                }
            }
            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)
        {

        }
    }

    public void UpdateDMRCommission()
    {
        try
        {
            bool flag = true;
            bool flag1 = true;
            foreach (GridViewRow gvdr in this.GVDMR.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVDMR.DataKeys[gvdr.RowIndex][0]);
                    Label Wlcommission = (Label)gvdr.FindControl("lblWlCommission");
                    TextBox UserCommission = (TextBox)gvdr.FindControl("UserCommission");
                    TextBox DSCommission = (TextBox)gvdr.FindControl("DsCommission");
                   // if (Convert.ToDouble(DSCommission.Text) < 0)
                    if(false)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Only Positive integer allowed');"), true);
                    }
                    else
                    {
                        if (Convert.ToDouble(Wlcommission.Text) >= Convert.ToDouble(Convert.ToDouble(UserCommission.Text) + Convert.ToDouble(DSCommission.Text)))
                        {
                            flag1 = false;
                            SqlParameter[] param_setcommission = new SqlParameter[]
                            {
                                new SqlParameter("@Userid",Session["LOGINID"].ToString()),
                                new SqlParameter("@OperatorID",operatorID),
                                new SqlParameter("@Comission",UserCommission.Text.Trim()),
                                new SqlParameter("@DSComission",DSCommission.Text.Trim()),
                                new SqlParameter("@User","WLUser")
                            };
                            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 if (flag1)
            {
                BindGridDMR();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Enter less then or equal value');"), true);
            }
            else
            {
                BindGridDMR();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);
            }
        }
        catch (Exception ex)
        { }
    }


    public void UpdateTravelCommission()
    {
        try
        {
            bool flag = true;
            bool flag1 = true;
            foreach (GridViewRow gvdr in this.GVTravel.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string operatorID = Convert.ToString(GVTravel.DataKeys[gvdr.RowIndex][0]);
                    Label Wlcommission = (Label)gvdr.FindControl("lblWlCommission");
                    TextBox UserCommission = (TextBox)gvdr.FindControl("UserCommission");
                    TextBox DSCommission = (TextBox)gvdr.FindControl("DsCommission");
                    // if (Convert.ToDouble(DSCommission.Text) < 0)
                    if (false)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Only Positive integer allowed');"), true);
                    }
                    else
                    {
                        if (Convert.ToDouble(Wlcommission.Text) >= Convert.ToDouble(Convert.ToDouble(UserCommission.Text) + Convert.ToDouble(DSCommission.Text)))
                        {
                            flag1 = false;
                            SqlParameter[] param_setcommission = new SqlParameter[]
                            {
                                new SqlParameter("@Userid",Session["LOGINID"].ToString()),
                                new SqlParameter("@OperatorID",operatorID),
                                new SqlParameter("@Comission",UserCommission.Text.Trim()),
                                new SqlParameter("@DSComission",DSCommission.Text.Trim()),
                                new SqlParameter("@User","WLUser")
                            };
                            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 if (flag1)
            {
                BindGridTravel();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Enter less then or equal value');"), true);
            }
            else
            {
                BindGridTravel();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);
            }
        }
        catch (Exception ex)
        { }
    }
}
