using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using JuSNS.Config;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.UI.Page
{
    public class ManagePage : BasePage
    {
        string loginUrl = "/system/login.aspx";

        protected string GetSystemMenu(string sort)
        {
            string listStr = string.Empty;
            listStr += "<li><a href=\"http://www.juSNS.com\" target=\"_blank\" title=\"访问聚网社交软件官方站\"><img border=\"0\" src=\""+rooDir+"/system/images/skin/logo.gif\" /></a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/album\">相册</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/mood\">心情</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/blog\">日志</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/news\">热文</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/group\">群组</a></li>\r\n";
            //listStr += "<li><a href=\"" + rooDir + "/system/share\">分享</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/gift\">礼物</a></li>\r\n";
            //listStr += "<li><a href=\"" + rooDir + "/system/bill\">账本</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/movie\">电影</a></li>\r\n";
            //listStr += "<li><a href=\"" + rooDir + "/system/vote\">投票</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/sys/gbook.aspx\">留言</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/user\">会员</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/ask\">问答</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/app\">应用</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/ads\">广告</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/sys\">系统</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/start\">初始化</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/help\">帮助</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/admin\">账户</a></li>\r\n";
            listStr += "<li><a href=\"" + rooDir + "/system/login.aspx?r=out\" title=\"退出登录\">注销</a></li>\r\n";
            listStr += "<li style=\"font-weight:normal;font-size:10px;font-family:'lucida grande', tahoma, arial, 'bitstream vera sans', helvetica, sans-serif;\">" + JuSNS.Config.UiConfig.version + "</li>\r\n";
            return listStr;
        }

        /// <summary>
        /// 私有变量-权限代码,在子类中必须初始化的
        /// </summary>
        private string _Authority_Code = string.Empty;
        /// <summary>
        /// 检查权限
        /// </summary>
        protected void CheckAdminAuthority()
        {
            SNSToKenCookie = Context.Request.Cookies["SnsAdminToken"];
            if (SNSToKenCookie == null || SNSToKenCookie["token"] == null)
                base.CheckResultShow(JuSNS.Model.EnumLoginState.Err_AdminTimeOut, loginUrl);
            string adminCookie = SNSToKenCookie["token"];
            this.UserName = Input.URLDecode(SNSToKenCookie["username"]);
            string desstr = JuSNS.Common.DES.Decrypt(adminCookie, SecretConfig.AdminKey, SecretConfig.AdminIV);
            string[] userInfo = desstr.Split(SecretConfig.AdminSeparator);
            EnumLoginState state = EnumLoginState.Err_AdminTimeOut;
            if (userInfo.Length == 3)
            {
                UserID = int.Parse(userInfo[0]);
                string pass = userInfo[1];
                string cookip = userInfo[2];
                if (cookip != Public.GetClientIP())
                {
                    state = EnumLoginState.Err_AdminTimeOut;
                }
                else
                {
                    JuSNS.Manage.Manager mg = new JuSNS.Manage.Manager();
                    state = mg.CheckLogin(UserID, pass);
                }
            }
            if (state == EnumLoginState.Succeed)
            {
                //SNSToKenCookie.Expires = DateTime.Now.AddMinutes(60);
                Context.Response.AppendCookie(SNSToKenCookie);
            }
            else
            {
                CheckResultShow(state, rooDir + "/system/login.aspx");
            }
        }
        /// <summary>
        /// 设置登录的Cookie
        /// </summary>
        /// <param name="adminId">用户ID</param>
        /// <param name="userName">用户姓名</param>
        /// <param name="Password">已Md5加密后的密码</param>
        /// <param name="userPortrait">用户的头像</param>
        /// <param name="isAlways">是否保持登录状态(下次同一计算机免登录)</param>
        static public void SetCookie(int adminId, string trueName, string Password)
        {
            HttpCookie SNSToKenCookie = new HttpCookie("SnsAdminToken");
            string s = adminId.ToString() + SecretConfig.AdminSeparator.ToString() + Password + SecretConfig.AdminSeparator.ToString() + Public.GetClientIP();
            SNSToKenCookie["token"] = DES.Encrypt(s, SecretConfig.AdminKey, SecretConfig.AdminIV);
            SNSToKenCookie["username"] = Input.URLEncode(trueName);
            //SNSToKenCookie.Expires = DateTime.Now.AddMinutes(60);
            HttpContext.Current.Response.AppendCookie(SNSToKenCookie);
        }
        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagePage_Load(object sender, EventArgs e)
        {
            CheckAdminAuthority();
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public ManagePage()
        {
            this.Load += new EventHandler(ManagePage_Load);
        }
        /// <summary>
        /// 退出处理
        /// </summary>
        protected override void Logout()
        {
            base.Logout();

            //if ((_dirDumm).Trim() != "") { _dirDumm = "/" + _dirDumm; }
            //TmpPath = JuSNS.Config.UiConfig.dirMana + "/";
            //ExecuteJs("top.location.href=\"" + _dirDumm + "/" + TmpPath + "login.aspx\";");
            Context.Response.End();
        }
        /// <summary>
        /// 设置权限编号，要在子类的构造函数里设置
        /// </summary>
        protected string Authority_Code
        {
            set { _Authority_Code = value; }
        }
    }
}
