using System;
using System.Data;
using System.Collections.Generic;

namespace Stat.Html
{
    public class RPage
    {
        /// <summary>
        /// 来路页面数据列表（HTML）
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="cPage"></param>
        /// <param name="pSize"></param>
        /// <returns></returns>
        public static string GetList(List<Model.StatItem> list, int cPage, int pSize)
        {
            if (list != null && list.Count > 0)
            {
                /*--------------------------------------------------------*/
                int st = (cPage - 1) * pSize;
                st = st > list.Count ? list.Count : st;
                int et = st + pSize;
                et = et > list.Count ? list.Count : et;
                /*--------------------------------------------------------*/
                if (list[0].IpCount == 0)
                {
                    return GetSingle(list, ref st, et);
                }
                else
                {
                    return GetMulti(list, ref st, et);
                }
            }
            return null;

        }

        private static string GetMulti(List<Model.StatItem> dt, ref int st, int et)
        {
            System.Text.StringBuilder tep = new System.Text.StringBuilder();
            tep.Append(" <table bgcolor=\"#cedfef\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" class=\"padding\">");
            tep.Append(" <tr>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"8%\" align=\"center\"  height=\"25\"><strong>排名</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"47%\" align=\"left\" ><strong>来路页面</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"8%\" align=\"left\" ><strong>P V</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"7%\" align=\"left\" ><strong>I P</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"8%\" align=\"left\" ><strong>U V</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"7%\" align=\"left\" ><strong>人均PV</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"*\" align=\"left\" ><strong>份额比例</strong></td>");
            tep.Append("  </tr>");

            for (int i = st; i < et; i++)
            {
                /*----------合计--------------------------------------------*/
                if (i == 0)
                {
                    tep.Append("<tr bgcolor=\"#FFFFFF\" onmouseover=\"this.style.backgroundColor='#EEF3F9';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\">");
                    tep.Append(" <td align=\"center\" height=23 class=\"blud14\">合计</td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">-</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt[0].PvCount + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt[0].IpCount + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt[0].UvCount + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt[0].PerPV.ToString("0.0") + "</span></td>");
                    tep.Append(" <td align=\"left\" >&nbsp;-</td>");
                    tep.Append("</tr>");
                }
                /*--------------------------------------------------------*/
                else
                {
                    tep.Append("<tr bgcolor=\"#FFFFFF\" onmouseover=\"this.style.backgroundColor='#EEF3F9';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\">");

                    if (i < 4)
                    {
                        tep.Append(" <td align=\"center\" height=\"23\"><span class=\"red14\"><strong>" + (i) + "</strong></span></td>");
                    }
                    else
                    {
                        tep.Append(" <td align=\"center\" height=\"23\">" + (i) + "</td>");
                    }
                    tep.Append(" <td align=\"left\" ><a href=\"" + dt[i].Name + "\" target=\"_blank\"><strong>" + FormatName(dt[i].Name) + "</strong></a></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\"><strong>" + dt[i].PvCount + "</strong></span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + dt[i].IpCount + "</span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + dt[i].UvCount + "</span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + dt[i].PerPV.ToString("0.0") + "</span></td>");
                    tep.Append(" <td align=\"left\" ><img src=\"Images/bar4.gif\" height=\"10\" width=\"" + Convert.ToInt32(dt[i].Percent) + "\" />&nbsp;" + dt[i].Percent.ToString("0.00") + "%</td>");

                    tep.Append("</tr>");

                    st++;
                }
            }
            tep.Append("</table>");
            return tep.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string FormatName(string s)
        {
            if (s == "")
            {

                return "输入网址或者书签";
            }
            if (s.Length > 48)
            {
                return s.Substring(0, 45) + "...";
            }
            return s;
        }
        private static string GetSingle(List<Model.StatItem> dt, ref int st, int et)
        {
            System.Text.StringBuilder tep = new System.Text.StringBuilder();
            tep.Append(" <table bgcolor=\"#cedfef\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" class=\"padding\">");
            tep.Append(" <tr>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"10%\" align=\"center\"  height=\"25\"><strong>排名</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"50%\" align=\"left\" ><strong>来路页面</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"20%\" align=\"left\" ><strong>P V</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"*\" align=\"left\" ><strong>份额比例</strong></td>");
            tep.Append("  </tr>");

            for (int i = st; i < et; i++)
            {
                if (i == 0)
                {
                    tep.Append("<tr bgcolor=\"#FFFFFF\" onmouseover=\"this.style.backgroundColor='#EEF3F9';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\">");
                    tep.Append(" <td align=\"center\" height=23 class=\"blud14\">合计</td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">-</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt[0].PvCount + "</span></td>");
                    tep.Append(" <td align=\"left\" >&nbsp;-</td>");
                    tep.Append("</tr>");
                }
                else
                {
                    tep.Append("<tr bgcolor=\"#FFFFFF\" onmouseover=\"this.style.backgroundColor='#EEF3F9';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\">");

                    if (i < 4)
                    {
                        tep.Append(" <td align=\"center\" height=\"23\"><span class=\"red14\"><strong>" + (i) + "</strong></span></td>");
                    }
                    else
                    {
                        tep.Append(" <td align=\"center\" height=\"23\">" + (i) + "</td>");
                    }
                    tep.Append(" <td align=\"left\" ><a href=\"" + dt[i].Name + "\" target=\"_blank\"><strong>" + FormatName(dt[i].Name) + "</strong></a></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\"><strong>" + dt[i].PvCount + "</strong></span></td>");
                    tep.Append(" <td align=\"left\" ><img src=\"Images/bar4.gif\" height=\"10\" width=\"" + dt[i].PerPV.ToString("0.0") + "\" />&nbsp;" + dt[i].Percent.ToString("0.00") + "%</td>");
                    tep.Append("</tr>");

                    st++;
                }
            }

            tep.Append("</table>");
            return tep.ToString();
        }
    }
}
