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.visit
{
    public class VisitTelphoneMarkController : BaseController
    {
        BLL.T_Vis_Telphone_Mark bll = new BLL.T_Vis_Telphone_Mark();

        /// <summary>
        /// 获取外呼标记（无效号码）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            string cusPhone = HttpUtility.UrlDecode(RequestString.GetQueryString("cusPhone"));
            string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("strstarttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("strendtime"));
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            string sql = " and F_IsDelete=0 ";
            if (cusPhone.Trim() != "")
            {
                sql += " and F_CusPhone like '%" + cusPhone + "%'";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateDate,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateDate,'" + strendtime + "')>=0   ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                    "T_Vis_Telphone_Mark",
                    "F_Id",
                    "*",
                    sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取一条详细信息 Id
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public ActionResult GetInfo(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return Error("获取参数失败");
            }
            Model.T_Vis_Telphone_Mark model = bll.GetModel(int.Parse(id.Trim()));
            if (model != null)
            {
                return Success("获取成功", model);
            }
            else
            {
                return Error("获取失败");
            };
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <returns></returns>
        
        public ActionResult SaveInfo()
        {
            int id = RequestString.GetInt("id", 0);
            string cusName = RequestString.GetFormString("cusName");
            string cusTelphone = RequestString.GetFormString("cusTelphone");
            string cusPhone = RequestString.GetFormString("cusPhone");
            string note = RequestString.GetFormString("note");

            Model.T_Vis_Telphone_Mark model = new Model.T_Vis_Telphone_Mark();
            if (id == 0)
            {
                model.F_CusName = cusName;
                model.F_CusTelphone = cusTelphone;
                model.F_CusPhone = cusPhone;
                model.F_Note = note;
                int n = bll.Add(model);
                if (n > 0)
                {
                    return Success("添加成功", n);
                }
                else
                {
                    return Error("添加失败");
                }
            }
            else
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    model.F_ID = id;
                    model.F_CusName = cusName;
                    model.F_CusTelphone = cusTelphone;
                    model.F_CusPhone = cusPhone;
                    model.F_Note = note;
                    if (bll.Update(model))
                    {
                        return Success("修改成功");
                    }
                    else
                    {
                        return Error("修改失败");
                    }
                }
                return Error("信息不存在");
            }
        }

    }
}