using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using Fm.Bussiness;
using Fm.DataAccess;
using System.Web;
using System.Collections;

namespace Fm.Tags
{
    public class AdTags
    {
        public string Analysis_Ad(string content)
        {
            GlobalTags gb = new GlobalTags();
            public_select ps = new public_select();
             //标签解析开始---
            string Attributes = "";//存储属性
            string Text = "";//存储标签Text
            string AllText="";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:ad\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:ad\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {

                //存储新的内容
                StringBuilder str = new StringBuilder();
                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();

                //获取相关属性
                string acid = null;
                acid=gb.TagVal(Attributes,"acid");
                string adid = null;
                adid=gb.TagVal(Attributes,"adid");
                string count = null;
                count= gb.TagVal(Attributes,"count");
                string col = null;
                col=gb.TagVal(Attributes, "col");

                StringBuilder sql = new StringBuilder();

                if (acid != null && adid == null)
                {
                    sql.Append("acid=");
                    sql.Append(acid);
                }
                else if (acid == null && adid != null)
                {
                    sql.Append("adid=");
                    sql.Append(adid);
                }
                else
                {
                    sql.Append("");
                }

                string field = "top 8 *";
                if (count != null)
                {
                    field = "top " + count + " *";
                }
                if (col == null)
                {
                    col = "0";
                }
                DataView dv = ps.Getps("Fm_ad", field, sql.ToString());

                if (dv.Count > 0)
                {
                    if (int.Parse(col) > 0)
                    {
                        str.Append("<table width=100%>");
                        //获取显示行数
                        int rows = 1;
                        if (dv.Table.Rows.Count % int.Parse(col) == 0)
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col);

                        }
                        else
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col) + 1;
                        }
                        //循环行数
                        int num = 0;
                        for (int row = 1; row <= rows; row++)
                        {
                            StringBuilder txt = new StringBuilder();
                            txt.Append("<tr>");
                            //循环列
                            for (int c = 1; c <= int.Parse(col); c++)
                            {
                                if (num < dv.Table.Rows.Count)
                                {
                                    txt.Append("<td>");
                                    txt.Append(Text);
                                    txt = txt.Replace("{field.letter}", dv[num]["adletter"].ToString());
                                    txt = txt.Replace("{field.pic}", dv[num]["adpic"].ToString());
                                    txt = txt.Replace("{field.js}", dv[num]["adjs"].ToString());
                                    txt = txt.Replace("{field.url}", dv[num]["adurl"].ToString());
                                    txt.Append("</td>");

                                }
                                num++;
                            }
                            txt.Append("</tr>");
                            str.Append(txt);

                        }
                        str.Append("</table>");
                        content = content.Replace(AllText, str.ToString());
                    }
                    else
                    {
                        foreach (DataRow row in dv.Table.Rows)
                        {
                            StringBuilder txt = new StringBuilder();
                            txt.Append(Text);
                            txt = txt.Replace("{field.letter}", row["adletter"].ToString());
                            txt = txt.Replace("{field.pic}", row["adpic"].ToString());
                            txt = txt.Replace("{field.js}", row["adjs"].ToString());
                            txt = txt.Replace("{field.url}", row["adurl"].ToString());
                            str.Append(txt);
                        }
                        content = content.Replace(AllText,str.ToString());
                    }
                }
                else
                {
                    content = content.Replace(AllText, "");
                }

                
            }
            return content;
            
        }
    }
}
