using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Common;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    //[Authority]
    public class ModuleController : BaseController
    {
        private BLL.T_Sys_ModuleFunctions moduleFunctionBLL = new BLL.T_Sys_ModuleFunctions();

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterModuleFunction filter)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();

                if (filter.ModuleFunctionId != 0)
                { sql.Append(" and f_parentid=" + filter.ModuleFunctionId); }

                //加载列表数据
                sql.Append(" and f_stateflag=1 and f_type=0 ");
                var recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                                   "T_Sys_ModuleFunctions",
                                   "F_FunctionId",
                                   "F_FunctionId,(select f_name from T_Sys_ModuleFunctions as b where b.F_FunctionId = T_Sys_ModuleFunctions.F_ParentId) as F_ParentName,F_Name,F_Sort,F_StateFlag,F_Controller,F_Action",
                                   sql.ToString(),
                                   "ORDER BY F_Sort desc ",
                                   filter.PageSize,
                                   filter.PageIndex,
                                   true,
                                   out recordCount);
                if (dt.Rows.Count > 0)
                    res = Success("加载成功", BindTree(dt, "0"));
                else
                    res = Error("加载失败");
            

                dt.Clear();
                dt.Dispose();
            
            return res;
        }

        /// <summary>
        /// 获取搜索条件数据源
        /// </summary>
        /// <returns></returns>
        public ActionResult GetFilter()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
     
                var filterModuleList = moduleFunctionBLL.DataTableToList(moduleFunctionBLL.GetList(" f_stateflag=1  and f_type=1 order by F_FunctionId").Tables[0]);
                if (filterModuleList.Count > 0)
                {
                    res = Success("加载成功", filterModuleList.Select(x => new
                    {
                        Value = x.F_FunctionId,
                        Text = x.F_Name
                    }));
                }
                else
                    res = Error("加载失败");

            return res;
        }

        /// <summary>
        /// 获取菜单实体
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        public ActionResult GetModule(int mId = 0)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
  
                if (mId <= 0) return Error("参数错误");
                var filterModuleList = moduleFunctionBLL.DataTableToList(moduleFunctionBLL.GetList(" f_stateflag=1 and f_type=1  order by F_FunctionId").Tables[0]);
                Model.T_Sys_ModuleFunctions baseModel = new BLL.T_Sys_ModuleFunctions().GetModel(mId);
                if (baseModel != null && filterModuleList.Count > 0)
                {
                    res = Success("加载菜单成功", new
                    {
                        model = new
                        {
                            name = baseModel.F_Name,
                            code = baseModel.F_FunctionCode,
                            controller = baseModel.F_Controller,
                            action = baseModel.F_Action,
                            enable = baseModel.F_StateFlag,
                            remark = baseModel.F_Remark,
                            sort = baseModel.F_Sort,
                            type = baseModel.F_Type
                        },
                        modellist = filterModuleList.Select(x => new
                        {
                            Value = x.F_FunctionId,
                            Text = x.F_Name
                        })
                    });
                }
                else
                    res = Error("加载菜单失败");

            return res;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpPost]
        //public ActionResult AddModule(ModuleFunctionInput input)
        //{
        //    ActionResult res = NoToken("未知错误，请重新登录");
   
        //        BLL.T_Sys_ModuleFunctions orderBll = new BLL.T_Sys_ModuleFunctions();
        //        Model.T_Sys_ModuleFunctions orderModel = new Model.T_Sys_ModuleFunctions();
        //        if (input.ParentId <= 0) return Error("参数错误");
        //        var model = orderBll.GetModel(input.ParentId);
        //        if (model == null) return Error("父节点不存在");
        //        orderModel.F_ParentId = model.F_FunctionId;
        //        orderModel.F_ParentCode = model.F_ParentCode;
        //        orderModel.F_Name = input.Name;
        //        orderModel.F_FunctionCode = input.Code;
        //        orderModel.F_Controller = input.Controller;
        //        orderModel.F_Action = input.Action;
        //        orderModel.F_OptUrl = input.Url;
        //        orderModel.F_Remark = input.Remark;
        //        orderModel.F_Sort = input.Sort;
        //        orderModel.F_StateFlag = input.Flag ? 1 : 0;
        //        orderModel.F_Type = input.Type;
        //        if (orderBll.Add(orderModel) > 0)
        //            res = Success("菜单添加成功");
        //        else
        //            res = Error("菜单添加失败");


        //    return res;
        //}

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpPost]
        //public ActionResult EditModule(ModuleFunctionInput input)
        //{
        //    ActionResult res = NoToken("未知错误，请重新登录");
       
        //        BLL.T_Sys_ModuleFunctions orderBll = new BLL.T_Sys_ModuleFunctions();
        //        Model.T_Sys_ModuleFunctions orderModel = orderBll.GetModel(input.ParentId);
        //        if (orderModel != null)
        //        {
        //            orderModel.F_Name = input.Name;
        //            //orderModel.F_Controller = input.Controller;
        //            //orderModel.F_Action = input.Action;
        //            orderModel.F_OptUrl = input.Url;
        //            orderModel.F_Remark = input.Remark;
        //            orderModel.F_Sort = input.Sort;
        //            orderModel.F_StateFlag = input.Flag ? 1 : 0;
        //            orderModel.F_Type = input.Type;
        //            if (orderBll.Update(orderModel))
        //            {
        //                res = Success("菜单修改成功");
        //            }
        //            else
        //            {
        //                res = Error("菜单修改失败");
        //            }
        //        }
        //        else
        //        {
        //            res = Error("菜单对象获取失败");
        //        }

        //    return res;
        //}

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelModule(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                if (new BLL.T_Sys_ModuleFunctions().DeleteList(idd.TrimEnd(',')))
                {
                    return Success("删除成功");
                }
                else
                {
                    return Error("删除失败,请重试！");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }



        #region 可能不需要
        //获取菜单树形列表
        public ActionResult GetTreeList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dt = new DataTable();
         
                dt = new BLL.T_Sys_ModuleFunctions().GetList("F_ParentId=0 and F_StateFlag=1 order by F_Sort").Tables[0];
                List<Model.TreeModel> modelList = BindTree(dt, "0");
                if (modelList.Count > 0)
                {
                    res = Success("加载成功", modelList);
                }
                else
                    res = Error("加载失败");

            return res;
        }
        /// <summary>
        /// 递归获取树形结构
        /// </summary>
        /// <param name="tab">数据源</param>
        /// <param name="parentid">父节点ID</param>
        /// <returns></returns>
        public List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {
                List<Model.T_Sys_ModuleFunctions> categorylist = new BLL.T_Sys_ModuleFunctions().DataTableToList(tab);
                List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_FunctionId.ToString();//当前功能ID
                    if (currentID == "224")
                    {

                    }
                    model.id = currentID;
                    model.IconCls = "1";//图标
                    model.text = categorylist[i].F_Name;
                    tab2 = new BLL.T_Sys_ModuleFunctions().GetList("F_ParentId=" + currentID + " and F_StateFlag=1  order by F_Sort").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, "0");
                    }
                    modelList.Add(model);
                }
                return modelList;
            }
            else
            {
                return null;
            }
        }
        #endregion

    }
}
