using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CitySunlight
{
    public partial class Default : System.Web.UI.Page
    {
        protected string msg;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                Response.Redirect("../Main.aspx");
            }
        }

        protected void login_Click(object sender, EventArgs e)
        {
            //String username = UserNameTx.Value;
            String password = HttpUtils.getElementsbyName("PassWordTx");
            String username = HttpUtils.getElementsbyName("UserNameTx");

            if ((username.Length == 0) || (password.Length == 0))
            {
                msg = "请填写完整的账号和密码!";
                return;
            }

            if (!UserManager.isExist(username))
            {
                msg = "当前用户不存在!";
                return;
            }

            if (UserManager.GetTimes(username) > 2)
            {
                msg = "登录错误超过3次,已被锁定,请联系管理员!";
                return;
            }

            if (UserManager.LoginUser(username, password))
            {
                Session["username"] = username;
                Response.Write("<script>alert('登录成功,欢迎回来" + username + "!')</script>");
                Response.Redirect("../Main.aspx");
            }
            else
            {
                msg = "账号密码错误,还可以尝试" + (4 - UserManager.GetTimes(username)) + "次..!";
            }
        }

        protected void register_Click(object sender, EventArgs e)
        {
            Response.Redirect("./Register.aspx");
        }
    }
}