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.customer
{
    [Authority]
    public class CusVipController : BaseController
    {
        private BLL.T_Cus_VipInfo vipinfp=new BLL.T_Cus_VipInfo ();
        public ActionResult GetList(string name, string phone
             , string remarks, int state = -1)
        {
            string sql = "and F_IsDelete=0";
            DataTable dt = new DataTable();

        
            //string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            //string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));

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

            if (!string .IsNullOrEmpty (name))
            {
                sql += " and F_Name like '%" + name + "%' ";
            }
            if (!string.IsNullOrEmpty(phone))
            {
                sql += " and F_Phone like '%" + phone + "%' ";
            }
            if (!string.IsNullOrEmpty(remarks))
            {
                sql += " and F_Remarks like '%" + remarks + "%' ";
            }
            if (state>-1)
            {
                sql += " and F_State ="+ state;
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

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

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetVIP()
        {
            int id = Utils.StrToInt(RequestString.GetQueryString("id"), 0);
            if (id  != 0)
            {
                Model.T_Cus_VipInfo vipInfo  = vipinfp.GetModel(id );
                if (vipInfo != null)
                {
                    return Success("获取成功", vipInfo);
                }
                else
                {
                    return Error("获取失败");
                }
            }
            else
            {
                return Error("参数传输失败");
            }

        }
        public ActionResult AddVip(string F_Name,string F_Phone,string F_Remarks)
        {
            
            if (string.IsNullOrEmpty(F_Phone))
                return Error("请输入vip电话");
            Model.T_Cus_VipInfo model = new Model.T_Cus_VipInfo();
            model.F_Name = F_Name;
            model.F_Phone = F_Phone;
            model.F_Remarks = F_Remarks;
            model.F_State = 0;
            model.F_AddUser = User.F_UserCode ;
            model.F_AddTime  = DateTime .Now ;
            model.F_IsDelete = 0;
            int n = vipinfp.Add(model) ;
            if (n > 0)
                return Success("添加成功");
            else
                return Error("添加失败");
        }
        public ActionResult EditVip(string F_Phone,string F_Name,string F_Remarks, int F_ID=0)
        {
            
            
            if (F_ID <= 0)
                return Error("参数错误");
            if (string.IsNullOrEmpty(F_Phone))
                return Error("请输入vip电话");
            Model.T_Cus_VipInfo model = vipinfp.GetModel (F_ID );
            if (model == null)
                return Error("该VIP用户不存在");
            model.F_Name = F_Name;
            model.F_Phone =F_Phone;
            model.F_Remarks =F_Remarks;
            model.F_State = 0;
            model.F_UpdateUser  = User.F_UserCode;
            model.F_UpdateTime  = DateTime.Now;
            model.F_IsDelete = 0;
            bool  n = new BLL.T_Cus_VipInfo().Update (model);
            if (n )
                return Success("修改成功");
            else
                return Error("修改失败");
        }
        public ActionResult DeleteVip(string id)
        {
            
            
            if (string.IsNullOrEmpty(id))
                return Error("请选择vip用户");
            string sql = "update T_Cus_VipInfo set F_IsDelete=1,F_DeleteTime='"+DateTime .Now.ToString ("yyyy-MM-dd HH:mm:ss") + "',F_DeleteUser="+ User.F_UserCode + " where F_ID in(" + id.TrimEnd(',') + ")";
            if (DbHelperSQL.ExecuteSql(sql) > 0)
            {
                return Success("设置成功");
            }
            else
            {
                return Error("设置失败");
            }
        }
        public ActionResult EditState(string id,int state=0)
        {
            
            
            if (string .IsNullOrEmpty (id))
                return Error("请选择vip用户");
            string sql = "update T_Cus_VipInfo set F_State="+ state + " where F_ID in(" + id.TrimEnd(',') + ")";
            if (DbHelperSQL.ExecuteSql(sql) > 0)
            {
                return Success("设置成功");
            }
            else
            {
                return Error("设置失败");
            }
        }
    }
}