using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Common;
using System.Text;

using System.Web.Services;

namespace HySoft.BaseCallCenter.Web
{
    public partial class _default : System.Web.UI.Page
    {
        private List<Model.T_Sys_ModuleFunctions> moduleFunList = new List<Model.T_Sys_ModuleFunctions>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            if (!this.IsPostBack)
            {
                try
                {
                    //检查用户是否已经登录，没有则跳转到登陆页面
                    LoginUser p_LoginUser = new LoginUser(this.Context);
                    UserID.Value = p_LoginUser.UserID.ToString();
                    if (p_LoginUser.UserID == 0)
                    {
                        HdIswin.Value = "false";
                        Response.Redirect("login.aspx", false);
                    }
                    else
                    {
                        if (p_LoginUser.SeatFlag == "True" || p_LoginUser.SeatFlag == "1")
                        {
                            HdIswin.Value = "true";
                            //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "$.ligerDialog.waitting('电话服务连接中...');");
                        }
                        else
                        {
                            HdIswin.Value = "false";
                        }
                        GetUserInfo();
                        moduleFunList = new BLL.T_Sys_ModuleFunctions().GetModelList(" F_StateFlag=1 and F_FunctionId in (SELECT F_FunctionId FROM T_Sys_RoleFunction where F_RoleId =" + p_LoginUser.RoleId.ToString() + ")");
                        this.HdModuleFunIdCount.Value = moduleFunList.Where(mf => mf.F_ParentId == 0).ToList().Count.ToString();
                    }
                }
                catch (Exception ex)
                {
                    SysLog.WriteLog(ex);
                }
            }
        }

        #region 拼接功能列表

        /// <summary>
        /// 拼接功能列表
        /// </summary>
        /// <returns></returns>
        protected string GetFunTree()
        {
            StringBuilder res = new StringBuilder();
            string roleId = "";
            int i = 1;
            try
            {
                LoginUser p_LoginUser = new LoginUser(this.Context);
                if (p_LoginUser.UserID == 0)
                {
                    HdIswin.Value = "false";
                    Response.Redirect("login.aspx", false);
                }
                else
                {
                    roleId = p_LoginUser.RoleId.ToString();
                    moduleFunList.Where(mf => mf.F_ParentId == 0).OrderBy(mm => mm.F_Sort).ToList().ForEach(mff =>
                    {
                        res.Append("<div title='<span class=\"" + mff.F_ImgUrl + "\"></span>" + mff.F_Name + "' iconcss='" + mff.F_ImgUrl + "'>");
                        res.Append("<ul id='tree" + i + "' style='margin-top: 3px; width: 100%;'>");
                        i++;
                        moduleFunList.Where(ms => ms.F_ParentId == mff.F_FunctionId).OrderBy(mmm => mmm.F_Sort).ToList().ForEach(msf =>
                        {
                            if (msf.F_SysFlag)
                            {
                                res.Append("<li style='width: 100%;' id='" + msf.F_FunctionCode.Trim() + "' url='outsysurl.aspx?outurl=" + HttpUtility.UrlEncode(msf.F_OptUrl.Trim()) + "' text='" + msf.F_Name + "'>");
                            }
                            else
                            {
                                res.Append("<li style='width: 100%;' id='" + msf.F_FunctionCode.Trim() + "' url='" + msf.F_OptUrl + "' text='" + msf.F_Name + "'>");
                            }
                            res.Append("<span>");
                            res.Append(msf.F_Name);
                            res.Append("</span>");
                            GetChildren(moduleFunList, res, msf);
                            res.Append("</li>");
                        });
                        res.Append("</ul></div>");
                    });
                }
            }
            catch (Exception ex)
            {
                //SysLog.WriteLog(new Exception("[" + DateTime.Now + "]系统导航->读取角色系统功能列表,异常：" + ex.Message));
                Response.Redirect("login.aspx");
            }
            return res.ToString();
        }

