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.Text;

public partial class FundTransper : System.Web.UI.Page
{
    string  IPaddress = HttpContext.Current.Request.UserHostAddress.ToString();
    decimal balance;
    string transactionid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                getdetails_amountrequest();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getdetails_amountrequest()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
        {
            new SqlParameter("@Loginid",Session["LOGINID"].ToString() ),
        };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GET_AMOUNT_REQUEST_FROM_PARTNER_Warlock", param);
            if (ds.Tables[0].Rows.Count != 0)
            {
                GridViewFundTransper.DataSource = ds;
                GridViewFundTransper.DataBind();
                lblerror.Visible = false;
            }
            else
            {
                lblerror.Visible = true;
                lblerror.Text = "No Data Available";
                lblerror.ForeColor = Color.Red;
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        try
        {
            userdetails();
            bool flag = true;
            bool Remarks = false;
            bool checkamount = true;
            foreach( GridViewRow gridview in this.GridViewFundTransper.Rows)
            {
                if (((CheckBox)gridview.FindControl("chkbox")).Checked)
                {
                    flag = false;

                    TextBox txt_Remark = (TextBox)gridview.FindControl("txt_Remark");
                    if (txt_Remark.Text == "")
                    {                        
                        Remarks = true;
                    }
                    else
                    {
                        Label lblcurrentlevel;
                        Label lbl_journalid;
                        Label amount;

                        lblcurrentlevel = (Label)gridview.FindControl("lbl_currentlevel");
                        lbl_journalid = (Label)gridview.FindControl("lbl_journalid");
                        amount = (Label)gridview.FindControl("amount");


                        int journalcurrentlevel = Convert.ToInt32(lblcurrentlevel.Text);
                        int journal_id = Convert.ToInt32(lbl_journalid.Text);
                        decimal amountval = Convert.ToDecimal(amount.Text);
                        if (balance > amountval)
                        {
                            checkamount = false;
                            Random rand = new Random();
                            rand.Next(0, 1);
                            for (int i = 0; i <= 1; i++)
                            {
                                transactionid = RandomString(rand, 12);
                                //lbluniqueid.Text = abc;
                            }
                            SqlParameter[] approvedrequest = new SqlParameter[]
                            {
                                new SqlParameter("@DSCURRENTLEVEL",Session["LOGINID"].ToString()),
                                new SqlParameter("@REFNUMBER",transactionid),
                                new SqlParameter("@id",journalcurrentlevel),
                                new SqlParameter("@journalid",journal_id),
                                new SqlParameter("@totalcash",amountval),
                                new SqlParameter("@ipaddress",IPaddress),
                                new SqlParameter("@Remark",txt_Remark.Text.Trim())
                            };
                            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_approve_balancerequest_TEST", approvedrequest);
                        }
                        else
                        {
                            lblerror.Text = "You don’t have enough balance to do this transaction. Please take balance in your login.";
                            lblerror.ForeColor = Color.Red;
                        }
                    }
                } getdetails_amountrequest();
            }
            if (flag)
            {
                lblerror.Text = "Please select any checkbox";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible =true;
            }
            else if (Remarks)
            {
                lblerror.Text = "Please Enter Remark";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else if (checkamount)
            {
                lblerror.Text = "You don’t have enough balance to do this transaction. Please take balance in your login.";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else
            {
                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 userdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);
            if (dr.Read())
            {
                balance = Convert.ToDecimal(dr["balance"]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
   
    protected void btnDecline_Click1(object sender, EventArgs e)
    {
        try
        {
            bool flagdec = true;
            bool flagrmk = false;
            foreach (GridViewRow griddec in this.GridViewFundTransper.Rows)
            {
                if (((CheckBox)griddec.FindControl("chkbox")).Checked)
                {
                    flagdec = false;
                    TextBox txt_Decline = (TextBox)griddec.FindControl("txt_Remark");
                    if (txt_Decline.Text == "")
                    {
                        flagrmk = true;
                    }
                    else
                    {
                        Label lblcurrentlevel;
                        Label lbl_journalid;
                        Label amount;

                        lblcurrentlevel = (Label)griddec.FindControl("lbl_currentlevel");
                        lbl_journalid = (Label)griddec.FindControl("lbl_journalid");
                        amount = (Label)griddec.FindControl("amount");


                        int journalcurrentlevel = Convert.ToInt32(lblcurrentlevel.Text);
                        int journal_id = Convert.ToInt32(lbl_journalid.Text);
                        decimal amountval = Convert.ToDecimal(amount.Text);

                        SqlParameter[] approvedrequest = new SqlParameter[]
                        {
                            new SqlParameter("@id",journalcurrentlevel),
                            new SqlParameter("@journalid",journal_id),
                            new SqlParameter("@totalcash",amountval),
                            new SqlParameter("@ipaddress",IPaddress),
                            new SqlParameter("@Remark",txt_Decline.Text.Trim())
                        };
                        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_Decline_balancerequest", approvedrequest);
                    }
                } getdetails_amountrequest();
            }
            if (flagdec)
            {
                lblerror.Text = "Please select any checkbox";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else if (flagrmk)
            {
                lblerror.Text = "Please Enter Remark";
                lblerror.ForeColor = Color.Red;
                lblerror.Visible = true;
            }
            else
            {
                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);
        }
    }
    public string RandomString(Random r, int len)
    {
        string str = "1234567890";
        StringBuilder sb = new StringBuilder();

        while ((len--) > 0)
        sb.Append(str[(int)(r.NextDouble() * str.Length)]);

        return sb.ToString();
    }
}
