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

namespace JuSNS.Web
{
    public partial class login : JuSNS.UI.Page.BasePage
    {
        protected string email = string.Empty;
        protected int flag = -1;
        protected bool isCode = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (JuSNS.Common.Public.GetXMLValue("loginCode") == "1")
            {
                isCode = true;
            }
            if (!IsPostBack)
            {
                string r = Request.QueryString["r"];
                if (r == "out")
                {
                    HttpCookie SNSToKenCookie = Context.Request.Cookies["SNSUserPassPort"];
                    if (SNSToKenCookie != null)
                    {
                        SNSToKenCookie.Values.Clear();
                        SNSToKenCookie.Domain = JuSNS.Config.UiConfig.CookieDomain;
                        SNSToKenCookie.Expires = DateTime.Now.AddDays(20);
                        Response.SetCookie(SNSToKenCookie);
                    }
                    Response.Redirect(rooDir + "/");
                }
                else
                {
                    if (isCode)
                    {
                        string errorStr = string.Empty;
                        if (Session["CheckCode"] == null)
                        {
                            PageError("验证码过期，请重新登陆！", rooDir + "/");
                        }
                        if (string.IsNullOrEmpty(Session["CheckCode"].ToString()))
                        {
                            PageError("验证码过期，请重新登陆！", rooDir + "/");
                        }
                        else
                        {
                            if (Request.Form["TxtLogCode"] != Session["CheckCode"].ToString())
                            {
                                PageError("验证码错误，请重新登陆！", rooDir + "/");
                            }
                        }
                    }
                    if (Request.Form["TxtLogEmail"] == null || Request.Form["TxtLogPassword"] == null
                        || Request.Form["TxtLogEmail"].Trim() == string.Empty || Request.Form["TxtLogPassword"].Trim() == string.Empty)
                    {
                        if (Request.QueryString["a"] == "relog")
                        {
                            PageError("登录已经过期！", rooDir + "/");
                        }
                        else
                        {
                            PageError("请填写用户名和密码！", rooDir + "/");
                        }
                    }
                    else
                    {
                        email = Request.Form["TxtLogEmail"].Trim();
                        string Password = Request.Form["TxtLogPassword"].Trim();
                        string IsAuto = Request.Form["ChbLogAuto"];
                        string urls = Request.Form["Retrunurls"];
                        Home.User us = new Home.User();
                        int userId = 0;
                        string userName = string.Empty;
                        string userPortrait = string.Empty;
                        int loginNum = 0;
                        EnumLoginState state = us.Login(email, Password, out userId, out userName, out userPortrait, out loginNum);
                        switch (state)
                        {
                            case EnumLoginState.Succeed:
                                bool auto = false;
                                if (IsAuto == "on")
                                {
                                    auto = true;
                                }
                                JuSNS.UI.Page.UserPage.SetCookie(userId, userName, Input.MD5(Password, false), userPortrait, auto);
                                us.updateinte(userId, 1, 0, 0, "登录");
                                if (!string.IsNullOrEmpty(urls) && urls.IndexOf("inc/") == -1 && urls.IndexOf("/login.aspx") == -1 && urls.IndexOf("/home/ram/net.aspx") == -1)
                                {
                                    if (string.IsNullOrEmpty(userPortrait) && loginNum == 0)
                                    {
                                        Response.Redirect(rooDir + "/home/space/edit.aspx?f=1");
                                    }
                                    else
                                    {
                                        Response.Redirect(urls.Replace("-----", "&"));
                                    }
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(userPortrait) && loginNum == 0)
                                    {
                                        Response.Redirect(rooDir + "/home/space/edit.aspx?f=1");
                                    }
                                    else
                                    {
                                        Response.Redirect(rooDir + "/home");
                                    }
                                }
                                return;
                            case EnumLoginState.Err_UnActivation:
                                PageError("帐号(" + email + ")未通过<a href=\"EmailActive.aspx\">电子邮件激活</a>", email);
                                break;
                            case EnumLoginState.Err_NameOrPwdError:
                                PageError("用户名(" + email + ")和密码错误！", email);
                                break;
                            case EnumLoginState.Err_Locked:
                                PageError("用户(" + email + ")已经被锁定！", email);
                                break;
                        }
                    }
                    DataBind();
                }
            }
        }
    }
}
