using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Common;
using CallCenterApi.Interface.Models.Dto;
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
{
    [Authorize]
    public class ModuleInfoController : BaseController
    {
        private BLL.T_Sys_ModuleInfo moduleInfoBLL = new BLL.T_Sys_ModuleInfo();


        /// <summary>
        /// 返回树形下拉框 菜单数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetTreeList()
        {
            var ds_ModuleInfo = moduleInfoBLL.GetAllList();//DataSet
            List<Model.T_Sys_ModuleInfo> list_ModuleInfo = moduleInfoBLL.DataTableToList(ds_ModuleInfo.Tables[0]);
            list_ModuleInfo.Sort((x, y) => x.F_Sort - y.F_Sort);//排序
            var treeList = new List<TreeModel>();
            foreach (var item in list_ModuleInfo)
            {
                TreeModel treeModel = new TreeModel();
                treeModel.id = item.F_ModuleId.ToString();
                treeModel.IconCls = item.F_ImgUrl;
                treeModel.text = item.F_ModuleName;
                treeModel.parentid = item.F_ParentID.ToString();
                treeList.Add(treeModel);
            }
            //return Content(treeList.ToJson());
            return Success("获取菜单树成功", treeList.TreeRecursion("0"));
        }

        /// <summary>
        /// 依据关键字获取相关菜单数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetAllGrid(string keyword)
        {
            //TODO:计划使用缓存
            var ds_ModuleInfo = moduleInfoBLL.GetAllList();//DataSet  不在此处添加搜素条件，是因为计划改为缓存
            List<Model.T_Sys_ModuleInfo> list_ModuleInfo = moduleInfoBLL.DataTableToList(ds_ModuleInfo.Tables[0]);
            if (!string.IsNullOrEmpty(keyword))
            {
                list_ModuleInfo = list_ModuleInfo.TreeWhere(t => t.F_ModuleName.Contains(keyword), "F_ModuleId", "F_ParentID").ToList();
            }
            list_ModuleInfo.Sort((x, y) => x.F_Sort - y.F_Sort);//排序
            return Success("获取菜单", list_ModuleInfo);

        }

        /// <summary>
        /// 获取菜单实体
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetModule(int mId = 0)
        {
            var entity_ModuleInfo = moduleInfoBLL.GetModel(mId);
            if (entity_ModuleInfo != null)
            {
                return Success("加载菜单成功", new
                {
                    name = entity_ModuleInfo.F_ModuleName,
                    code = entity_ModuleInfo.F_ModuleCode,
                    url = entity_ModuleInfo.F_OptUrl,
                    enable = entity_ModuleInfo.F_StateFlag,
                    remark = entity_ModuleInfo.F_Remark,
                    sort = entity_ModuleInfo.F_Sort
                });
            }
            else
            {
                return Error("加载菜单失败");
            }
        }


        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddModule(ModuleInfoDto input)
        {
            var entity_ModuleInfo = new Model.T_Sys_ModuleInfo();
            entity_ModuleInfo.F_ParentID = input.parentid;
            entity_ModuleInfo.F_ModuleName = input.name;
            entity_ModuleInfo.F_ModuleCode = input.code;
            entity_ModuleInfo.F_Remark = input.remark;
            entity_ModuleInfo.F_Sort = input.sort;
            entity_ModuleInfo.F_StateFlag = Convert.ToInt32(input.flag);
            entity_ModuleInfo.F_OptUrl = input.url;
            entity_ModuleInfo.F_CreateTime = DateTime.Now;
            entity_ModuleInfo.F_CreateUID = CurrentUser.UserData.F_UserId;

            if (moduleInfoBLL.Add(entity_ModuleInfo) > 0)
            {
                return Success("菜单添加成功");
            }
            else
            {
                return Error("菜单添加失败,请重试！");
            }

        }

        /// <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_ModuleInfo().DeleteList(idd.TrimEnd(',')))
                {
                    return Success("删除成功");
                }
                else
                {
                    return Error("删除失败,请重试！");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditModule(ModuleInfoDto input)
        {
            var entity_ModuleInfo = moduleInfoBLL.GetModel(input.id);
            if (entity_ModuleInfo != null)
            {
                entity_ModuleInfo.F_ParentID = input.parentid;
                entity_ModuleInfo.F_ModuleName = input.name;
                entity_ModuleInfo.F_ModuleCode = input.code;
                entity_ModuleInfo.F_Remark = input.remark;
                entity_ModuleInfo.F_Sort = input.sort;
                entity_ModuleInfo.F_StateFlag = Convert.ToInt32(input.flag);
                entity_ModuleInfo.F_OptUrl = input.url;


                if (moduleInfoBLL.Update(entity_ModuleInfo))
                {
                    return Success("菜单修改成功");
                }
                else
                {
                    return Error("菜单修改失败,请重试！");
                }
            }
            else
            {
                return Error("菜单对象获取失败");
            }
        }

        /// <summary>
        /// 获取菜单列表分页数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetList(FilterCategory filter)
        {
            var sql = "";
            var key = WebHelper.Formatstr(filter.Name ?? "");
            if (key != "")
            {
                sql += " and F_ModuleName like '%" + key + "%' ";
            }

            int recordCount = 0;
            Model.PageData<Model.T_Sys_ModuleInfo> pageModel = new Model.PageData<Model.T_Sys_ModuleInfo>();

            var dt = BLL.PagerBLL.GetListPager(
                "T_Sys_ModuleInfo",
                "F_ModuleId",
                "*",
                sql,
                "ORDER BY F_Sort desc",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }
    }
}
