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_dx
{
    public class DXRecordsController : BaseController
    {
        //获取回访记录
        public ActionResult GetList(string telid, string telphone, string license,string dode, int? type)
        {
                string sql = "";
                DataTable dt = new DataTable();
                if (telid!=null && telid.Trim() != "")
                {
                    sql += " and F_HFID=" + telid.Trim() + " ";
                }
                if (telphone != null && telphone.Trim() != "")
                {
                    sql += " and F_HFTelphone like '%" + telphone.Trim() + "%'";
                }
                
                if (type != null)
                {
                    string sqlwhere = "";
                    if (license != null && license.Trim() != "")
                    {
                        sqlwhere += " and F_License like '%" + license.Trim() + "%' ";
                    }
                    
                    if (type == 0)
                    {
                        if (dode != null && dode.Trim() != "")
                        { sqlwhere += " and F_Numbers = '" + dode.Trim() + "' "; }
                        //F_Numbers
                        sql += " and F_HFID in (select F_DVoID from T_Vis_DXOTelphone where 1=1 " + sqlwhere + ")";
                    }
                    if (type == 1)
                    {
                        if (dode != null && dode.Trim() != "")
                        { sqlwhere += " and F_InfoDode = '" + dode.Trim() + "' "; }
                        //F_InfoDode
                        sql += " and F_HFID in (select F_DVNID from T_Vis_DXNTelphone where 1=1 " + sqlwhere + ")";
                    }
                    sql += " and F_HFType="+type;
                }
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

                if (strpagesize.Trim() != "")
                {
                    pagesize = Convert.ToInt32(strpagesize);
                }
                int recordCount = 0;

                dt = BLL.PagerBLL.GetListPager(
                   "T_Vis_DXTelRecords",
                   "F_DXTRID",
                   "*",
                   " " + sql,
                   "ORDER BY F_DXTRID desc",
                   pagesize,
                   pageindex,
                   true,
                   out recordCount);

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

                return Content(obj.ToJson());
        }

        //获取回访通话记录
        public ActionResult GetTelList(string type,string telid, string phone, string usercode)
        {
                string sql = "";
                DataTable dt = new DataTable();
                if (type != null && type.Trim() != "")
                {
                    if(type=="0")
                        sql += " and TaskType='2'";
                    if (type=="1")
                        sql += " and TaskType='3'";
                }
                    if (telid != null && telid.Trim() != "")
                {
                    sql += " and CallType=1 and TaskPhoneID='" + telid.Trim() + "' ";
                }
                if (phone != null && phone.Trim() != "")
                {
                    sql += " and CallNumber like'%" + phone + "%'";
                }
                if (usercode != null && usercode.Trim() != "")
                {
                    sql += " and UserCode='" + usercode + "'";
                }
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                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",
                    "*",
                    sql,
                    "ORDER BY CallRecordsId desc",
                   pagesize,
                   pageindex,
                   true,
                   out recordCount);

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

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