using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace JuSNS.Web
{
    public partial class EmailActive : JuSNS.UI.Page.BasePage
    {
        JuSNS.Home.User us = new JuSNS.Home.User();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Button1.Attributes.Add("onclick", "return checkfrm();");
                if (string.IsNullOrEmpty(Request.QueryString["email"]) && !string.IsNullOrEmpty(Request.QueryString["r"]))
                {
                    JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
                    mdl = us.GetUserInfo(Convert.ToInt32(Request.QueryString["r"]));
                    this.Email.Text = mdl.Email;
                }
                else
                {
                    this.Email.Text = Request.QueryString["email"];
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string email = this.Email.Text;
                if (string.IsNullOrEmpty(email))
                {
                    this.LtrMsg.Text = "<div class=\"msg-error\">电子邮件不能为空！</div>";
                }
                else
                {
                    if (!us.IsExist(email))
                    {
                        this.LtrMsg.Text = "<div class=\"msg-error\">电子邮件不存在！</div>";
                    }
                    else
                    {
                        JuSNS.Model.UserInfo ui = new JuSNS.Model.UserInfo();
                        ui.Email = email;
                        int n = us.EmailActive(email, ui);
                        if (n == 0)
                        {
                            this.LtrMsg.Text = "<div class=\"msg-succ\">一封邮件已经发送到您的电子邮件：" + email + "，请去查收。<a href=\"" + JuSNS.Config.EmailConfig.GetEmailLoginUrl(email) + "\" target=\"_blank\">点这里登录您的邮箱</a></div>";
                        }
                        else if (n == 2)
                        {
                            this.LtrMsg.Text = "<div class=\"msg-error\">此帐号为激活状态，无需激活！</div>";
                        }
                        else
                        {
                            this.LtrMsg.Text = "<div class=\"msg-error\">发送激活电子邮件失败！请与系统管理员联系。</div>";
                        }
                        
                    }
                }
            }
        }
    }
}
