using CallCenter.Utility;
using CallCenterApi.Common;
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.tel
{
    [Authority]
    public class CallOutScreenController : BaseController
    {
        /// <summary>
        /// 当前用户最近电话列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPhoneList()
        {
                
                

                string sql = " and UserCode='" + User.F_WorkNumber + "'";
                DataTable dt = new DataTable();

                string strtel = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
                string strcalltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
                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 (strcalltype.Trim() != "" && strcalltype != "undefined")
                {
                    sql += " and CallType=" + strcalltype + " ";
                }
                if (strtel.Trim() != "" && strtel != "undefined")
                {
                    sql += " and CallNumber like '%" + strtel.Trim() + "%' ";
                }
                //else
                //{
                //    sql += " and 1=0 ";
                //}
                if (strstarttime.Trim() != "" && strstarttime != "undefined")
                {
                    sql += " and datediff(day,BeginTime,'" + strstarttime + "')<=0 ";
                }
                if (strendtime.Trim() != "" && strendtime != "undefined")
                {
                    sql += " and datediff(day,BeginTime,'" + strendtime + "')>=0   ";
                }

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

                if (strpagesize.Trim() != "")
                {
                    pagesize = Convert.ToInt32(strpagesize);
                }
                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "T_Call_CallRecords",
                    "CallRecordsId",
                    "*,dbo.GetUserName(UserCode) as UserName",
                    sql,
                    "ORDER BY CallRecordsId 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 GetUserList()
        {
            string sql = " and F_DeleteFlag=0 ";//" and UserID="+ID;
            DataTable dt = new DataTable();

            string strtel = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));
            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 (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and F_UserName like '%" + strname.Trim() + "%'";
            }
            if (strtel.Trim() != "" && strtel != "undefined")
            {
                sql += " and (F_Telephone like '%" + strtel.Trim() + "%' or F_Mobile like '%" + strtel.Trim() + "%' or F_HomePhone like '%" + strtel.Trim() + "%' or F_WorkNumber like '%" + strtel.Trim() + "%') ";
            }
            //else
            //{
            //    sql += " and 1=0 ";
            //}
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateOn,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateOn,'" + strendtime + "')>=0   ";
            }

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_UserAccount",
                "F_UserId",
                "*",
                sql,
                "ORDER BY F_UserId 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 GetCustomerList()
        {
            string sql = "";//" and UserID="+ID;
            DataTable dt = new DataTable();

            string strtel = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));
            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 (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and F_CustomerName like '%" + strname.Trim() + "%'";
            }
            if (strtel.Trim() != "" && strtel != "undefined")
            {
                sql += " and (F_Telephone like '%" + strtel.Trim() + "%' or F_Mobile like '%" + strtel.Trim() + "%' or F_ChargeTelephone like '%" + strtel.Trim() + "%') ";
            }
            //else
            //{
            //    sql += " and 1=0 ";
            //}
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateOn,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateOn,'" + strendtime + "')>=0   ";
            }

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

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

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }
    }
}