using System;

namespace Stat.web.API
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UserLogin : System.Web.UI.Page
    {
        Stat.Security.ISecurity security = Stat.Security.DataFatory.CreateSecurity();
        Stat.Cache.IConfig config = Stat.Cache.CacheFatory.CreateConfig();
        string txtUserName = "", txtUserPwd = "", txtUrl = "";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName = Plugin.Common.Rq("Uname");
            txtUserPwd = Plugin.Common.Rq("Upwd");
            txtUrl = Plugin.Common.Rq("Uurl");
            Ulogin();
        }
        /// <summary>
        /// 用户登陆
        /// </summary> 
        private void Ulogin()
        {
            //检查是否开启
            if (!config.IsAllowUserLogin())
            {
                Response.Write("Failed! 温馨提示：系统暂时关闭用户登录！");
                Response.End();
                return;
            }
            //检查填写的表单
            if (!CheckForm()) { return; }
            //系统登陆
            Stat.Model.Users model = new Stat.Model.Users();
            model.UserName = txtUserName;
            model.UserPwd = txtUserPwd;
            if (security.Login(model))
            {
                if (txtUrl.Length > 0)
                    Response.Redirect(txtUrl);
                else
                    Response.Write("Succeed!");
                Response.End();
            }
            else
            {
                // Response.Write("Failed! 登录失败！");
                Plugin.Common.Alert_Top("登录失败！", "/");
            }
        }
        /// <summary>
        /// 检查表单
        /// </summary>
        private bool CheckForm()
        {
            if (txtUserName.Equals("") || txtUserPwd.Equals(""))
            {
                Plugin.Common.Alert_Top("用户名,密码不能够为空！", "/");
                //  Response.Write("Failed! 用户名,密码不能够为空！");
                Response.End();

                return false;
            }
            return true;
        }
    }
}
