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

public partial class PanCard : System.Web.UI.Page
{
    string refrenceno;
    decimal balance;
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            trPancardfrm1.Visible = false;
            trPancardfrm2.Visible = false;
            divControl.Visible = true;
            if (Session["LOGINID"] == null && Session["USERNAME"] == null)
            {
                Response.Redirect("../login.aspx");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    private void DeductAmountPanCardFees()
    {
        Random rand = new Random();
        rand.Next(0, 1);
        for (int i = 0; i <= 1; i++)
        {
            refrenceno = RandomString(rand, 15);
            //lbluniqueid.Text = abc;
        }
        SqlParameter[] pancardservice = new SqlParameter[] 
        { 
            new SqlParameter("@AgentID",Session["LOGINID"].ToString()),
            new SqlParameter("@name",txtName.Text.Trim()),
            new SqlParameter("@mobile",txtMobNo.Text.Trim()),
            new SqlParameter("@IPAddress",IPAddress),
            new SqlParameter("@ReferenceNumber",refrenceno),
            new SqlParameter("@servicetype","Pan Card"),
            new SqlParameter("@circle","All Circle"),
            new SqlParameter("@operator","Pan Card"),
            new SqlParameter("@accountno",""),
            new SqlParameter("@ErrorMessage",SqlDbType.Int)
        };
        pancardservice[9].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_offlineservice_Pan_Card", pancardservice);
        int result = Convert.ToInt32(pancardservice[9].Value);
        if (result == 1)
        {
            lblMsg.Text = "There is some technical problem .Please try again";
            lblMsg.ForeColor = Color.Red;
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('You can Download the form by Clicking the Buttons');"), true);
            
            divControl.Visible = false;

            trPancardfrm1.Visible = true;
            trPancardfrm2.Visible = 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();
    }
    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"]);
            }
            //ViewState["leftbalance"]=Label5.Text;
        }
        catch (Exception ex)
        {
            Server.Transfer("Error.aspx?id=" + ex + "");
            //string st = ex.Message;
        }
    }
    protected void btnPancard_Click1(object sender, EventArgs e)
    {
        try
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Rs. 110.00 will be debit from your account as Pan Crad Fee. Are you sure want to continue!');"), true);
            userdetails();

            if (balance < 150)
            {
                lblMsg.Text = "You don’t have enough balance to do this transaction. Please take balance in your login.";
                lblMsg.ForeColor = Color.Red;
            }
            else
            {
                DeductAmountPanCardFees();

            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    //protected void btnyes_Click(object sender, EventArgs e)
    //{
    //    DeductAmountPanCardFees();
    //    divControl.Visible = false;
    //    btnPancard.Visible = false;
    //    //divconfirm.Visible = false;
    //    trPancardfrm1.Visible = true;
    //    trPancardfrm2.Visible = true;
    //}
    //protected void btnNo_Click(object sender, EventArgs e)
    //{
    //    trPancardfrm1.Visible = false;
    //    trPancardfrm2.Visible = false;
    //    //divconfirm.Visible = false;
    //    divControl.Visible = true;
    //}
}
