using CallCenter.Utility;
using CallCenterApi.Common;
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.customer
{
    public class RegionCategoryController : BaseController
    {
        private BLL.T_RegionCategory regionBLL = new BLL.T_RegionCategory();

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

        /// <summary>
        /// 获取地市列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
                DataTable dt = new DataTable();
                string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;

                string sql = "";
                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

                if (strpagesize.Trim() != "")
                {
                    pagesize = Convert.ToInt32(strpagesize);
                }
                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "T_RegionCategory",
                    "F_RegionId",
                    "*",
                    sql,
                    "ORDER BY F_RegionId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                var obj = new
                {
                    rows = dt,
                    total = recordCount
                };

                res = Content(obj.ToJson());
            return res;
        }

        /// <summary>
        /// 获取地市
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetRegion(int deptId = 0)
        {
            if (Request.IsAuthenticated)
            {
                BLL.T_RegionCategory dBLL = new BLL.T_RegionCategory();
                Model.T_RegionCategory dModel = dBLL.GetModel(deptId);
                if (dModel != null)
                    return Success("获取地市信息成功", dModel);
                return Error("获取地市信息失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 添加地市
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddRegion(RegionCategoryInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_RegionCategory dModel = new Model.T_RegionCategory();
                dModel.F_ParentId = input.F_ParentId;
                dModel.F_Sort = input.F_Sort;
                dModel.F_RegionName = input.F_RegionName;

                dModel.F_DeleteFlag = 0;
                dModel.F_CreateBy = CurrentUser.UserData.F_UserId;
                dModel.F_CreateOn = DateTime.Now;
                if (regionBLL.Add(dModel) > 0)
                    res = Success("添加成功");
                else
                    res = Success("添加失败");
            }
            return res;
        }

        /// <summary>
        /// 编辑地市
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditRegion(RegionCategoryInput input)
        {
            if (Request.IsAuthenticated)
            {
                if (input.F_RegionId <= 0)
                    return Error("请选择要编辑的地市");
                BLL.T_RegionCategory dBLL = new BLL.T_RegionCategory();
                Model.T_RegionCategory dModel = dBLL.GetModel(input.F_RegionId);
                if (dModel == null)
                    return Error("获取信息失败");
                dModel.F_ParentId = input.F_ParentId;
                dModel.F_Sort = input.F_Sort;
                dModel.F_RegionName = input.F_RegionName;

                dModel.F_ModifyBy = CurrentUser.UserData.F_UserId;
                dModel.F_ModifyOn = DateTime.Now;
                if (dBLL.Update(dModel))
                    return Success("编辑成功");
                return Error("编辑失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 删除地市
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelRegion(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 (regionBLL.DeleteList(idStr))
                    return Success("删除成功");
                return Error("删除失败");
            }
            return NoToken("未知错误，请重新登录");
        }

        /// <summary>
        /// 获取地市列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRegionList(int pId = 0)
        {
            if (Request.IsAuthenticated)
            {
                DataTable dt = new DataTable();
                dt = new BLL.T_RegionCategory().GetList(" F_DeleteFlag=0 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>
        /// 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_RegionCategory> categorylist = new BLL.T_RegionCategory().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_RegionId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_RegionName;
                    tab2 = new BLL.T_RegionCategory().GetList("F_ParentId=" + currentID + " and F_DeleteFlag=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;
            }

        }
    }
}