using System;
using System.Data;

namespace Stat.Html
{
   public class Vexit
    {
        /// <summary>
        /// 数据列表（HTML）
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="cPage"></param>
        /// <param name="pSize"></param>
        /// <returns></returns>
        public static string GetList(DataTable dt, int cPage, int pSize)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                /*--------------------------------------------------------*/
                int st = (cPage - 1) * pSize;
                st = st > dt.Rows.Count ? dt.Rows.Count : st;
                int et = st + pSize;
                et = et > dt.Rows.Count ? dt.Rows.Count : et;
                /*--------------------------------------------------------*/
                if (dt.Columns.Count == 4)
                {
                    return GetSingle(dt, ref st, et);
                }
                else
                {
                    return GetMulti(dt, ref st, et);
                }
            }
            return null;

        }

        private static string GetMulti(DataTable 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>跳出次数</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>人均次数</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.Rows[0][2] + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt.Rows[0][3] + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + dt.Rows[0][4] + "</span></td>");
                    tep.Append(" <td align=\"left\"><span class=\"blud14\">" + Convert.ToDouble(dt.Rows[0][5]).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.Rows[i][1].ToString() + "\" target=\"_blank\"><strong>" + FormatName(dt.Rows[i][1].ToString()) + "</strong></a></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\"><strong>" + dt.Rows[i][2] + "</strong></span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + dt.Rows[i][3] + "</span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + dt.Rows[i][4] + "</span></td>");
                    tep.Append(" <td align=\"left\" ><span class=\"red14\">" + Convert.ToDouble(dt.Rows[i][5]).ToString("0.0") + "</span></td>");
                    tep.Append(" <td align=\"left\" ><img src=\"Images/bar4.gif\" height=\"10\" width=\"" + Convert.ToInt32(dt.Rows[i][6]) + "\" />&nbsp;" + Convert.ToDouble(dt.Rows[i][6]).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 "未知URL";
            }
            if (s.Length > 48)
            {
                return s.Substring(0, 45) + "...";
            }
            return s;
        }
        private static string GetSingle(DataTable 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>跳出次数</strong></td>");
            tep.Append("   <td bgcolor=\"#e9f1fa\" width=\"*\" align=\"left\" ><strong>跳出比例</strong></td>");
            tep.Append("  </tr>");

            for (int i = st; i < et; i++)
            {

                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 + 1) + "</strong></span></td>");
                }
                else
                {
                    tep.Append(" <td align=\"center\" height=\"23\">" + (i + 1) + "</td>");
                }
                tep.Append(" <td align=\"left\" ><a href=\"" + dt.Rows[i][1].ToString() + "\" target=\"_blank\"><strong>" + FormatName(dt.Rows[i][1].ToString()) + "</strong></a></td>");
                tep.Append(" <td align=\"left\" ><span class=\"red14\"><strong>" + dt.Rows[i][2] + "</strong></span></td>");
                tep.Append(" <td align=\"left\" ><img src=\"Images/bar4.gif\" height=\"10\" width=\"" + Convert.ToDouble(dt.Rows[i][3]).ToString("0.0") + "\" />&nbsp;" + Convert.ToDouble(dt.Rows[i][3]).ToString("0.00") + "%</td>");
                tep.Append("</tr>");

                st++;
            }

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