using CallCenter.Utility;
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
{
    /// <summary>
    /// 地址 - 省市县乡
    /// </summary>
    public class AddressManageController : BaseController
    {
        private BLL.T_Sys_Address bll = new BLL.T_Sys_Address();

        /// <summary>
        /// 获取地址信息(省市县乡)
        /// </summary>
        /// <param name="parentcode">默认是空（省）</param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetAddressList(string parentcode = "")
        {
            string sql = "";
             sql += " and ISNULL(Parent_Code,'') = '" + parentcode.Trim() + "' ";
            DataTable dt = bll.GetList(0, $" 1=1 {sql}", " Sort").Tables[0];
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取地址名称和编号
        /// </summary>
        /// <param name="codes">编码组合（例如：111|222|333|444）</param>
        /// <returns></returns>
        public ActionResult GetAddressNamesByCodes(string codes = "")
        {
            // 省 市 县 乡镇 名称
            // 省 市 县 乡镇 编码
            string address_Name = "";
            string address_code = "";
            var list = bll.DataTableToList(bll.GetAllList().Tables[0]);
            if (!string.IsNullOrEmpty(codes))
            {
                for (int i = 0; i < codes.Length; i++)
                {
                    if (!string.IsNullOrEmpty(codes[i].ToString()))
                    {
                        var model = list.FirstOrDefault(x => x.Code == codes[i].ToString());
                        if (model != null)
                        {
                            address_Name += model.Code + "|";
                            address_code += model.Name + " ";
                        }
                    }
                }
            }
            var obj = new
            {
                state = "success",
                message = "成功",
                names = address_Name,
                codes = address_code
            };
            return Content(obj.ToJson());
        }
    }
}