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.workorder
{
    [Authority]
    public class DemandsTypeController : BaseController
    {
        private BLL.T_Bus_DemandsType demandstypeBLL = new BLL.T_Bus_DemandsType();
        private BLL.T_Sys_UserAccount useraccountBLL = new BLL.T_Sys_UserAccount();
        #region 分类
        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptList(int pid=0)
        {
            var dt = demandstypeBLL.GetList(0, "F_IsDelete=0 and F_ParentId=" + pid, " F_Sort").Tables[0];
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptListtree(int pid = 0)
        {
            DataTable dt = new DataTable();
            dt = demandstypeBLL.GetList(" F_IsDelete=0 and F_ParentId=" + pid).Tables[0];
            List<Model.TreeModel> modelList = BindTree(dt, "0");

            if (modelList == null)
                return Error("加载失败");
            if (modelList.Count > 0)
                return Success("加载成功", modelList);
            else
                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_Bus_DemandsType> categorylist = demandstypeBLL.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_Id.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_Name;
                    tab2 = demandstypeBLL.GetList("F_ParentId=" + currentID + " and F_IsDelete=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 GetModel(int id = 0)
        {
            if (id <= 0)
                return Error("参数错误");
            Model.T_Bus_DemandsType dModel = demandstypeBLL.GetModel(id);
            if (dModel != null)
                return Success("获取信息成功", dModel);
            else
                return Error("获取信息失败");
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveDemands(int id, string name,string remark, int sort=0, int pid = 0)
        {
            #region 验证
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            if (userId <= 0)
                return Error("权限不足");
            Model.T_Sys_UserAccount userModel = useraccountBLL.GetModel(userId);
            if (userModel == null)
                return Error("权限不足!");
            //获取是否有重复
            var list = demandstypeBLL.GetModelList(" F_ParentId='" + pid + "' and F_Name='" + name + "' and F_Id="+ id + " and F_IsDelete=0 ");
            if (list.Count > 0)
            {
                return Error("此目录下已经存在“"+ name + "”分类");
            }
            #endregion
            var layer = 0;var pcode = "|0|";
            #region 处理父级
            Model.T_Bus_DemandsType pModel = new Model.T_Bus_DemandsType();
            if (pid > 0)
            {
                pModel = demandstypeBLL.GetModel(pid);
                if(pModel!=null)
                {
                    layer = pModel.F_layer.Value + 1;
                    pcode = pModel.F_ParentCode;
                }
            }
            #endregion

            Model.T_Bus_DemandsType dModel = new Model.T_Bus_DemandsType();
            if (id == 0)
            {
                #region 添加
                dModel.F_Name = name;
                dModel.F_ParentId = pid;
                dModel.F_ParentCode = pcode;
                dModel.F_layer = layer;
                dModel.F_Sort = sort;
                dModel.F_Remark = remark;
                dModel.F_CreateBy = userModel.F_UserCode;
                dModel.F_CreateOn = DateTime.Now;
                dModel.F_IsDelete = 0;
                var resid = demandstypeBLL.Add(dModel);
                if(resid>0)
                {
                    dModel.F_Code = pcode + resid.ToString() + "|";
                    dModel.F_Id = resid;
                    demandstypeBLL.Update(dModel);
                    return Success("添加成功");
                }
                else
                    return Error("添加失败");
                #endregion
            }
            else
            {
                #region 修改
                dModel = demandstypeBLL.GetModel(id);
                if (dModel == null)
                    return Error("获取信息失败");

                dModel.F_Name = name;
                dModel.F_ParentId = pid;
                dModel.F_ParentCode = pcode;
                dModel.F_Code = pcode + id.ToString() + "|";
                dModel.F_layer = layer;
                dModel.F_Sort = sort;
                dModel.F_Remark = remark;
                var res= demandstypeBLL.Update(dModel); 
                if(res)
                    return Success("保存成功");
                else
                    return Error("保存失败");
                #endregion
            }
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDept(string[] ids)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            if (userId <= 0)
                return Error("权限不足");
            Model.T_Sys_UserAccount userModel = useraccountBLL.GetModel(userId);
            if (userModel == null)
                return Error("权限不足!");

            if (ids == null || ids.Length <= 0)
                return Error("请选择要删除的内容");

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

            if (demandstypeBLL.DeleteList(idStr, userModel.F_UserCode))
                return Success("删除成功");
            else
                return Error("删除失败");
        }

        /// <summary>
        /// 删除五级分类及下属分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDepts(int id)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            if (userId <= 0)
                return Error("权限不足");
            Model.T_Sys_UserAccount userModel = useraccountBLL.GetModel(userId);
            if (userModel == null)
                return Error("权限不足!");
            if (id <= 0)
                return Error("参数错误");

            var model = demandstypeBLL.GetModel(id);
            if (model == null)
                return Error("获取参数信息错误");
            var res = true;
            if (!string.IsNullOrEmpty(model.F_Code))
            {
                var clist = demandstypeBLL.GetModelList(" F_IsDelete=0 and F_Code like '" + model.F_Code + "%'");
                foreach (var item in clist)
                {
                    res &= demandstypeBLL.Delete(item.F_Id, userModel.F_UserCode);
                }
            }
            if (res)
            {
                if (demandstypeBLL.Delete(id, userModel.F_UserCode))
                    return Success("删除成功");
                else
                    return Error("删除失败");
            }
            return Error("子类删除失败");

        }
        #endregion
    }
}