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;
using System.Net;
using System.Net.Mail;
using System.Threading;

public partial class Panel_Premium : System.Web.UI.Page
{
    string refrenceno;
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString();
    float balance;
    int balanceavailable;
    int operatorcommission;
    int  NetAmount;
    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);
        }    
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            insertinsuranceservice();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    private void insertinsuranceservice()
    {
        try
        {
            userdetails();
            string Amount = txtPolicyAmount.Text;

            balanceavailable = Convert.ToInt32(Amount);
            if (balanceavailable < balance)
            {
                Random rand = new Random();
                rand.Next(0, 1);
                for (int i = 0; i <= 1; i++)
                {
                    refrenceno = RandomString(rand, 15);
                    //lbluniqueid.Text = abc;
                }
                SqlParameter[] param_dthrecharge = new SqlParameter[] 
                { 
                    new SqlParameter("@AgentID",Session["LOGINID"].ToString()),
                    new SqlParameter("@companyname",ddl_CompName.SelectedItem.Text.Trim()),
                    new SqlParameter("@ReferenceNumber",refrenceno),
                    new SqlParameter("@policyno",txtPolicyNo.Text.Trim()),
                    new SqlParameter("@Amount",txtPolicyAmount.Text.Trim()),
                    new SqlParameter("@policyholdername",txtHolderName.Text.Trim()),
                    new SqlParameter("@mobile",txtMobileNo.Text.Trim()),                    
                    new SqlParameter("@IPAddress",IPAddress),
                    new SqlParameter("@ResultOutCommission",SqlDbType.Int),
                    new SqlParameter("@ErrorMessage",SqlDbType.Int),                    
                };
                param_dthrecharge[8].Direction = ParameterDirection.Output;
                param_dthrecharge[9].Direction = ParameterDirection.Output;
                SqlDataReader dr= SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpOfflineServiceInsurance_TEST", param_dthrecharge);
                int result = Convert.ToInt32(param_dthrecharge[9].Value);
                if (result == 1)
                {
                    Label1.Text = "There is some technical problem .Please try again";
                    Label1.ForeColor = Color.Red;
                }
                else
                {
                    operatorcommission = Convert.ToInt32(param_dthrecharge[8].Value);
                    NetAmount = Convert.ToInt32(Amount) - operatorcommission;
                    string[] mailData = new string[5];
                    mailData[0] = mailmsg();
                    mailData[1] = "shalendra.egen@gmail.com";
                    mailData[2] = "sachin.egen@gmail.com";
                    mailData[3] = Convert.ToString(Session["EMAILID"]);
                    mailData[4] = "EGen Premium Payment Request Information";

                    Thread thmail = new Thread(new ParameterizedThreadStart(SendMail));
                    thmail.Start(mailData);
                    CommonFunction.ClearControlValues(insurance);
                    Label1.Text = "Your request for Premium Payment is sucessfully submitted. Ref.No." + refrenceno;
                    Label1.ForeColor = Color.Blue;
                }

            }
            else
            {
                Label1.Text = "You don’t have enough balance to do this transaction. Please take balance in your login.";
                Label1.ForeColor = Color.Red;
            }
        }
        catch (Exception ex)
        {
            //Server.Transfer("Error.aspx?id=" + Label1.Text + "");
            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.ToInt32(dr["balance"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
            throw (ex);
        }
    }
    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();
    }
    private string mailmsg()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table width=70% border=1 style='font-family:Trebuchet MS'>");
        strHTML.Append("<tr><td width=100% colspan=2 align=center ><b>Egen " + "EGen Premium Payment Request Information" + "</b></td></tr>");
        strHTML.Append("<tr><td width=20%><b>Email Id</b></td><td width=80%>" + Session["EMAILID"].ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Login id</b></td><td width=80%>" + Session["LOGINID"].ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>User Name</b></td><td width=80%>" + Session["USERNAME"].ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Company Name</b></td><td width=80%>" + ddl_CompName.SelectedItem.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Policy No</b></td><td width=80%>" + txtPolicyNo.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Request Amount</b></td><td width=80%>" + txtPolicyAmount.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Service Charge</b></td><td width=80%>" +'0'+ "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Commission</b></td><td width=80%>" + operatorcommission + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Net Amount</b></td><td width=80%>" + NetAmount + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Policy Holder Name</b></td><td width=80%>" + txtHolderName.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Mobile no.</b></td><td width=80%>" + txtMobileNo.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("</table>");
        return strHTML.ToString();
    }
    private void SendMail(object mail)
    {
        string[] mailData = new string[5];
        mailData = (String[])mail;
        CommonFunction.SendMailmsg(mailData[0], mailData[1], mailData[2], mailData[3], mailData[4]);
    }
   
}
