using CallCenter.Utility;
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.Client
{
    [Authority]
    public class UserFeedbackController : BaseController
    {
        BLL.T_Client_UserFeedback bll = new BLL.T_Client_UserFeedback();
        /// <summary>
        /// 获取所有库列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllList()
        {
            DataTable dt = new DataTable();
            string mobile = HttpUtility.UrlDecode(RequestString.GetQueryString("mobile"));
            string title = HttpUtility.UrlDecode(RequestString.GetQueryString("title"));
            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 (mobile.Trim() != "")
            {
                sql += " and F_Mobile like '%" + mobile + "%'";
            }
            if (title.Trim() != "")
            {
                sql += " and F_Title like '%" + title + "%'";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strendtime + "')>=0   ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                    "T_Client_UserFeedback",
                    "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>
        /// 获取一条详细信息
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public ActionResult GetInfo(string infoid)
        {
            if (infoid != null && infoid.Trim() != "")
            {
                Model.T_Client_UserFeedback model = bll.GetModel(int.Parse(infoid.Trim()));
                if (model != null)
                {
                    return Success("获取成功", model);
                }
                else
                {
                    return Error("获取失败");
                };
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddInfo()
        {

            int id = RequestString.GetInt("id", 0);
            string mobile = RequestString.GetFormString("mobile");
            string title = RequestString.GetFormString("title");
            string content = RequestString.GetFormString("content");
            int userId = RequestString.GetInt("userId", 0);
            string userCode = RequestString.GetFormString("userCode");
            int isDelete = RequestString.GetInt("isDelete", 0);
            int order = RequestString.GetInt("order", 0);

            if (!string.IsNullOrWhiteSpace(mobile))
            {
                Model.T_Client_UserFeedback model = new Model.T_Client_UserFeedback();
                if (id == 0)
                {
                    model.F_Mobile = mobile;
                    model.F_Title = title;
                    model.F_Content = content;
                    model.UserId = userId;
                    model.UserCode = userCode;
                    model.F_CreateTime = DateTime.Now;
                    //model.F_UpdateTime = DateTime.Now;
                    //model.F_DeleteTime = DateTime.Now;
                    model.F_IsDelete = isDelete;
                    model.F_Order = order;
                    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_Mobile = mobile;
                        model.F_Title = title;
                        model.F_Content = content;
                        model.UserId = userId;
                        model.UserCode = userCode;
                        model.F_CreateTime = model.F_CreateTime;
                        model.F_UpdateTime = DateTime.Now;
                        model.F_DeleteTime = model.F_DeleteTime;
                        model.F_IsDelete = isDelete;
                        model.F_Order = order;
                        if (bll.Update(model))
                        {
                            return Success("修改成功");
                        }
                        else
                        {
                            return Error("修改失败");
                        }
                    }
                    return Error("信息不存在");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelInfo(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                string sql = "update T_Client_UserFeedback set F_IsDelete=1 where F_Id in(" + idd.TrimEnd(',') + ")";
                if (!string.IsNullOrEmpty(idd.Trim()))
                {
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        return Success("删除成功");
                    }
                    else
                    {
                        return Error("删除失败");
                    }
                }
                else
                {
                    return Error("获取参数失败");
                }
                //if (bll.DeleteList(idd.TrimEnd(',')))
                //{
                //    return Success("删除成功");
                //}
                //else
                //    return Error("删除失败");
            }
            else
            {
                return Error("获取参数失败");
            }
        }


    }
}