using CallCenter.Utility;
using CallCenterApi.Common;
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
{
    public class AreaController : BaseController
    {
        private BLL.T_Sys_Area areaBLL = new BLL.T_Sys_Area();

        #region 区域
        /// <summary>
        /// 获取区域列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetAreaList()
        {
            DataTable dt = areaBLL.GetList(0, "", " F_Sort").Tables[0];
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取区域列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetAreaListById(int id = 0)
        {
            DataTable dt = areaBLL.GetList(" isnull(F_PrentId,0)='" + id + "' and F_State=0 ").Tables[0];
            return Success("列表加载成功", dt);
        }

        /// <summary>
        /// 获取区域
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetArea()
        {
           
                int id = RequestString.GetInt("id", 0);
                Model.T_Sys_Area dModel = areaBLL.GetModel(id);
                if (dModel != null)
                    return Success("获取信息成功", dModel);
                return Error("获取信息失败");
       
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Authority]
        public ActionResult AddArea()
        {
            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 pid = RequestString.GetInt("pid", 0);
            int sort = RequestString.GetInt("sort", 0);
            string name = RequestString.GetFormString("name");

            Model.T_Sys_Area dModel = new Model.T_Sys_Area();
            if (id == 0)
            {
                var list = areaBLL.GetModelList(" F_AreaId='" + id + "' and F_AreaName='" + name + "' ");
                if (list.Count > 0)
                {
                    return Error("已经存在此部门");
                }
                else
                {
                    dModel.F_PrentId = pid;
                    dModel.F_Sort = sort;
                    dModel.F_AreaName = name;
                    dModel.F_State = 0;
                    dModel.F_CreateUser = userModel.F_UserCode;
                    dModel.F_CreateDate = DateTime.Now;
                    int n = areaBLL.Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = areaBLL.GetModel(id);
                if (dModel != null)
                {
                    var list = areaBLL.GetModelList(" F_AreaId='" + id + "' and F_AreaName='" + name + "' and F_AreaId!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("已经存在此部门");
                    }
                    else
                    {
                        dModel.F_PrentId = pid;
                        dModel.F_Sort = sort;
                        dModel.F_AreaName = name;
                        dModel.F_State = 0;
                        if (areaBLL.Update(dModel))
                            return Success("修改成功");
                        else
                            return Error("修改失败");
                    }
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

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

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

            if (areaBLL.DeleteList(idStr))
                return Success("删除成功");
            return Error("删除失败");
        }

        /// <summary>
        /// 删除部门和其下级部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult DelAreas(int id)
        {

            if (id > 0)
            {
                var model = new BLL.T_Sys_Area().GetModel(id);
                if (DelAreasByPId(id))
                {
                    new BLL.T_Sys_Area().Delete(id);
                }
                return Success("删除成功");
            }
            else
            {
                return Error("删除失败");
            }

        }

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

            var list = new BLL.T_Sys_Area().GetModelList(" F_PrentId ='" + id + "'");
            foreach (var l in list)
            {
                if (DelAreasByPId(l.F_AreaId))
                {
                    new BLL.T_Sys_Area().Delete(l.F_AreaId);
                }
            }

            return bl;
        }
        #endregion
    }
}