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

namespace HySoft.BaseCallCenter.Web.reportmanage
{
    public partial class totalcallreport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] == "show")
            {
                string str = GetHighCharts("");
                Response.Write(str);
                Response.End();
            }
        } 
        /// <summary>
        /// 获取HighCharts数据
        /// </summary>
        /// <returns></returns>
        public string GetHighCharts(string BeginYear)
        {
            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1").Tables[0];
            DateTime dtNow;

            if (string.IsNullOrEmpty(BeginYear))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(BeginYear);
            }

            string result = "[";
            for (int i = 1; i <= 12; i++)
            {
                result += "{";
                DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-01 00:00:00");
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);
                DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<'" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                result += "y:" + dtRow.Length + ",";
                result += "color:colors[" + ((i - 1) / 3) + "],";
                result += "drilldown:{" + GetDrillDown(dtRow, beginTime, days) + "}";
                result += "},";
            }
            result = result.TrimEnd(',');
            result += "]";
            return result;
        }

        /// <summary>
        /// 获取月份中的天数数据
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string GetDrillDown(DataRow[] DataSource, DateTime SummaryDateTime, int Days)
        {
            string result = "";
            string categories = "[";
            string data = "[";
            result += "name:'" + SummaryDateTime.Month + "',";
            for (int i = 1; i <= Days; i++)
            {
                categories += "'" + i + "日',";
                if (DataSource.Count() > 0)
                    data += DataSource.CopyToDataTable().Select("begintime >='" + SummaryDateTime.AddDays(i - 1) + "' and  beginTime<'" + SummaryDateTime.AddDays(i).AddSeconds(-1) + "'").Length + ",";
                else
                    data += "0" + ",";
            }
            categories = categories.TrimEnd(',') + "],";
            data = data.TrimEnd(',') + "],";
            result += "categories:" + categories;
            result += "data:" + data;
            result += "color:colors[" + (SummaryDateTime.Month - 1) / 3 + "]";
            return result;
        }


        /// <summary>
        /// 生成表格的标题和统计字段
        /// </summary>
        /// <param name="TableTitle">表格的标题</param>
        /// <param name="TableHead">表格统计字段</param>
        /// <returns></returns>
        public StringBuilder CreateTableTitleAndHead(string TableTitle, string beginYear, params object[] TableHead)
        {
            StringBuilder html = new StringBuilder();
            html.Append("<h1 style='font-size: 18px;font-weight: bold;color: #333333;text-align:center;width:100%;'><b>" + TableTitle + "</b></h1>");
            html.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"400\" align='center'>");
            html.Append("<tr style=\"text-align: center;\">");
            for (int i = 0; i < TableHead.Length; i++)
            {
                //标题第一行
                html.Append("<td class=\"formtabletitle1\"style=\"height: 25px;background-color: #D5EDFE;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-top: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;padding: 3px 3px 3px 3px;text-align:center;\">" + TableHead[i] + "</td>");
            }
            html.Append("</tr>");
            GetTabelData(html, beginYear);
            html.Append("</table>");
            return html;
        }


        /// <summary>
        ///  获取表格中的统计数据
        /// </summary>
        /// <returns></returns>
        public string GetTabelData(StringBuilder SB, string BeginYear)
        {
            DataTable dt = new BLL.T_Call_CallRecords().GetList("1=1").Tables[0];
            DateTime dtNow;
            int Count = 0;
            int DayCount = 0;
            string strHtml = "";

            if (string.IsNullOrEmpty(BeginYear))
            {
                dtNow = DateTime.Now;
            }
            else
            {
                dtNow = DateTime.Parse(BeginYear);
            }

            for (int i = 1; i <= 12; i++)
            {
                DateTime beginTime = Convert.ToDateTime(dtNow.Year + "-" + i + "-01 00:00:00");
                int days = DateTime.DaysInMonth(dtNow.Year, beginTime.Month);
                DataRow[] dtRow = dt.Select("begintime >='" + beginTime.ToString() + "' and  beginTime<'" + beginTime.AddDays(days).AddSeconds(-1) + "'");
                if (dtRow.Length > 0)
                {
                    Count += dtRow.Length;
                    for (int j = 1; j <= days; j++)
                    {
                        if (dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<'" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length > 0)
                        {
                            DayCount += 1;
                            strHtml += "<tr align=\"center\" valign=\"middle\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\" bgcolor=\"#ffffff\">";
                            strHtml += "<td class=\"formtable_td\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;\">" + beginTime.AddDays(j - 1).ToShortDateString() + "&nbsp;</td>";
                            strHtml += "<td class=\"formtable_td\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;\">" + dtRow.CopyToDataTable().Select("begintime >='" + beginTime.AddDays(j - 1) + "' and  beginTime<'" + beginTime.AddDays(j).AddSeconds(-1) + "'").Length + "&nbsp;</td>";
                            strHtml += "</tr>";
                        }
                    }

                    if (DayCount != 0)
                    {
                        SB.Append("<tr align=\"center\" valign=\"middle\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\" bgcolor=\"#ffffff\">");
                        SB.Append("<td class=\"formtable_td\" rowspan=\"" + (DayCount + 2) + "\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;\">" + beginTime.Month + "月&nbsp;</td>");
                        SB.Append(strHtml);
                        SB.Append("<tr align=\"center\" valign=\"middle\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\" bgcolor=\"#ffffff\">");
                        SB.Append("<td class=\"formtable_td\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;background-color: #e4d354;\">总计：&nbsp;</td>");
                        SB.Append("<td class=\"formtable_td\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;background-color: #e4d354;\">" + dtRow.Length + "&nbsp;</td>");
                        SB.Append("</tr>");
                        SB.Append("</tr>");
                    }
                    strHtml = "";
                    DayCount = 0;
                }
            }
            SB.Append("<tr align=\"center\" valign=\"middle\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\" onmouseout=\"this.style.backgroundColor='#ffffff'\" bgcolor=\"#ffffff\">");
            SB.Append("<td class=\"formtable_td\" style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;background-color: #D5EDFE;\">" + dtNow.Year + "年总计：&nbsp;</td>");
            SB.Append("<td class=\"formtable_td\" colspan=\"2\"  style=\"vertical-align: middle;border-top: 0px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;padding-left: 2px;height: 25px;padding-top: 3px;padding-bottom: 3px;background-color: #D5EDFE;\">" + Count + "&nbsp;</td>");
            SB.Append("</tr>");
            return SB.ToString();
        }

    }
}