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

public partial class ForgotPassword : System.Web.UI.Page
{
    int status;
    string password, name, emailid, status1;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            GetWhitelabelDetails();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  


    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        try
        {
            forgetpassword();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  


    }
    private void forgetpassword()
    {
        if (txtemail.Text != "")
        {
            try
            {
                SqlParameter[] forgetpassword = new SqlParameter[]
                {
                        new SqlParameter("@emailid",txtemail.Text.Trim())
                };
                SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_partner_passwordrecovery", forgetpassword);

                if (sdr.Read())
                {
                    password = sdr["password"].ToString();
                    emailid = sdr["emailid"].ToString();
                    name = sdr["username"].ToString();
                    status = 1;
                }
                if (status == 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Your e-mail is not registered with us. Kindly use registered e-mail id to retrieve your password.');"), true);
                }
                else
                {
                    string body = mailmsg();
                    string sub = "Partner login Account Details of Venus Utility Services";
                    SendMailmsg(body, emailid, "customercare@venusrecharge.com", sub);
                    
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Your current password has been sent to your registered e-mail id. Please check your mail box.');"), true);
                    
                }
            }
            catch (Exception ex)
            {
                lblsucess.Text = "Send Email Failed-Technical Error.<br>" + ex.Message;
                lblsucess.ForeColor = Color.Red;
            }
        }
    }
    private string mailmsg()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table style='font-family:Calibri; width:80%'>");
        strHTML.Append("<tr><td align='left' colspan='2'>Dear " + "User" + ",</td></tr>");
        strHTML.Append("<tr><td colspan='2'><p>Here are the login details of your account. Please use the username and password detailed below to access your account.</p></td></tr>");
        strHTML.Append("<tr><td width='50%'>User Name :</td><td width='50%'>" + name + "</td></tr>");
        strHTML.Append("<tr><td width='50%'>Password :</td><td width='50%'>" + password + "</td></tr>");
        strHTML.Append("<tr><td width='50%'></td><td width='50%'>" + "" + "</td><br /></tr>");
        strHTML.Append("<tr><td colspan='2' align='left'><p>Regards</p></td></tr>");
        strHTML.Append("<tr><td colspan='2' align='left'><p></p></td></tr>");
        strHTML.Append("<tr><td colspan='2' align='left'><p>" + Session["CompanyName"] + "</p></td></tr>");
        strHTML.Append("<tr><td colspan='2' align='left'><p><a href=" + ConfigurationManager.AppSettings["WebsiteUrl"] + "/login.aspx target='_blank'>" + ConfigurationManager.AppSettings["WebsiteUrl"] + "/login.aspx</a></p></td></tr>");
        strHTML.Append("<tr><td colspan='2'><p></p></td></tr>");
        strHTML.Append("<tr><td colspan='2'><p>Note: Please do not reply to this mail.</p></td></tr>");
        strHTML.Append("</table>");
        return strHTML.ToString();
    }
    private string SendMailmsg(string body, string msgto, string msgfrom, string sub)
    {
        MailMessage message = new MailMessage();
        message.Body = body;
        message.IsBodyHtml = true;
        message.To.Add(msgto);
        message.From = new System.Net.Mail.MailAddress(msgfrom);
        message.Subject = sub;
        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;
        status1 = "success";
        return status1;      
    }

    protected void btnGoback_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }

    private void GetWhitelabelDetails()
    {
        try
        {
            SqlParameter[] check_DomainName = new SqlParameter[]
                {
                    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)
                {
                    lblCopyright.Text = dr[3].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            throw(ex);
        }
    }

    
}
