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.County
{
    public class CountyBranchController : BaseController
    {
        // GET: CountyBranch


        private BLL.T_Branch_List branchBLL = new BLL.T_Branch_List();

        #region 网点

        /// <summary>
        /// 获取网点列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetList(int isdc = 0)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(userId);

            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            string strcode = HttpUtility.UrlDecode(RequestString.GetQueryString("code"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            if (strcode.Trim() != "" && strcode != "undefined")
            {
                sql += " and F_Code like '%" + strcode + "%' ";
            }
            if (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and F_Name like '%" + strname + "%' ";
            }

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            string cols = "*,dbo.GetUserName(F_CreateUser) as UserName";

            if (isdc > 0)
            {
                var dtdc = DbHelperSQL.Query(" select " + cols + " from T_Branch_List where 1=1 " + sql).Tables[0];
                var msg = new NPOIHelper().ExportToExcel("网点列表", dtdc);
                if (msg == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }

            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Branch_List",
                "F_Id",
                cols,
                sql,
                "ORDER BY F_CreateTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取网点列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetBranchList()
        {
            DataTable dt = branchBLL.GetList(" F_IsDelete=0 ").Tables[0];
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取网点
        /// </summary>
        /// <param name="BranchId"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetBranch()
        {

            int id = RequestString.GetInt("id", 0);
            Model.T_Branch_List dModel = branchBLL.GetModel(id);
            if (dModel != null)
                return Success("获取信息成功", dModel);
            return Error("获取信息失败");

        }

        /// <summary>
        /// 添加网点
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Authority]
        public ActionResult AddBranch()
        {
            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);
            string code = RequestString.GetFormString("code");
            string name = RequestString.GetFormString("name");
            string pwd = RequestString.GetFormString("pwd");
            string address = RequestString.GetFormString("address");
            string conname = RequestString.GetFormString("conname");
            string conphone = RequestString.GetFormString("conphone");
            string contel = RequestString.GetFormString("contel");
            string conemail = RequestString.GetFormString("conemail");

            Model.T_Branch_List dModel = new Model.T_Branch_List();
            if (id == 0)
            {
                var list = branchBLL.GetModelList(" F_Name='" + name + "' ");
                if (list.Count > 0)
                {
                    return Error("已经存在此网点");
                }
                else
                {
                    dModel.F_Code = code;
                    dModel.F_Name = name;
                    dModel.F_Password = pwd;
                    dModel.F_Address = address;
                    dModel.F_ConName = conname;
                    dModel.F_ConPhone = conphone;
                    dModel.F_ConTel = contel;
                    dModel.F_ConEmail = conemail;

                    dModel.F_IsDelete = 0;
                    dModel.F_State = 0;
                    dModel.F_CreateUser = userModel.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    int n = branchBLL.Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = branchBLL.GetModel(id);
                if (dModel != null)
                {
                    var list = branchBLL.GetModelList(" F_Name='" + name + "' and F_Id!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("已经存在此网点");
                    }
                    else
                    {
                        dModel.F_Code = code;
                        dModel.F_Name = name;
                        dModel.F_Password = pwd;
                        dModel.F_Address = address;
                        dModel.F_ConName = conname;
                        dModel.F_ConPhone = conphone;
                        dModel.F_ConTel = contel;
                        dModel.F_ConEmail = conemail;

                        if (branchBLL.Update(dModel))
                            return Success("修改成功");
                        else
                            return Error("修改失败");
                    }
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

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

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

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

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

            if (id > 0)
            {
                if (new BLL.T_Branch_List().Delete(id))
                {
                    return Success("删除成功");
                }
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("删除失败");
            }

        }

        #endregion
    }
}