using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Common;
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 FunctionController : BaseController
    {
        private BLL.T_Sys_Function functionBLL = new BLL.T_Sys_Function();

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetAllList()
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();

            dt = functionBLL.GetList(0, "1=1", "F_Sort asc").Tables[0];
            return Success("加载成功", dt);

        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();

            int pid = RequestString.GetInt("pid", 0);
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (pid != 0)
            { sql.Append(" and f_parentid=" + pid); }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                               "T_Sys_Function",
                               "F_FunctionId",
                               "*",
                               sql.ToString(),
                               "ORDER BY F_Sort ",
                               pagesize,
                               pageindex,
                               true,
                               out recordCount);
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetFunction()
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();


            int id = RequestString.GetInt("id", 0);
            var model = functionBLL.GetModel(id);
            return Success("加载成功", model);

        }

        /// <summary>
        /// 添加/编辑菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddFunction()
        {
            int id = RequestString.GetInt("id", 0);
            int pid = RequestString.GetInt("pid", 0);
            string name = RequestString.GetFormString("name");
            string code = RequestString.GetFormString("code");
            int state = RequestString.GetInt("state", 0);
            int sort = RequestString.GetInt("sort", 0);
            string url = RequestString.GetFormString("url");
            string img = RequestString.GetFormString("img");
            string remark = RequestString.GetFormString("remark");

            Model.T_Sys_Function orderModel = new Model.T_Sys_Function();
            if (pid == 0) return Error("参数错误");
            if (pid != -1)
            {
                var model = functionBLL.GetModel(pid);
                if (model == null) return Error("父菜单不存在");
            }

            if (id == 0)
            {
                orderModel.F_ParentId = pid;
                orderModel.F_FunctionCode = code;
                orderModel.F_FunctionName = name;
                orderModel.F_OptUrl = url;
                orderModel.F_ImgUrl = img;
                orderModel.F_Sort = sort;
                orderModel.F_Remark = remark;
                orderModel.F_State = state;
                orderModel.F_Layer = 2;
                orderModel.F_flag = 0;
                if (pid < 0)
                {
                    orderModel.F_Layer = 1;
                    orderModel.F_flag = 1;
                }

                if (functionBLL.Add(orderModel) > 0)
                    return Success("菜单添加成功");
                else
                    return Error("菜单添加失败");
            }
            else
            {
                orderModel = functionBLL.GetModel(id);
                if (orderModel != null)
                {
                    orderModel.F_ParentId = pid;
                    orderModel.F_FunctionCode = code;
                    orderModel.F_FunctionName = name;
                    orderModel.F_OptUrl = url;
                    orderModel.F_ImgUrl = img;
                    orderModel.F_Sort = sort;
                    orderModel.F_Remark = remark;
                    orderModel.F_State = state;
                    orderModel.F_Layer = 2;
                    orderModel.F_flag = 0;
                    if (pid < 0)
                    {
                        orderModel.F_Layer = 1;
                        orderModel.F_flag = 1;
                    }

                    if (functionBLL.Update(orderModel))
                        return Success("菜单修改成功");
                    else
                        return Error("菜单修改失败");
                }
                else
                {
                    return Error("菜单修改失败");
                }
            }

        }

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

        }

    }
}
