using System;
using System.Web.UI;
namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Login : System.Web.UI.Page
    {
        Stat.Security.ISecurity security = Stat.Security.DataFatory.CreateSecurity();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login_Click(object sender, ImageClickEventArgs e)
        {
            this.btnLogin.Enabled = false;

            //检查验证码
            if (!security.CheckCode("CheckCode", this.txtCheckCode.Text))
            {
                this.btnLogin.Enabled = true; //先解放按钮
                return;
            }
            /*-------------------------------------*/

            //检查填写的表单
            if (!CheckForm())
            {
                this.btnLogin.Enabled = true; //先解放按钮
                return;
            }
            /*-------------------------------------*/
            //系统登陆
            //系统登陆
            Stat.Model.Users model = new Stat.Model.Users();
            model.UserName = this.txtUserLoginName.Text.Trim();
            model.UserPwd = this.txtUserLoginPwd.Text.Trim();
            if (security.Login(model))
            {
                this.btnLogin.Enabled = true;

                if (security.IsMaster())
                {
                    Response.Redirect("/Manager/Main.Aspx");
                }
                else if (security.IsUser())
                {
                    Response.Redirect("/v/WebsiteList.aspx");
                }
                Response.End();
            }
            else
            {
                this.btnLogin.Enabled = true;
            }

        }

        /// <summary>
        /// 检查表单
        /// </summary>
        private bool CheckForm()
        {
            //系统登陆
            string userLoginName = this.txtUserLoginName.Text;
            string userLoginPwd = this.txtUserLoginPwd.Text;

            if (userLoginName.Equals("") || userLoginPwd.Equals(""))
            { 
                Plugin.Common.Alert("温馨提示：用户名或者密码为空");
                this.btnLogin.Enabled = true;
                return false;
            }
            return true;
        }
    }
}
