using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.customer
{
    public class CustomerFieldController : BaseController
    {
        private readonly BLL.T_Cus_CustomerField customerFieldBLL = new BLL.T_Cus_CustomerField();
        private readonly BLL.T_Form_EntityAttribute entityAttrBLL = new BLL.T_Form_EntityAttribute();


        /// <summary>
        /// 系统字段+自定义字段列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterCustomerField filter)
        {
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "T_Cus_CustomerField",
                "F_Id",
                "*",
                "",
                "ORDER BY F_Sort ",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var fieldList = customerFieldBLL.DataTableToList(dt);

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

            return Content(obj.ToJson());
        }

        //public ActionResult GetModel(int id)
        //{
        //    if (id <= 0)
        //        return Error("参数不正确");
        //    var model = customerFieldBLL.GetModel(id);
        //    if (model != null)
        //        return Error("该条记录不存在");
        //    return Success("", model);
        //}

        public ActionResult CreateOrUpdate(CustomerFieldInput input)
        {
            Model.T_Cus_CustomerField model = new Model.T_Cus_CustomerField();
            Model.T_Form_EntityAttribute attrModel;
            if (input.Id <= 0 && input.AttributeId > 0)
            {
                attrModel = entityAttrBLL.GetModel(input.AttributeId);
                model.F_FieldId = 1;
                model.F_FieldType = attrModel.F_EntityId;
                model.F_Name = attrModel.F_Name;
                model.F_DBFieldName = attrModel.F_DBFieldlName;
                model.F_DBTypeName = attrModel.F_DBTypeName;

                if (customerFieldBLL.Add(model) > 0)
                    return Success("添加成功");
                return Error("添加失败");
            }
            if (input.Id > 0)
            {
                model = customerFieldBLL.GetModel(input.Id);
                if (model != null)
                    return Error("该条记录不存在");
                model.F_Name = string.IsNullOrWhiteSpace(input.Name) ? model.F_Name : input.Name;
                model.F_Sort = input.Sort > 0 ? input.Sort : model.F_Sort;
                model.F_CharLength = input.CharLength > 0 ? input.CharLength : model.F_CharLength;
                model.F_IsNecessary = input.IsNecessary > 0 ? input.IsNecessary : model.F_IsNecessary;
                model.F_IsExportOrInput = input.IsExportOrInput > 0 ? input.IsExportOrInput : model.F_IsExportOrInput;
                model.F_IsShowOnList = input.IsShowOnList > 0 ? input.IsShowOnList : model.F_IsShowOnList;
                model.F_IsShowOnScrean = input.IsShowOnScrean > 0 ? input.IsShowOnScrean : model.F_IsShowOnScrean;
                model.F_Search = input.Search > 0 ? input.Search : model.F_Search;


                if (customerFieldBLL.Update(model))
                    return Success("修改成功");
                return Error("修改失败");
            }
            return Error("参数错误");
        }

        public ActionResult Delete(int id)
        {
            if (customerFieldBLL.Delete(id))
                return Success("删除失败");
            return Error("删除成功");
        }

        public ActionResult GetEntity(FilterExcelTmp filter)
        {
            var sql = "  and F_DBTableName='T_Cus_CustomerExpand' ";

            if (filter.Type > 0)
            {
                sql += " and F_DBTypeName='" + filter.Type + "' ";
            }
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                "T_Form_EntityAttribute",
                "F_AttributeId",
                "*",
                sql,
                "ORDER BY F_AttributeId ",
                filter.PageSize,
                filter.PageIndex,
                true,
                out recordCount);
            var fieldList = entityAttrBLL.DataTableToList(dt);

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

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