using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LYFZ.WeixinServers.WeiXinAPP
{
    public class CommonHandler
    {
        public CommonHandler() { 
        
        }
        public static LYFZ.WeixinServiceDate.Model.Model_AdminUser GetAdminUser(System.Web.UI.Page page)
        {
            LYFZ.WeixinServiceDate.Model.Model_AdminUser retModel = new WeixinServiceDate.Model.Model_AdminUser();
            if (CheckLogin(page))
            {
                retModel = (LYFZ.WeixinServiceDate.Model.Model_AdminUser)page.Session["Login"];
            }
            else {
                retModel.ID = -1;
                retModel.UserName = "未登录";
            }
            return retModel;
        }
        /// <summary>
        /// 检查当前帐号是否有操作DZKJ的权限
        /// </summary>
        /// <param name="page"></param>
        /// <param name="isJump">在没有权限时是否跳转到首页</param>
        /// <returns></returns>
        public static bool CheckAdmin(System.Web.UI.Page page, bool isJump=true)
        {
            bool ret = false;
            int Competence=GetAdminUser(page).Competence;
            if ( isAdmin(page, false) || (page.Request.Url.ToString().ToLower().Contains("/DZKJ_".ToLower()) && (Competence == 1 || Competence == 2)))
            {
                ret = true;
            }
            else {
                if (isJump)
                {
                    page.Response.Write("<script>alert('对不起,你没有此操作权限。');document.location='/WeiXinAPP/index.aspx';</script>");
                }
            }
            return ret;
        }
        /// <summary>
        /// 检查当前帐号是否为超级管理员
        /// </summary>
        /// <param name="page"></param>
        /// <param name="isJump">在没有权限时是否跳转到首页</param>
        /// <returns></returns>
        public static bool isAdmin(System.Web.UI.Page page, bool isJump=true)
        {
            bool ret = false;
            int Competence = GetAdminUser(page).Competence;
            if (Competence == 99)
            {
                ret = true;
            }
            else {
                if (isJump)
                {
                    page.Response.Write("<script>alert('对不起,你没有此操作权限。');document.location='/WeiXinAPP/index.aspx';</script>");
                }
            }
            return ret;
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="page"></param>
        public static void Logout(System.Web.UI.Page page)
        {
            LYFZ.Command.Command_Session.Del("Login");
            page.Response.Write("<script>document.location='/WeiXinAPP/Login.aspx';</script>"); return; 
        }
        /// <summary>
        /// 检查是否已登录,如果没有登录就跳转到登录页
        /// </summary>
        /// <param name="page"></param>
        public static void CheckLoginJump(System.Web.UI.Page page)
        {
            if (!CheckLogin(page))
            {
                page.Response.Write("<script>alert('你还没有登录或已超时,请重新登录!');document.location='/WeiXinAPP/Login.aspx';</script>"); return; 
            }
        }

        /// <summary>
        /// 检查是否已登录
        /// </summary>
        /// <param name="page"></param>
        public static bool CheckLogin(System.Web.UI.Page page)
        {
            if (page.Session["Login"] == null)
            {
                return false;
            }
            else
            {
                LYFZ.Command.Command_Session.AddObject("Login", page.Session["Login"],300);
                return true;
            }
        }

        public static string I18NAccountType(int TypeFlag){
            switch (TypeFlag)
            {
                case 0: return "普通用户";
                case 1: return "点赞科技管理员";
                case 2: return "点赞科技分销商";
                case 99: return "超级管理员";
                default: return "参数错误";
            }
        }
    }
}