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 Agent_PanCardDetail : System.Web.UI.Page
{

    string refrenceno, CouponValue, ResponseDate;
    int PanCardStatus;
    decimal balance, cutoffBalance, deduct;
    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");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }

    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;
        }
    }


    public void GetStatusForPanCard()
    {
        try
        {
            SqlParameter[] get_pancard_status = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_get_user_status_for_pancard", get_pancard_status);
            if (dr.Read())
            {
                PanCardStatus = Convert.ToInt32(dr["PanCardFlag"]);
            }
            //ViewState["leftbalance"]=Label5.Text;
        }
        catch (Exception ex)
        {
            Server.Transfer("Error.aspx?id=" + ex + "");
            //string st = ex.Message;
        }
    }

    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 Get_Deducted_Pancard_Charge()
    {

        try
        {
            SqlParameter[] get_pancard_status = new SqlParameter[]
            {
                new SqlParameter("@DomainName",ConfigurationManager.AppSettings["WebsiteUrl"])
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "spGetPancardCharge", get_pancard_status);
            if (dr.Read())
            {
                deduct = Convert.ToDecimal(dr["WlSerChaInRS"]);

            }


        }
        catch (Exception ex)
        {

        }
    }
    public void GetCoupanDetails()
    {
        try
        {
            SqlParameter[] Coupan = new SqlParameter[] 
            {
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_getCoupanNo", Coupan);
            if (ds.Tables[0].Rows.Count != 0)
            {
                CouponValue = Convert.ToString(ds.Tables[0].Rows[0]["CoupanNo"]);
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Coupon not available.');"), true);
            }
        }
        catch (Exception ex)
        {

        }
    }
    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);
            Session["Name"] = txtName.Text;
            Get_Deducted_Pancard_Charge();
            GetStatusForPanCard();
            if (PanCardStatus == 1)
            {
                userdetails();
                GetCoupanDetails();
                if (CouponValue != null)
                {
                    Session["CouponNo"] = CouponValue;
                    if ((balance - cutoffBalance) < deduct)
                    {
                        lblMsg.Text = "You don’t have enough balance to do this transaction. Please take balance in your login.";
                        lblMsg.ForeColor = Color.Red;
                    }
                    else
                    {
                        Session["CouponNo"] = CouponValue;
                        DeductAmountPanCardFees();
                        Response.Redirect("PanAcknowledgement.aspx");

                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Coupon not available.');"), true);
                    txtAddress.Text = "";
                    txtMobNo.Text = "";
                    txtName.Text = "";
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Services Inactive, Pls contact Administrator.');"), true);
                txtAddress.Text = "";
                txtMobNo.Text = "";
                txtName.Text = "";

            }
        }
        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("@CoupanNo",CouponValue),
            new SqlParameter("@AgentID",Session["LOGINID"].ToString()),
            new SqlParameter("@name",txtName.Text.Trim()),
            new SqlParameter("@mobile",txtMobNo.Text.Trim()),
            new SqlParameter("@custAddress",txtAddress.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("@amount",deduct),
            new SqlParameter("@ErrorMessage",SqlDbType.Int)
        };
        pancardservice[12].Direction = ParameterDirection.Output;
        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "[udp_Pan_Card_Services_new]", pancardservice);
        int result = Convert.ToInt32(pancardservice[12].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("Pan card details process successfully"), true);
                       
        }
    }

    public void UserCutOffDetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",Session["LOGINID"].ToString())
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getcutoffbalance", get_user_details);
            if (dr.Read())
            {
                cutoffBalance = Convert.ToInt32(dr["CuttOffAmount"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }
}
