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

public partial class EnquiryForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetEmailId();
            BindDropdown("select * from StateTable", ddl_state, "StateID", "StateName", null, null);

        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (Session["CAPTCHA"].ToString().Equals(txtInput.Text))
        {
            sendmail();
            txtName.Text = "";
            txtAddress.Text = "";
            txtMobileNo.Text = "";
            txtMessage.Text = "";
            txtEmailId.Text = "";
            txtInput.Text = "";
            ddl_state.SelectedIndex = 0;
            ddl_city.SelectedIndex = 0;
            string url = "thank-you.html";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "redirect", "window.parent.location='" + url + "'", true);
        }
        else
        {
            txtInput.Text = "";
            return;
        }
    }

    public void BindDropdown(string sql, DropDownList InputDropDown, string ColValToBind, string ColTextToBind, string DropdownSelItem, string DropdownSelValue)
    {
        string ReturnString;
        string strQuery;
        strQuery = sql;
        try
        {
            // SqlDataReader dbDataReader;
            //if (cn.State == ConnectionState.Closed)
            //{
            //    cn.Open();
            //}
            //cmd = new SqlCommand(strQuery, cn);
            //dbDataReader = cmd.ExecuteReader();

            SqlParameter[] Statecity = new SqlParameter[]
            {
                
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.Text, strQuery, Statecity);
            InputDropDown.DataSource = ds;
            InputDropDown.DataTextField = ColTextToBind;
            InputDropDown.DataValueField = ColValToBind;
            InputDropDown.Items.Clear();
            InputDropDown.Items.Insert(0, new ListItem("Select", "Select"));
            InputDropDown.DataBind();

            //cn.Close();
            if (DropdownSelValue != "")
            {
                foreach (ListItem LstItem in InputDropDown.Items)
                {
                    if (DropdownSelItem == LstItem.Value)
                    {
                        LstItem.Selected = true;
                    }
                }
            }

        }
        catch (Exception objException)
        {
            ReturnString = "Error " + objException;
        }
    }

    private void sendmail()
    {
        if (txtName.Text != "" && txtEmailId.Text != "" && txtMobileNo.Text != "")
        {
            try
            {
                MailMessage message = new MailMessage();
                message.Body = mailmsg();
                message.IsBodyHtml = true;
                message.To.Add(lblemailid.Text.Trim());
                message.Bcc.Add("support@egenservices.com");
                message.From = new System.Net.Mail.MailAddress(txtEmailId.Text.ToString().Trim());
                message.Subject = "Venus Utility Services Enquiry Details";
                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);
                    //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Your information has been sent. We will contact you in 24 working hours');"), true);

                    lblerror.Text = "Your information has been sent. We will contact you in 24 working hours";
                    lblerror.ForeColor = Color.Blue;
                }
                message = null;
            }
            catch (Exception ex)
            {
                lblerror.Text = "Send Email Failed.<br>" + ex.Message;
                lblerror.ForeColor = Color.Red;

            }
        }
        else
        {
            lblerror.Text = "Please enter All Details";
            lblerror.ForeColor = Color.Red;

        }

    }
    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>::Venus Utility Services Enquiry Details::</b></td></tr>");
        strHTML.Append("<tr><td width=20%><b>Name</b></td><td width=80%>" + txtName.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Address</b></td><td width=80%>" + txtAddress.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>State</b></td><td width=80%>" + ddl_state.SelectedItem.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>City</b></td><td width=80%>" + ddl_city.SelectedItem.Text.Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Email id</b></td><td width=80%>" + txtEmailId.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Mobile</b></td><td width=80%>" + txtMobileNo.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("<tr><td width=20%><b>Message</b></td><td width=80%>" + txtMessage.Text.ToString().Trim() + "</td></tr>");
        //strHTML.Append("<tr><td width=20%><b>Profession</b></td><td width=80%>" + txtprofession.Text.ToString().Trim() + "</td></tr>");
        strHTML.Append("</table>");
        return strHTML.ToString();

    }
    protected void ddl_state_SelectedIndexChanged1(object sender, EventArgs e)
    {
        BindDropdown("select * from DistrictName where StateID='" + ddl_state.SelectedValue + "'", ddl_city, "CityId", "CityName", null, null);
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtAddress.Text = "";
        txtMobileNo.Text = "";
        txtMessage.Text = "";
        txtEmailId.Text = "";
        txtInput.Text = "";
        ddl_state.SelectedIndex = 0;
        ddl_city.SelectedIndex = 0;
    }
    private void GetEmailId()
    {
        try
        {
            SqlParameter[] check_DomainName = new SqlParameter[]
                {
                    //new SqlParameter("@IpAddress",Ipaddress)
                    new SqlParameter("@DomainName",ConfigurationManager.AppSettings["WebsiteUrl"])
                };

            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetWhiteLabelDetails", check_DomainName);
            if (ds.Tables[0].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    lblemailid.Text = dr[2].ToString();
                    //Session["helpdeskmobileno"] = dr[1].ToString();
                    //Session["helpdeskemailid"] = dr[2].ToString();
                    //Session["copyright"] = dr[3].ToString();
                }
            }

        }
        catch (Exception ex)
        {

        }
    }
}
