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

public partial class CallMe : System.Web.UI.Page
{
    string MobileNumber;
    string Registeredname;
    string transactionid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"].Equals(null))
            {
                Response.Redirect("../login.aspx");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Getmobilenumber();
            Insertuserdetail();
            sendmail();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void Insertuserdetail()
    {
        try
        {
            Random rand = new Random();
            rand.Next(0, 1);
            for (int i = 0; i <= 1; i++)
            {
                transactionid = RandomString(rand, 10);
            }
            SqlParameter[] savedetail = new SqlParameter[]
            {
                new SqlParameter("@CURRENTLEVEL",Session["LOGINID"].ToString()),                
                new SqlParameter("@AlternateMobNo",txt_Alt_Mob_No.Text.Trim()),
                new SqlParameter("@Timeforcall",ddl_TimeforCall.SelectedItem.Text.Trim()),
                new SqlParameter("@Problem",txt_CallDes.Text.Trim()),
                new SqlParameter("@MobileNo",MobileNumber),                
                new SqlParameter("@Transactionid",transactionid),
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_INSERT_CAllME", savedetail);            
            
        }
        catch(Exception ex)
        {

        }
    }
    private void Getmobilenumber()
    {
        SqlParameter[] get_user_details = new SqlParameter[]
        {
            new SqlParameter("@LoginID",Session["Loginid"].ToString()), 
        };
        SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_USERDETAILS", get_user_details);
        if (dr.Read())
        {
            MobileNumber = Convert.ToString(dr["MobileNo"]);
            Registeredname = Convert.ToString(dr["firstname"]);
        }
        dr.Close();
    }
    private void sendmail()
    {
        try
        {
            MailMessage message = new MailMessage();
            message.Body = mailmsgmobile();
            message.IsBodyHtml = true;
            message.To.Add(Session["helpdeskemailid"].ToString());
            message.Bcc.Add("acvenus27@gmail.com");
            message.Bcc.Add("venushelp@yahoo.com");
            message.From = new System.Net.Mail.MailAddress("enquiry@egenservices.com");
            message.Subject = "Call Me Partner Query.";
            message.Priority = MailPriority.High;
            SmtpClient SmtpMail = new SmtpClient();
            {
                SmtpMail.Host = "smtp.gmail.com";
                SmtpMail.Port = 587;
                SmtpMail.EnableSsl = true;
                SmtpMail.UseDefaultCredentials = true;
                SmtpMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                SmtpMail.Credentials = new System.Net.NetworkCredential("venusutilityservices@gmail.com", "Venus@123");
                SmtpMail.Send(message);
            }
            message = null;
            ddl_TimeforCall.SelectedIndex = 0;
            //ddlServiceName.SelectedIndex = 0;
            txt_Alt_Mob_No.Text = "";
            txt_CallDes.Text = "";
            lblerror.Text = "You have successfully submitted Request for Call. You will get call on given time and number.Your Ref.No. " + transactionid;
            lblerror.ForeColor = Color.Blue;
        }
        catch (Exception ex)
        {
            lblerror.Text = "Send Email Failed.<br>" + ex.Message;
            lblerror.ForeColor = Color.Red;
        }
    }
    private string mailmsgmobile()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table width=70% style='font-family:Trebuchet MS'>");
        strHTML.Append("<tr><td width=100%>" + Session["CompanyName"] + ", Partner Query.</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Time for Call</b></td><td width=70% align=left>" + ddl_TimeforCall.SelectedItem.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Date</b></td><td width=70% align=left>" + DateTime.Now + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>User Id</b></td><td width=70% >" + Session["USERNAME"].ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>User Name</b></td><td width=70%>" + Registeredname.ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>User Mobile Number</b></td><td width=70% >" + MobileNumber.ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Alternet Number</b></td><td width=70% align=left>" + txt_Alt_Mob_No.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Email Id</b></td><td width=70% >" + Session["EMAILID"].ToString() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Problem In</b></td><td width=70% align=left>" + txt_CallDes.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20% align=left>Transaction ID</b></td><td width=70% align=left>" + transactionid + "</td></tr>");        
        strHTML.Append("</table>");
        return strHTML.ToString();
    }
    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();
    }
}
