using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
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();

        #region 部门
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptList()
        {
            DataTable dt = new DataTable();
            dt = departmentBLL.GetList(0, "F_State=0", " F_Sort").Tables[0];
            return Success("加载成功", dt);
        }

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

            if (modelList != null)
            {
                if (modelList.Count > 0)
                    return Success("加载成功", modelList);
            }
            return Error("加载失败");

        }

        /// <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_PartentId=" + currentID + " and F_State=0 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;
            }

        }

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetDept()
        {
            int id = RequestString.GetInt("id", 0);
            Model.T_Sys_Department dModel = departmentBLL.GetModel(id);
            if (dModel != null)
                return Success("获取信息成功", dModel);
            return Error("获取信息失败");

        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddDept()
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(userId);
            int id = RequestString.GetInt("id", 0);
            int did = RequestString.GetInt("did", 0);
            int sort = RequestString.GetInt("sort", 0);
            string name = RequestString.GetFormString("name");
            string phone = RequestString.GetFormString("phone");
            string leader = RequestString.GetFormString("leader");
            string split = RequestString.GetFormString("split");
            string topleader = RequestString.GetFormString("topleader");

            Model.T_Sys_Department dModel = new Model.T_Sys_Department();
            if (id == 0)
            {
                var list = departmentBLL.GetModelList(" F_PartentId='" + did + "' and F_DeptName='" + name + "' ");
                if (list.Count > 0)
                {
                    return Error("已经存在此部门");
                }
                else
                {
                    dModel.F_PartentId = did;
                    dModel.F_Sort = sort;
                    dModel.F_DeptName = name;
                    dModel.F_DeptPhone = phone;
                    dModel.F_layer = did > 0 ? 1 : 0;
                    dModel.F_Type = 0;
                    dModel.F_LeaderUser = leader;
                    dModel.F_TopSplitUser = split;
                    dModel.F_TopLeaderUser = topleader;
                    dModel.F_State = 0;
                    dModel.F_CreateDate = DateTime.Now;
                    dModel.F_CreateUser = userModel.F_UserCode;
                    int n = departmentBLL.Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = departmentBLL.GetModel(id);
                if (dModel != null)
                {
                    var list = departmentBLL.GetModelList(" F_PartentId='" + did + "' and F_DeptName='" + name + "' and F_DeptId!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("已经存在此部门");
                    }
                    else
                    {
                        dModel.F_PartentId = did;
                        dModel.F_Sort = sort;
                        dModel.F_DeptName = name;
                        dModel.F_DeptPhone = phone;
                        dModel.F_LeaderUser = leader;
                        dModel.F_TopSplitUser = split;
                        dModel.F_TopLeaderUser = topleader;
                        if (departmentBLL.Update(dModel))
                            return Success("修改成功");
                        else
                            return Error("修改失败");
                    }
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDept(string[] ids)
        {
            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("删除失败");

        }

        /// <summary>
        /// 删除部门和其下级部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDepts(int id)
        {
            if (id > 0)
            {
                var model = new BLL.T_Sys_Department().GetModel(id);
                if (DelDeptsByPId(id))
                {
                    new BLL.T_Sys_Department().Delete(id);
                }
                return Success("删除成功");
            }
            else
            {
                return Error("删除失败");
            }

        }

        public bool DelDeptsByPId(int id)
        {
            bool bl = true;

            var list = new BLL.T_Sys_Department().GetModelList(" F_PartentId ='" + id + "'");
            foreach (var l in list)
            {
                if (DelDeptsByPId(l.F_DeptId))
                {
                    new BLL.T_Sys_Department().Delete(l.F_DeptId);
                }
            }

            return bl;
        }

        #endregion

    }
}