        public void GetChildren(List<Model.T_Sys_ModuleFunctions> modelFunList, StringBuilder sb, Model.T_Sys_ModuleFunctions modelFun)
        {
            try
            {
                if (modelFunList.Exists(m => m.F_ParentId == modelFun.F_FunctionId))
                {
                    sb.Append("<ul id='tree" + modelFun.F_FunctionId + "' style='margin-top: 3px; width: 100%;'>");
                    modelFunList.Where(ms => ms.F_ParentId == modelFun.F_FunctionId).OrderBy(mm => mm.F_Sort).ToList().ForEach(msf =>
                    {
                        sb.Append("<li style='width: 100%;' id='" + msf.F_FunctionCode.Trim() + "' url='" + msf.F_OptUrl + "' text='" + msf.F_Name + "'>");
                        sb.Append("<span>");
                        sb.Append(msf.F_Name);
                        sb.Append("</span>");
                        GetChildren(modelFunList, sb, msf);
                        sb.Append("</li>");
                    });
                    sb.Append("</ul>");
                }
            }
            catch (Exception ex)
            {
                //SysLog.WriteLog(new Exception("[" + DateTime.Now + "]系统导航->循环功能子菜单,异常：" + ex.Message));
                Response.Redirect("login.aspx");
            }
        }
        #endregion

        #region 获取登陆坐席的信息
        /// <summary>
        /// 获取登陆坐席的信息
        /// </summary>
        private void GetUserInfo()
        {
            try
            {
                LoginUser p_User = new LoginUser(this.Context);
                if (p_User != null)
                {
                    Model.T_Sys_UserAccount p_LoginUser = new BLL.T_Sys_UserAccount().GetModel(p_User.UserID);
                    if (p_LoginUser != null)
                    {
                        this.lblAgentCode.Text = p_LoginUser.F_UserCode;//用户工号
                        this.lblAgentName.Text = p_LoginUser.F_UserName;//用户姓名
                        txtAgentName.Value = p_LoginUser.F_UserName;
                        this.lblAgentExt.Text = p_User.ExtNo;//分机号
                        hfUserId.Value = p_LoginUser.F_UserId.ToString();
                        //--------------------------------------------
                        txtLabourNo.Value = p_LoginUser.F_WorkNumber.ToString();
                        txtPhoneID.Value = p_LoginUser.F_UserCode.ToString();
                        this.txtPhoneNum.Value = p_User.ExtNo;
                        txtLabourwork.Value = p_LoginUser.F_UserCode;
                        txtIsAgent.Value = p_User.SeatFlag.ToString().ToLower();
                        txtAgentRight.Value = p_User.SeatRight;
                        txtAgentId.Value = p_LoginUser.F_UserId.ToString();
                        SeatType.Value = p_User.SeatRight;
                        //2017-02-14 clq 临时修改
                        txtAgentGroup.Value = p_User.GroupID.ToString();
                        //if (p_LoginUser.F_HJType == 1)
                        //{
                        //    txtAgentGroup.Value = "2001";//呼出组
                        //}
                        //else
                        //{
                        //    txtAgentGroup.Value = "1001";//呼入组
                        //}
                        string pasword = p_LoginUser.F_Password;
                        //if (p_User.ExtNo.Trim() == "")
                        //{
                        //    HdIswin.Value = "false";
                        //}
                        if (!string.IsNullOrEmpty(pasword))
                        {
                            pwd.Value = pasword;
                            passwd.Value = pasword;
                        }
                        BLL.T_Sys_SystemConfig bll_T_Sys_SystemConfig = new BLL.T_Sys_SystemConfig();
                        string CTIserverIP = bll_T_Sys_SystemConfig.GetParamValueByParamCode("AcdServerIp");//CTIIP
                        if (!string.IsNullOrEmpty(CTIserverIP))
                        {
                            this.txtMostlyAddress.Value = CTIserverIP;
                            this.txtSubordinationAddress.Value = CTIserverIP;
                        }

                        try
                        {
                            string CTIserverPort = bll_T_Sys_SystemConfig.GetParamValueByParamCode("AcdServerPort");//CTIIP
                            if (!string.IsNullOrEmpty(CTIserverPort))
                            {
                                this.txtServerPort.Value = CTIserverPort;
                            }
                        }
                        catch
                        { }
                    }
                }
            }
            catch (Exception ex)
            {
                SysLog.WriteLog(ex);
            }
        }

        #endregion

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            try
            {
                CookieUtil.RemoveAllCookies();
            }
            catch { }
            Response.Redirect("login.aspx");
        }


        [WebMethod]
        public static string Get(string id)
        {
            string panduan = "0";
            try
            {
              System.Data.DataTable dt = DBUtility.DbHelperSQL.Query("select  * from V_WorkOrderBase where   F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where F_LOSEFLAG=0 and F_OWNERID="+id+") and F_WORKORDERSTATEID!=0").Tables[0];
              panduan = dt.Rows.Count.ToString();
            }
            catch { return panduan; }
            return panduan;
        }
    }
}