using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;

namespace CallCenter.Utility
{
    public class LoginUser
    {

        private HttpRequest m_Request = null;
        private HttpContext m_HttpContent = null;
        private HttpResponse m_Response = null;
        private Page m_Page = null;
        //private HttpCookie m_Cookie = null;
        /// <summary>
        /// 构造器,读cookie使用
        /// </summary>
        /// <param name="p_HttpContext">Http请求上下文</param>
        public LoginUser(HttpContext p_HttpContext)
        {
            m_Request = p_HttpContext.Request;
            m_Response = p_HttpContext.Response;
            m_Page = (Page)p_HttpContext.CurrentHandler;
            m_HttpContent = p_HttpContext;
        }
        /// <summary>
        /// 构造器，写cookie使用
        /// </summary>
        /// <param name="p_HttpContext">Http请求上下文</param>
        /// <param name="p_WriteFlag">写标志，为true时设置写的Cookie的相关信息</param>
        public LoginUser(HttpContext p_HttpContext, bool p_WriteFlag)
        {
            m_Request = p_HttpContext.Request;
            m_Response = p_HttpContext.Response;
            m_Page = (Page)p_HttpContext.CurrentHandler;
            m_HttpContent = p_HttpContext;
            if (p_WriteFlag)
            {
                DateTime _Date = DateTime.Now.AddDays(7);
                m_Page.Response.Cookies["BaseCallCenter_T_User"].Expires = _Date;
            }
        }

        /// <summary>
        /// 用户ID,未正常取到值时返回 0
        /// </summary>
        public int UserID
        {
            get
            {
                string _UserId = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return 0;
                _UserId = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_UserId"]);
                return ToInt(_UserId, 0);
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_UserId"] = value.ToString();
            }
        }

        /// <summary>
        /// 用户工号,未正常取到值时返回空字符串
        /// </summary>
        public string UserCode
        {
            get
            {
                string _UserCode = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return string.Empty;
                _UserCode = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_UserCode"]);
                return _UserCode == null ? string.Empty : _UserCode;
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_UserCode"] = value;
            }
        }


        /// <summary>
        /// 用户权限,未正常取到值时返回空字符串
        /// </summary>
        public string SeatRight
        {
            get
            {
                string _SeatRight = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return string.Empty;
                _SeatRight = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_SeatRight"]);
                return _SeatRight == null ? string.Empty : _SeatRight;
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_SeatRight"] = value;
            }
        }

        /// <summary>
        /// 分机号,未正常取到值时返回空字符串
        /// </summary>
        public string ExtNo
        {
            get
            {
                string ExtNo = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return string.Empty;
                ExtNo = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_ExtensionNumber"]);
                return ExtNo == null ? string.Empty : ExtNo;
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_ExtensionNumber"] = value;
            }
        }
        /// <summary>
        /// 用户姓名,未正常取到值时返回空字符串
        /// </summary>
        public string UserName
        {
            get
            {
                string _Name = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return string.Empty;
                _Name = EncryptHelper.DecryptDES(m_Page.Server.UrlDecode(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_UserName"]));
                return _Name == null ? string.Empty : _Name;
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_UserName"] = value;
            }
        }

        /// <summary>
        ///用户部门
        /// </summary>
        public int DeptId
        {
            get
            {
                string _DeptId = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return 0;
                _DeptId = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_DeptId"]);
                return ToInt(_DeptId, 0);
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_DeptId"] = value.ToString();
            }
        }

        /// <summary>
        ///角色名称
        /// </summary>
        public int RoleId
        {
            get
            {
                string _RoleId = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return 0;
                _RoleId = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_RoleID"]);
                return ToInt(_RoleId, 0);
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_RoleID"] = value.ToString();
            }
        }


        /// <summary>
        ///坐席组
        /// </summary>
        public int GroupID
        {
            get
            {
                string _GroupId = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return 0;
                _GroupId = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_GroupId"]);
                return ToInt(_GroupId, 0);
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_GroupId"] = value.ToString();
            }
        }
        /// <summary>
        /// 是否是坐席,未正常取到值时返回空字符串
        /// </summary>
        public string SeatFlag
        {
            get
            {
                string _SeatFlag = string.Empty;
                if (m_Page.Request.Cookies["BaseCallCenter_T_User"] == null)
                    return string.Empty;
                _SeatFlag = EncryptHelper.DecryptDES(m_Page.Request.Cookies["BaseCallCenter_T_User"]["F_SeatFlag"]);
                return _SeatFlag == null ? string.Empty : _SeatFlag;
            }
            set
            {
                m_Page.Response.Cookies["BaseCallCenter_T_User"]["F_SeatFlag"] = value;
            }
        }
        /// <summary>
        /// 字符串转换为数值ss
        /// </summary>
        /// <param name="p_Value">要转换的字符串</param>
        /// <param name="p_DefaultValue">默认值，如果转换失败，返回此值</param>
        /// <returns>转换后的数值</returns>
        private int ToInt(string p_Value, int p_DefaultValue)
        {
            int _Value = 0;
            if (int.TryParse(p_Value, out _Value))
                return _Value;
            else
                return p_DefaultValue;
        }

        protected int my;
    }
}
