using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace Fm.Tags
{
    public class Search
    {
        //搜索关键字
        private string keywords;

        public string Keywords
        {
            get { return keywords; }
            set { keywords = value; }
        }
        //页码
        private int pid;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }
        //获取关键字
        private string GetWhere()
        {
            StringBuilder str = new StringBuilder();
            str.Append("(title like '%");
            str.Append(Keywords);
            str.Append("%'");
            str.Append(" or ");
            str.Append(" keyword like '%");
            str.Append(Keywords);
            str.Append("%')");

            return str.ToString();
        }
        //获取记录数
        public int GetTotal()
        {
            Fm.Bussiness.Formpage page = new Fm.Bussiness.Formpage();
            //查询记录总数
            DataView total = page.PageRow("Fm_model_search_view", "docid", 10, 1, GetWhere()) as DataView;
            return  int.Parse(total[0]["total"].ToString());
        }
        //分页
        public string Page()
        {

            int rownum = GetTotal();
            /*分页Start*************************/
          
            //int pagesize = 10;

            int rowcount = 0;
            //判断总页码
            if (rownum % 10 == 0)
            {
                rowcount = rownum / 10;
            }
            else
            {
                rowcount = rownum / 10 + 1;
            }
            if (rowcount == 0)
            {
                rowcount = 1;
            }
            StringBuilder str = new StringBuilder();


            //循环生成分页连接
            for (int i = 1; i <= rowcount; i++)
            {
                StringBuilder s = new StringBuilder();
                if (pid > 9)
                {
                    if (i < (pid - 4) && i > 1)
                    {
                        continue;
                    }

                }
                if (i > (pid + 4) && i < rowcount)
                {
                    break;
                }
                //链接替换
                if (pid == i)
                {
                    s.Append(i);
                    s.Append("&nbsp;");
                }
                else 
                {
                    s.Append("<a href='/search/search.aspx?page=");
                    s.Append(i);
                    s.Append("&");
                    s.Append("kw=");
                    s.Append(Keywords);
                    s.Append("'");
                    s.Append(">[");
                    s.Append(i);
                    s.Append("]</a>&nbsp;");
                }
                str.Append(s);
            }
            return str.ToString();
        }
        public DataTable PageBind()
        {
            Fm.Bussiness.Formpage page=new Fm.Bussiness.Formpage();
            DataView dv = page.PageView_Bywhere("Fm_model_search_view", "docid", 10, Pid, GetWhere()) as DataView;

            //实例化新DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("title",typeof(string)));
            dt.Columns.Add(new DataColumn("intro",typeof(string)));
            dt.Columns.Add(new DataColumn("url",typeof(string)));
            dt.Columns.Add(new DataColumn("time",typeof(string)));
            dt.Columns.Add(new DataColumn("total",typeof(int)));

            foreach (DataRow row in dv.Table.Rows)
            {
                //处理地址
                string url = "";
                if (row["html"].ToString().ToLower() == "true")
                {
                    url =  row["path"].ToString();
                }
                else
                {
                    url = "/article.aspx?docid=" + row["docid"].ToString();
                }
                string title = row["title"].ToString();
               
                //数据填充
                DataRow NewRow = dt.NewRow();
                NewRow["title"] = title.Replace(Keywords, "<font color='red'>" + Keywords + "</font>");
                
                NewRow["intro"] = row["intro"].ToString();
                //NewRow["intro"] = NewRow["intro"].ToString().Replace(Keywords, "<font color='red'>" + Keywords + "</font>");

                NewRow["url"] = url;
                NewRow["time"] = DateTime.Parse(row["writertime"].ToString()).ToShortDateString();

                //处理统计列
                Regex r = new Regex(@"("+Keywords+")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                NewRow["total"] = r.Matches(title).Count;

                //填充行
                dt.Rows.Add(NewRow);
            }
            dt.DefaultView.Sort = "total desc";

            return dt;
        }
    }
}
