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

namespace CallCenterApi.Interface.Controllers
{
    //[Authority]
    public class DepartmentController : BaseController
    {
        private BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllList()
        {
            if (Request.IsAuthenticated)
            {
                DataTable dt = new DataTable();
                dt = new BLL.T_Sys_Department().GetList(" F_State=1 ").Tables[0];
                return Success("加载成功", dt);
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetDept(int deptId = 0)
        {
            if (Request.IsAuthenticated)
            {
                BLL.T_Sys_Department dBLL = new BLL.T_Sys_Department();
                Model.T_Sys_Department dModel = dBLL.GetModel(deptId);
                if (dModel != null)
                    return Success("获取部门信息成功", new
                    {
                        deptid = dModel.F_DeptId,
                        depname = dModel.F_DeptName,
                        sort = dModel.F_Sort,
                        parentid = dModel.F_ParentId,
                        parentname = dModel.F_ParentName
                    });
                return Error("获取部门信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddDept(DepartmentInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string pcode = "";
                Model.T_Sys_Department dModel = new Model.T_Sys_Department();
                dModel.F_ParentId = input.PId;
                if (input.PId != 0)
                {
                    Model.T_Sys_Department pdModel = departmentBLL.GetModel(input.PId);
                    if (pdModel != null)
                    {
                        pcode = dModel.F_ParentCode = pdModel.F_DeptCode;
                    }
                }
                dModel.F_Sort = input.Sort;
                dModel.F_DeptName = input.DeptName;
                dModel.F_State = 1;
                int id = departmentBLL.Add(dModel);
                if (id > 0)
                {
                    departmentBLL.UpdateDeptCode(pcode + id.ToString() + "|", id);
                    res = Success("添加成功");
                }
                else
                    res = Success("添加失败");
            }
            return res;
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditDept(DepartmentInput input)
        {
            if (Request.IsAuthenticated)
            {
                if (input.DeptId <= 0)
                    return Error("请选择要编辑的部门");
                BLL.T_Sys_Department dBLL = new BLL.T_Sys_Department();
                Model.T_Sys_Department dModel = dBLL.GetModel(input.DeptId);
                if (dModel == null)
                    return Error("获取信息失败");
                dModel.F_ParentId = input.PId;
                if (input.PId != 0)
                {
                    Model.T_Sys_Department pdModel = departmentBLL.GetModel(input.PId);
                    if (pdModel != null)
                    {
                        dModel.F_ParentCode = pdModel.F_DeptCode;
                        dModel.F_DeptCode = pdModel.F_DeptCode + input.DeptId.ToString() + "|";
                    }
                }
                dModel.F_Sort = input.Sort;
                dModel.F_DeptName = input.DeptName;
                if (dBLL.Update(dModel))
                    return Success("编辑成功");
                return Error("编辑失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDept(string[] ids)
        {
            if (Request.IsAuthenticated)
            {
                if (ids == null || ids.Length <= 0)
                    return Error("请选择要删除的部门");

                var idStr = string.Join(",", ids);
                if (string.IsNullOrEmpty(idStr.Trim()))
                    return Error("请选择要删除的部门");

                if (departmentBLL.DeleteList(idStr))
                    return Success("删除成功");
                return Error("删除失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptList(int pId = 0)
        {
            if (Request.IsAuthenticated)
            {
                DataTable dt = new DataTable();
                dt = new BLL.T_Sys_Department().GetList(" F_State=1 and F_ParentId=" + pId).Tables[0];
                List<Model.TreeModel> modelList = BindTree(dt, "0");

                if (modelList != null)
                {
                    if (modelList.Count > 0)
                        return Success("加载成功", modelList);
                }
                return Error("加载失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptListForReport(int pId = 0, string text = "全部")
        {
            if (Request.IsAuthenticated)
            {
                DataTable dt = new DataTable();
                dt = new BLL.T_Sys_Department().GetList(" F_State=1 and F_ParentId=" + pId).Tables[0];
                List<Model.TreeModel> modelList = BindTree(dt, "0");

                if (modelList != null)
                {
                    if (modelList.Count > 0)
                    {
                        modelList.Insert(0, new Model.TreeModel
                        {
                            id = "-1",
                            text = text
                        });
                        return Success("加载成功", modelList);
                    }
                }
                return Error("加载失败");
            }
            return NoToken("未知错误，请重新登录");
        }


        /// <summary>
        /// tree  树形部门
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="parentid"></param>
        /// <returns></returns>
        private List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {
                List<Model.T_Sys_Department> categorylist = new BLL.T_Sys_Department().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_DeptId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_DeptName;
                    tab2 = new BLL.T_Sys_Department().GetList("F_ParentId=" + currentID + " and F_State=1 order by f_sort").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, currentID);
                    }
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }
    }
}