using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;
using ZXDT.Common;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class MainController : BaseController
    {


        BLL.T_Sys_RoleInfo roleBLL = new BLL.T_Sys_RoleInfo();
        public ActionResult Index()
        {
            string userid = "0";
            IndexModel model = new IndexModel();
            ZXDT.CallCenter.Model.T_Sys_UserAccount p_LoginUser = new ZXDT.CallCenter.Model.T_Sys_UserAccount();
            try
            {
                model.IsSeat = "false";
                if (Request.Cookies["BaseCallCenter_T_User"] != null)
                {
                    userid = Request.Cookies["BaseCallCenter_T_User"]["F_UserId"];
                }
                int u = Convert.ToInt32(userid);
            }
            catch
            { }
            if (userid != "0")
            {
                p_LoginUser = new BLL.T_Sys_UserAccount().GetModel(Convert.ToInt32(userid));
                if (p_LoginUser != null)
                {
                    if (Request.Cookies["BaseCallCenter_T_User"] != null)
                    {
                        p_LoginUser.F_ExtensionNumber = Request.Cookies["BaseCallCenter_T_User"]["F_ExtensionNumber"];
                    }


                    if (string.IsNullOrEmpty(p_LoginUser.F_HomePhone))
                    {
                        p_LoginUser.F_HomePhone = "/Content/images/face.jpg";

                    }
                    else
                    {
                        if (!System.IO.File.Exists(Server.MapPath(p_LoginUser.F_HomePhone)))
                        {
                            p_LoginUser.F_HomePhone = "/Content/images/face.jpg";
                        }
                    }



                    model.UserAccountModel = p_LoginUser;
                    model.IsSeat = "false";
                    if (p_LoginUser.F_SeatFlag.ToString().ToLower() == "true")
                    {
                        model.IsSeat = "true";
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "$.ligerDialog.waitting('电话服务连接中...');");
                    }
                    if (p_LoginUser.F_HJType == 1)
                    {
                        model.UserAccountModel.F_GroupId = 1001;//呼出组
                    }

                    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))
                    {
                        model.CTIserverIP = CTIserverIP;
                    }

                    try
                    {
                        string CTIserverPort = bll_T_Sys_SystemConfig.GetParamValueByParamCode("AcdServerPort");//CTIIP
                        if (!string.IsNullOrEmpty(CTIserverPort))
                        {
                            model.CTIserverPort = CTIserverPort;
                        }
                    }
                    catch
                    { }


                    try
                    {
                        if (p_LoginUser.F_RoleId > 0)
                        {
                            List<T_Sys_ModuleFunctions> menuList = roleBLL.GetRoleMenu(p_LoginUser.F_RoleId);

                            List<T_Sys_ModuleFunctions> menuModelList = new List<T_Sys_ModuleFunctions>();
                            //获取一级菜单
                            foreach (T_Sys_ModuleFunctions levelModel in menuList.Where(t => t.F_ParentId == 0).ToList())
                            {
                                List<T_Sys_ModuleFunctions> childMenuList = new List<T_Sys_ModuleFunctions>();
                                //获取二级菜单
                                foreach(T_Sys_ModuleFunctions childModel in menuList.Where(t=>t.F_ParentId==levelModel.F_FunctionId).ToList())
                                {
                                    childMenuList.Add(childModel);
                                }
                                levelModel.ChildMenuModelList = childMenuList;
                                if (levelModel.F_Name.Contains("微信客服") || levelModel.F_Name.Contains("微博客服"))
                                {
                                    levelModel.F_OptUrl += F_PId;
                                        }
                                menuModelList.Add(levelModel);
                            }

                            model.MenuModelList = menuModelList;
                        }

                    }
                    catch(Exception ex)
                    {

                    }

                }
            }
            else
            {
                Response.Redirect("/Default/Login");
            }
            return View(model);
        }

        public ActionResult IndexCenter()
        {
            string userid = "0";
            IndexCenterModel model = new IndexCenterModel();
            model.RoleName = "";
            ZXDT.CallCenter.Model.T_Sys_UserAccount p_LoginUser = new ZXDT.CallCenter.Model.T_Sys_UserAccount();
            try
            {
                if (Request.Cookies["BaseCallCenter_T_User"] != null)
                {
                    userid = Request.Cookies["BaseCallCenter_T_User"]["F_UserId"];
                }
                int u = Convert.ToInt32(userid);
            }
            catch
            { }
            if (userid != "0")
            {
                p_LoginUser = new BLL.T_Sys_UserAccount().GetModel(Convert.ToInt32(userid));
                if (p_LoginUser != null)
                {
                    if (Request.Cookies["BaseCallCenter_T_User"] != null)
                    {
                        p_LoginUser.F_ExtensionNumber = Request.Cookies["BaseCallCenter_T_User"]["F_ExtensionNumber"];
                    }
                    model.UserAccountModel = p_LoginUser;
                    try
                    {
                        model.RoleName = new BLL.T_Sys_RoleInfo().GetModel(p_LoginUser.F_RoleId).F_RoleName;
                    }
                    catch
                    { }
                }
            }
            else
            {
                Response.Redirect("/Default/Login");
            }
            return View(model);
        }

        public ActionResult PhoneCall()
        {
            return View();            
        }

        public ActionResult MainTabFrame()
        {
            return View();
        }


        public ActionResult iframeocx()
        {
            return View();
        }

        #region 电话操作日志
        /// <summary>
        /// 获取通话记录
        /// </summary>
        /// <returns></returns>
        [ActionName("InsertCallLogs")]
        [HttpGet]
        public string InsertCallLogs(string serverip, string ctiusercode)
        {
            string res = "error";
            return res;
        }

        #endregion

        #region 显示公告信息
        [ActionName("NoticeData")]
        [HttpGet]
        public string NoticeData(string  timeno)
        {
            string res = "";
            DataTable dt = new DataTable();
            DataTable dtinfo = new DataTable();
            DataTable dtwork = new DataTable();

            try {
                res = "  <ul class=\"layui-timeline\" id=\"notice\">";
                #region 待办工单数
                dtwork = new ZXDT.CallCenter.BLL.T_Wo_WorkOrderBase().GetList(" 1=1 and F_WORKORDERTYPEID=1 and F_WORKORDERSTATEID=1 and F_CREATEBY=" + F_UserID).Tables[0];
                if (dtwork.Rows.Count > 0)
                {
                    //显示所有待办工单
                    //foreach (DataRow drwork in dtwork.Rows)
                    //{
                    //    res = res + " <li class=\"layui-timeline-item\">";
                    //    res = res + " <i class=\"layui-icon layui-timeline-axis\" style=\"color: red; \">&#xe617;</i>";
                    //    res = res + "<div class=\"layui-timeline-content layui-text\">";
                    //    res = res + " <div class=\"layui-timeline-title\" style=\"color:#FF7F24;cursor: pointer;\" onclick=\"view(" + drwork["F_WORKORDERID"].ToString() + ");\" > 待办工单：" + drwork["F_CODE"].ToString() + " </div>";
                    //    res = res + " </div>";
                    //    res = res + " </li>";
                    //}
                    //显示待办工单数量
                    res = res + " <li class=\"layui-timeline-item\">";
                    res = res + " <i class=\"layui-icon layui-timeline-axis\" style=\"color: red; \">&#xe617;</i>";
                    res = res + "<div class=\"layui-timeline-content layui-text\">";
                    res = res + " <div class=\"layui-timeline-title\" style=\"color:#FF7F24;cursor: pointer;\"  > 待办工单：" + dtwork.Rows.Count + "&nbsp;条</div>";
                    res = res + " </div>";
                    res = res + " </li>";
                }
                #endregion
                #region 显示公告
                dt = new ZXDT.CallCenter.BLL.T_Msg_NoticeUsers().GetList("1=1 and F_UserId= " + F_UserID + "order by F_ReadDate desc").Tables[0];
                if (dt.Rows.Count>0) {
                    //foreach (DataRow dr in dt.Rows)
                    //{
                    //   dtinfo = new ZXDT.CallCenter.BLL.T_Msg_NoticeInfo().GetList("1=1 and F_State=1 and  F_NoticeId= " + Convert.ToInt32(dr["F_NoticeId"].ToString())+ "order by  ISNULL(F_ModifyDate,F_CreateOn) desc").Tables[0];

                    List<int> noticelist = (from r in dt.AsEnumerable() select r.Field<int>("F_NoticeId")).ToList<int>();

                   

                    //    string[] names = dt.Rows.OfType<DataRow>().Select(r => r["F_NoticeId"].ToString()).ToArray();


                    dtinfo = new ZXDT.CallCenter.BLL.T_Msg_NoticeInfo().GetList("1=1 and F_State=1 and  F_NoticeId in (  '" + string.Join("','", noticelist) + "')  order by  ISNULL(F_ModifyDate,F_CreateOn) desc").Tables[0];

                    foreach (DataRow drinfo in dtinfo.Rows)
                    {
                        string fabushijian = "";
                        if (drinfo["F_ModifyDate"] != null)
                        {
                            if (drinfo["F_ModifyDate"].ToString() != "")
                            {
                                fabushijian = drinfo["F_ModifyDate"].ToString();
                            }
                            else
                            {
                                fabushijian = drinfo["F_CreateOn"].ToString();
                            }
                        }
                        else
                        {
                            fabushijian = drinfo["F_CreateOn"].ToString();
                        }

                        res = res + " <li class=\"layui-timeline-item\">";
                        res = res + " <i class=\"layui-icon layui-timeline-axis\" style=\"color: yellow; \">&#xe617;</i>";
                        res = res + "<div class=\"layui-timeline-content layui-text\">";
                        res = res + " <div class=\"layui-timeline-title\" style=\"color:#63B8FF;cursor: pointer;\" onclick=\"view(" + drinfo["F_NoticeId"].ToString() + ");\" > " + drinfo["F_Title"].ToString() + "<div style=\"float:right\">" + fabushijian + "  </div> </div>";
                        res = res + " </div>";
                        res = res + " </li>";
                    }
                    //}
                }
                else {
                    res = res + " <li class=\"layui-timeline-item\">";
                    res = res + " <i class=\"layui-icon layui-timeline-axis\" style=\"color: yellow; \">&#xe617;</i>";
                    res = res + "<div class=\"layui-timeline-content layui-text\">";
                    res = res + " <div class=\"layui-timeline-title\">暂无公告</div>";
                    res = res + " </div>";
                    res = res + " </li>";
                }
                #endregion

                res = res + " </ul>";
               
            }
            catch (Exception)
            {
                res = res + " <li class=\"layui-timeline-item\">";
                res = res + " <i class=\"layui-icon layui-timeline-axis\" style=\"color: red; \">&#xe617;</i>";
                res = res + "<div class=\"layui-timeline-content layui-text\">";
                res = res + " <div class=\"layui-timeline-title\">请联系管理员</div>";
                res = res + " </div>";
                res = res + " </li>";
            }
            finally {
                dt.Clear();
                dt.Dispose();
                dtinfo.Clear();
                dtinfo.Dispose();
                dtwork.Clear();
                dtwork.Dispose();
            }

            return res;
        }

        [HttpPost]
        public bool addOPeratorLog(string typeName)
        {
          return   AddAction("",F_UserName,typeName,"","坐席操作记录");
        }
        
        #endregion
    }
}
