using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.Security;
using BusinessLayer;
using NikaiDSR;
using DTO;

namespace UI
{
    public partial class ForgotPassword : Page
    {

        #region code declaration
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            Page.Title = "NIKAI- FORGOT PASSWORD";

        }

        #region code btnSubmit_Command
        protected void btnSubmit_Command(object sender, CommandEventArgs e)
        {
            bool flag = true;
            string errorMessage = "";
            string InvalidMsg = "Invalid Email!";
            int User_Id = 0;
            if (e.CommandName == "save")
            {
                if (txtEmail.Text.Trim().Length == 0)
                {
                    flag = false;
                    errorMessage = InvalidMsg;
                }
                else
                {
                    UserDTO user = UM.IsValidEmail(txtEmail.Text.Trim());
                    if (user == null)
                    {
                        flag = false;
                        errorMessage = InvalidMsg;
                    }
                    else
                    {
                        // new EmqubeLib.Email().SendMail();

                        btnSubmit.Visible = false;
                        Session.Abandon(); // Remove session and redirect to login page
                        FormsAuthentication.SignOut();
                        string redirectionScript = "<script language='javascript'>Delayer()</script>";
                        String csname = "Startup";
                        Type cstype = this.GetType();
                        ClientScript.RegisterStartupScript(cstype, csname, redirectionScript);

                        String EmailBody = "Dear " + user.Name + ",<br/><br/>To access the online Nikai Sales Reporting website."
                                               + "<br/><br/> Your login details are : <b><br/>Site URL: <><br/>User name is: " + user.Email + "<br/>Password is: " + user.Password
                                               + "<br/><br/>Best Regards,<br/>Nikai Admin";

                        Helper.Common.SendEmail(user.Email, "Nikai - Your Login Details", EmailBody);

                        errorMessage = "New Password has been sent to your email.";
                    }
                }

                
                if (flag == false)
                {
                    errorMessage = InvalidMsg;
                    flag = false;
                }

                lblMsg.Text = errorMessage;
                if (flag)
                {
                    lblMsg.CssClass = "sucessMsg";
                }
                else
                    lblMsg.CssClass = "errorMsg";
            }

            if (e.CommandName == "cancel")
            {
                Response.Redirect("~/Login.aspx");
            }
        }
        #endregion


    }
}