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;
using System.Security.Cryptography;
using System.Text;

public partial class Admin_fundrequest : System.Web.UI.Page
{
    CommonFunction objcommonfun = new CommonFunction();
    static string Loginid;

    string transactionid, BalanceAfterTransaction, MobileNo;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                getdetails_amountrequest();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getdetails_amountrequest()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {
                
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GET_AMOUNT_REQUEST_FROM_PARTNER_All", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GvRequestAmount.DataSource = ds;
                GvRequestAmount.DataBind();                
                lblerror.Visible = false;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No Data Available";
                lblerror.ForeColor = Color.Red;
                GvRequestAmount.Visible = false;
               
            }
        }
        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 btn_RequestAccept_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gdrvw in this.GvRequestAmount.Rows)
            {
                if (((CheckBox)gdrvw.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    Loginid = Convert.ToString(GvRequestAmount.DataKeys[gdrvw.RowIndex][0]);
                    transactionid = Convert.ToString(GvRequestAmount.DataKeys[gdrvw.RowIndex][1]);
                    string amountval = Convert.ToString(GvRequestAmount.DataKeys[gdrvw.RowIndex][2]);
                    SqlParameter[] approverequest = new SqlParameter[]
                    {
                      new SqlParameter("@CURRENTLEVEL",Loginid),
                      new SqlParameter("@journalid",transactionid),
                      new SqlParameter("@totalcash",amountval),
                    };
                    DataSet objdataset = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "[udp_approve_balancerequestNew]", approverequest);
                    GetMobileNo(Loginid);
                    BalanceAfterTransaction = objcommonfun.userdetails(Loginid);
                    string sms = "An amount of Rs. " + amountval + " credited into your account on " + Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("dd-MMM-yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss") + " against txn id " + transactionid + " Your Current Balance is " + BalanceAfterTransaction + "";
                    objcommonfun.SendSMSByAdmin(MobileNo, sms, transactionid, Loginid); 
                }                
            }
            if (flag)
            {
                lblerror.Text = "Please Select Any One CheckBox";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else
            {
                getdetails_amountrequest();
                lblerror.Text = "Request Approved Successfully";
                lblerror.ForeColor = Color.Blue;
                lblerror.Visible = true;
            }
        }
        catch (Exception ev)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ev);
        }
    }

    public void GetMobileNo(string Loginid)
    {
        try
        {
            SqlParameter[] loginid = new SqlParameter[]
            {
                new SqlParameter ("@Loginid",Loginid)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Udp_GetRegisteredMobileNo", loginid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                MobileNo = ds.Tables[0].Rows[0]["MobileNo"].ToString();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

    protected void btn_RequestDecline_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            bool textDecline = false;
            foreach (GridViewRow gdrvw in this.GvRequestAmount.Rows)
            {
                if (((CheckBox)gdrvw.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    TextBox txt_Decline = (TextBox)gdrvw.FindControl("txt_DeclineReasion");
                    if (txt_Decline.Text == "")
                    {
                        textDecline = true;
                    }
                    else
                    {
                        string CURRENTLEVEL = Convert.ToString(GvRequestAmount.DataKeys[gdrvw.RowIndex][0]);
                        string referenceno = Convert.ToString(GvRequestAmount.DataKeys[gdrvw.RowIndex][1]);
                        SqlParameter[] AcceptRequestPanCard = new SqlParameter[]
                        {
                            new SqlParameter("@RefrenceNo",referenceno),
                            new SqlParameter("@CURRENTLEVEL",CURRENTLEVEL),
                            new SqlParameter("@Decline",txt_Decline.Text),
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_Rejected_balance_request", AcceptRequestPanCard);

                    }
                } 
            }
            if (flag)
            {
                lblerror.Text = "Please Select Any One Checkbox";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else if (textDecline)
            {
                getdetails_amountrequest();
                lblerror.Text = "Please Enter The Reasion";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else
            {
                getdetails_amountrequest();
                lblerror.Text = "Request Decline Successfully";
                lblerror.ForeColor = Color.Blue;
                lblerror.Visible = true;
            }
        }
        catch (Exception ev)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ev);
        }
    }
}
