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;
using JuSNS.Config;
using JuSNS.Common;
using JuSNS.Model;

namespace JuSNS.Web
{
    public partial class restpwd : JuSNS.UI.Page.BasePage
    {
        protected string semail;
        protected string scode;
        protected void Page_Load(object sender, EventArgs e)
        {
            scode = Request.QueryString["r"];
            semail = Request.QueryString["email"];
            JuSNS.Home.User us = new JuSNS.Home.User();
            //判断是否存在。
            if (string.IsNullOrEmpty(scode) || string.IsNullOrEmpty(semail) || !us.getRestPwdRecord(scode, semail))
            {
                this.errresetpwd.Visible = true;
                this.rpwds.Disabled = true;
            }
            if (Request.Form["Action"] == "Submit")
            {
                string pass = Request.Form["TxtNewPass"];
                string _email = Request.Form["Restpwdsemail"];
                if (string.IsNullOrEmpty(pass) || string.IsNullOrEmpty(_email))
                {
                    this.errresetpwd.Visible = true;
                    this.rpwds.Disabled = true;
                }
                else
                {
                    string _code = JuSNS.Common.Input.URLDecode(Request.Form["code"]);
                    int userId;
                    string userName, userPortrait;
                    int n = us.ResetPwd(_email, pass, _code, out userId, out userName, out userPortrait);
                    if (n == 0)
                    {
                        JuSNS.UI.Page.UserPage.SetCookie(userId, userName, Input.MD5(pass, false), userPortrait, false);
                        Response.Write("<script type=\"text/javascript\">location.href='" + rooDir + "/';</script>");
                    }
                    else if (n == -1)
                    {
                        Response.Redirect("" + rooDir + "/?r=找不到申请记录。");
                    }
                    else if (n == -2)
                    {
                        Response.Redirect("" + rooDir + "/?r=您已经重新找回过密码了。");
                    }
                    else if (n == -3)
                    {
                        Response.Redirect("" + rooDir + "/?r=无效的电子邮件。");
                    }
                    else
                    {
                        Response.Redirect("" + rooDir + "/?r=错误的参数。");
                    }
                }
            }
        }
    }
}
