using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Common;
using System.Text;
using System.IO;
using System.Data;

namespace HySoft.BaseCallCenter.Web.calloutmanage.ajax
{
    public partial class callplanresultdata : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "resulstdata":
                    Response.Write(LoadReport());
                    break;
                case "resulstdataexcel":
                    Response.Write(LoadReportExcel());
                    break;
            }
        }

        #region 获取数据分析

        /// <summary>
        /// 获取数据分析
        /// </summary>
        /// <returns></returns>
        protected string LoadReport()
        {
            StringBuilder res = new StringBuilder();
            DataTable dt = new DataTable();
            string sql = "  ";
            try
            {
                string strpageindex = CommonRequest.GetQueryString("pageindex");
                int pageindex = 1;
                string strpagesize = CommonRequest.GetQueryString("pagesize");
                int pagesize = 10;
                string key = CommonRequest.GetQueryString("key");
                string taskid = CommonRequest.GetQueryString("taskid");
                string userid = CommonRequest.GetQueryString("userid");

                try
                {
                    string fpstate = CommonRequest.GetQueryString("fpstate");
                    string hcstate = CommonRequest.GetQueryString("hcstate");
                    string yjstate = CommonRequest.GetQueryString("yjstate");
                    string hjjg = CommonRequest.GetQueryString("hjjg");
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                    if (yjstate.Trim() != "")
                    {
                        sql += " and F_YJState=" + yjstate + " ";
                    }
                    if (hjjg.Trim() != "")
                    {
                        sql += " and F_HJJGId=" + hjjg + " ";
                    }
                }
                catch
                {
                }

                try
                {
                    string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                    string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                    string timesql = "";
                    int t = 0;
                    if (starttime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                    }
                    if (endtime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                    }
                    if (t > 0)
                    {

                        sql += " and F_Id in(SELECT distinct OperateObject FROM [T_Call_CallRecords] where OperateType=7 and CallType=1 " + timesql + ") ";
                    }

                }
                catch
                { }

                if (userid.Trim() != "")
                {
                    sql += " and F_UserId=" + userid + " ";
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }

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

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }

                int recordCount = 0;

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

                //if (dt != null)
                //{
                    string ctrl = "~/calloutmanage/_controls/callplanresultdata.ascx";
                    Control uc_rep = LoadControl(ctrl);
                    Repeater rptItems = (Repeater)uc_rep.FindControl("rptitems");

                    rptItems.DataSource = dt;
                    rptItems.DataBind();
                    StringWriter sw = new StringWriter(res);
                    HtmlTextWriter htw = new HtmlTextWriter(sw);

                    string pageinfo = GoPageBarStyle1(pageindex, pagesize, recordCount);
                    uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pageinfo, null);
                    rptItems.RenderControl(htw);
                //}

            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res.ToString();
        }

        /// <summary>
        /// 无刷新分页模式 当前第 {0} 页. 共 {1} 页. 每页 {2} 条. 共 {3} 条. 首页 上一页 下一页 末页 转到  页
        /// </summary>
        public string GoPageBarStyle1(int Sequence, int PageSize, int RecordCount)
        {
            string pageHtml = "<div style=\"float:left; width:48%;\">当前第 {0}/{1} 页. 每页 {2} 条. 共 {3} 条</div><div style=\"float:right; width:48%; text-align:right;\">{4} {5}</div>";
            string page0 = "", page1 = "", page2 = "", page3 = "", page4 = "", page5 = "";
            page0 = Sequence.ToString();
            int tempallpage = 1;
            if ((RecordCount % PageSize) == 0)
                tempallpage = RecordCount / PageSize;
            else
                tempallpage = RecordCount / PageSize + 1;
            page1 = tempallpage.ToString();
            page2 = PageSize.ToString();
            page3 = RecordCount.ToString();
            page5 = "<span onclick=\"var strgonum=document.getElementById('goPageNumTemp').value;if(strgonum!=''){if(strgonum>" + tempallpage + "){pagination(" + tempallpage + ");}else{pagination(strgonum);}}else{pagination(" + Sequence + ");}\" style='width:30px;cursor:hand;'>转到</span>&nbsp;&nbsp;<input id='goPageNumTemp' value='" + Sequence + "' style='width:40px;height:16px;BORDER-RIGHT: #7b9ebd 1px solid; BORDER-TOP: #7b9ebd 1px solid; FONT-SIZE: 12px; BORDER-LEFT: #7b9ebd 1px solid; BORDER-BOTTOM: #7b9ebd 1px solid;' onkeyup=\"this.value=this.value.replace(/\\D/g,'')\"  onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" />&nbsp;页";
            if (Sequence < 2)
                page4 += "<a href=\"javascript:void(null)\">首页</a> <a href=\"javascript:void(null)\">上一页</a>";
            else
            {
                page4 += "<a href=\"javascript:void(null)\" onclick=\"pagination(1);\">首页</a>&nbsp;";
                page4 += "<a href=\"javascript:void(null)\" onclick=\"pagination(" + (Sequence - 1) + ");\">上一页</a>&nbsp;";
            }

            if ((tempallpage - Sequence) < 1)
                page4 += "&nbsp;<a href=\"javascript:void(null)\">下一页</a> <a href=\"javascript:void(null)\">末页</a>";
            else
            {
                page4 += "&nbsp;<a href=\"javascript:void(null)\" onclick=\"pagination(" + (Sequence + 1) + ");\">下一页</a>&nbsp;";
                page4 += "<a href=\"javascript:void(null)\" onclick=\"pagination(" + tempallpage + ");\">末页</a>";
            }
            return string.Format(pageHtml, page0, page1, page2, page3, page4, page5);
        }
        #endregion

        #region 获取数据分析

        /// <summary>
        /// 获取数据分析
        /// </summary>
        /// <returns></returns>
        protected string LoadReportExcel()
        {
            StringBuilder res = new StringBuilder();
            DataTable dt = new DataTable();
            string sql = "  ";
            try
            {
                string key = CommonRequest.GetQueryString("key");
                string taskid = CommonRequest.GetQueryString("taskid");
                string userid = CommonRequest.GetQueryString("userid");

                try
                {
                    string fpstate = CommonRequest.GetQueryString("fpstate");
                    string hcstate = CommonRequest.GetQueryString("hcstate");
                    string yjstate = CommonRequest.GetQueryString("yjstate");
                    string hjjg = CommonRequest.GetQueryString("hjjg");
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                    if (yjstate.Trim() != "")
                    {
                        sql += " and F_YJState=" + yjstate + " ";
                    }
                    if (hjjg.Trim() != "")
                    {
                        sql += " and F_HJJGId=" + hjjg + " ";
                    }
                }
                catch
                {
                }

                try
                {
                    string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                    string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                    string timesql = "";
                    int t = 0;
                    if (starttime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                    }
                    if (endtime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                    }
                    if (t > 0)
                    {

                        sql += " and F_Id in(SELECT distinct OperateObject FROM [T_Call_CallRecords] where OperateType=7 and CallType=1 " + timesql + ") ";
                    }

                }
                catch
                { }

                if (userid.Trim() != "")
                {
                    sql += " and F_UserId=" + userid + " ";
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }
                
                dt = DBUtility.DbHelperSQL.Query("select * from vw_Call_TaskTelNum where 1=1 " + sql + " order by F_Id desc").Tables[0];

                //if (dt != null)
                //{
                    string ctrl = "~/calloutmanage/_controls/callplanresultdata.ascx";
                    Control uc_rep = LoadControl(ctrl);
                    Repeater rptItems = (Repeater)uc_rep.FindControl("rptitems");

                    rptItems.DataSource = dt;
                    rptItems.DataBind();
                    StringWriter sw = new StringWriter(res);
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, "", null);
                    rptItems.RenderControl(htw);
                //}

            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res.ToString();
        }
        #endregion
    }
}