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.Drawing;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Text;

public partial class FundRequestSelf : System.Web.UI.Page
{
    string transactionid;
    string date = DateTime.Now.ToString();
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            lblAgentID.Text = Session["USERNAME"].ToString();
            txtPaymentDate.Attributes.Add("readonly", "readonly");
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtPaymentDate.Text).Date > Convert.ToDateTime(date))
            {
                lblMessage.Text = "Payment date must be less than current date";
                lblMessage.ForeColor = Color.Red;
            }
            else
            {
                requestforupload_cash();
            }
        }
        catch (Exception ex)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    private void requestforupload_cash()
    {
        try
        {
            Random rand = new Random();
            rand.Next(0, 1);
            for (int i = 0; i <= 1; i++)
            {
                transactionid = RandomString(rand, 12);
            }
            SqlParameter[] insert_request_details = new SqlParameter[]
            {
                new SqlParameter("@CURRENTLEVEL",Session["LOGINID"].ToString()),
                new SqlParameter("@Amount",Convert.ToDecimal( txtAmount.Text.Trim())),
                new SqlParameter("@Date",txtPaymentDate.Text.Trim()),
                new SqlParameter("@Depositedin",ddlBankName.SelectedItem.Text.Trim()),
                new SqlParameter("@City_Branch",txtBranchName.Text.Trim()),
                new SqlParameter("@STATUS","Pending"),
                new SqlParameter("@modeofpayment",ddlPaymentMode.SelectedItem.Text.Trim()),

                new SqlParameter("@DepositorName",Tet_DepositorName.Text.Trim()), 
                new SqlParameter("@ACCOUNTNUMBER", txt_Account_no.Text.Trim()),
                new SqlParameter ("@BankTransactionID", txt_Bant_tran_id.Text .Trim ()),

                new SqlParameter("@CHEQUEDDNUMBER",txt_Cheque_dd_no.Text.Trim()),
                new SqlParameter("@CHEQUEDDDATE",txt_Cheque_DD_Date.Text.Trim()),
                new SqlParameter("@BANKNAME",txt_bank.Text.Trim()),
                new SqlParameter("@BankBranch",txt_Yoyr_Bank_Branch.Text.Trim()),

                new SqlParameter ("@Remark",txt_Remark.Text.Trim ()),
                new SqlParameter ("@TransactionID",transactionid),
                new SqlParameter ("@IPAddress",IPAddress),
                new SqlParameter ("@ErrorMessage",SqlDbType.Int)
            };
            insert_request_details[17].Direction = ParameterDirection.Output;                         
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_INSERT_requestforupload_cash_TEST", insert_request_details);
            int result = Convert.ToInt32(insert_request_details[17].Value);
            if (result == 1)
            {
                lblMessage.Text = "There is some technical problem .Please try again";
                lblMessage.ForeColor = Color.Red;
            }
            else
            {
                lblMessage.Text = "You have successfully submitted Request for Fund. Your Ref.No. " + transactionid;
                lblMessage.ForeColor = Color.Blue;
                CommonFunction.ClearControlValues(tblFundRequestSelf);
            }            
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    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();
    }
}
