using System;
using System.Configuration;

using TopWinCMS.BLL; 
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin
{
    /// <summary>
    /// Admin_Login 的摘要说明。
    /// </summary>
    public partial class Admin_Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (ConfigurationManager.AppSettings["Test"].Length == 0)
            {
                Response.Redirect(@"../Install/Index.aspx");
            }

            if (!IsPostBack)
            {
                //读取上次登录时的账号
                if (Request.Cookies["Logined_UserName"] != null)
                {
                    TB_UserName.Text = Request.Cookies["Logined_UserName"].Value;
                }
                //TR_ValidateCode.Visible = RunSetting.PassCode;


                //TB_UserName.Attributes.Add("onfocus", "PassCodeInit();");
                //TTB_Password.Attributes.Add("onfocus", "PassCodeInit();");
                //NTB_ValidateCode.Attributes.Add("onfocus", "PassCodeInit();");

                System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StatisticalUsers));
                th.Start(Request.Url);
            }

        }

        protected void BT_Login_Click(object sender, System.EventArgs e)
        {
            TopWinCMS.Model.AdminInfo model = new TopWinCMS.Model.AdminInfo();
            model.UserName = TB_UserName.Text;
            model.Password = TTB_Password.Text;
            model.PassCode = NTB_ValidateCode.Text;

            string strErr;
            new LoginManage().TryLoginSystem(model, out strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr);
            }


        }
        private void StatisticalUsers(object Url)
        {
            try
            {
                Uri u = null;
                if (Url is Uri)
                {
                    u = Url as Uri;
                }
                if (u.Host.ToLower().Contains("localhost") || u.Host.Contains("192.168.") || u.Host.Contains("TopWinCMS.com"))
                {
                    return;
                }
                else
                {
                    new WebServ.TopWinService().StatisticalUsers(u.Host, Licenses.License.SN);
                }
            }
            catch { }
        }
    }
}