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

public partial class article_list : System.Web.UI.Page
{
    sqlServer sql = new sqlServer();
    classify c = new classify();
    javasrcipt js = new javasrcipt();
    fileDocument f = new fileDocument();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            advertisement ad = new advertisement();
            ad200.Text = ad.read("20130427191139046", 200, 0, 2, true, true, "ad200H");

            string keyWordsStr = xabara.safeSql(Server.UrlDecode(Request.QueryString["keyWord"]));
            string ifStr = string.Empty;
            string unionStr = string.Empty;
            string orderBYstr = string.Empty;
            string className = string.Empty;

            long cidTemp = xabara.isNum(Request.QueryString["cid"]);
            //route取值
            if (cidTemp < 1)
            {
                cidTemp = xabara.isNum(this.Page.RouteData.Values["cID"].ToString().Replace(".htm", ""));
            }
            c.bindingRepeaterClass(true, cidTemp, "news", navList);

            string cidStr = c.ReadAllClassID("news", cidTemp); //得到所有ID
            //Response.Write(cidStr);
            //Response.End();

            if (cidStr.IndexOf(",") > 0)
            {
                string[] ids = cidStr.Split(new char[] { ',' });
                for (int i = 0; i <= ids.GetUpperBound(0); i++)
                {
                    ifStr += " select Nid,upPreImg,NewTitle,titleFontB,titleFontC,NewsAuthor,ReadNums,postDate,topDate from NewsXabara where adminPass=1 and treeTid=" + ids[i].ToString().Trim() + " union ";
                }
            }
            else
            {
                ifStr = " select Nid,upPreImg,NewTitle,titleFontB,titleFontC,NewsAuthor,ReadNums,postDate,topDate from NewsXabara where adminPass=1 and treeTid=" + cidStr + " union ";

                unionStr = " and treeTid=" + cidTemp.ToString().Trim(); //搜索判断用途
            }

            navClass.Text = "当前位置：<a href='/' target='_top'>首页</a> - <a href=\"/article/default.htm\" target='_top'>家教资讯</a>";
            if (!string.IsNullOrEmpty(keyWordsStr as string))
            {
                ifStr = " select Nid,upPreImg,NewTitle,titleFontB,titleFontC,NewsAuthor,ReadNums,postDate,topDate from NewsXabara where adminPass=1 " + unionStr + " and  NewTitle like '%" + xabara.safeSql(keyWordsStr) + "%' union ";
                ifStr += " select Nid,upPreImg,NewTitle,titleFontB,titleFontC,NewsAuthor,ReadNums,postDate,topDate from NewsXabara where adminPass=1" + unionStr + " and keyWord like '%" + xabara.safeSql(keyWordsStr) + "%' union ";

                navClass.Text += " - 搜索结果：<font color='red'>" + HttpUtility.HtmlEncode(keyWordsStr) + "</font>";
                //keyWords.Text = keyWordsStr;
                navClass.Text= "搜索关键词：<font color='red'>" + HttpUtility.HtmlEncode(keyWordsStr) + "</font>";
            }
            else
            {
                if (cidTemp > 0)
                {
                    className = " - " + c.readClassName_Link(cidTemp, "*.htm", string.Empty);
                }
                navClass.Text += className;
            }

            ifStr = "(" + ifStr.Substring(0, ifStr.Length - 6) + ") as news";

            string strSql = "select * from " + ifStr + " Order By TopDate desc,PostDate desc";
            //Response.Write(strSql);
            //Response.End();

            lblCurrentPage.Text = sql.RepeaterDB(RepeaterListNews, strSql, "&keyWord=" + Server.UrlEncode(keyWordsStr), 30, "page", "center", true);
            if (this.RepeaterListNews.Items.Count < 1)
            {
                errInfo.Text = "目前暂没有任何信息！";
                pageDiv.Visible = false;
            }
            else
            {
                errDiv.Visible = false;
            }

            if (this.RepeaterListNews.Items.Count == 1) //单条自动转向
            {
                HyperLink url = (HyperLink)this.RepeaterListNews.Items[0].FindControl("showLink");
                Response.Redirect(url.NavigateUrl.ToString().Trim(), true);
            }
        }

        Page.Title = CHAR.NoHtm(navClass.Text.Replace("当前位置：", "")) + "　" + xml.read("xabara.config", "webName", 180) + xml.read("xabara.config", "titleMeta", 180);
    }

    public string cut(string str, int word)
    {
        return CHAR.left(str, word);
    }

    public string bc(string title, string b, string c)
    {
        string titleBC = string.Empty;
        if (!string.IsNullOrEmpty(title as string))
        {
            if (b.Equals("1"))
            {
                titleBC = "font-weight:bold;";
            }

            if (!string.IsNullOrEmpty(c as string))
            {
                titleBC += "color:" + c + ";";
            }

            titleBC = "<span style=\"" + titleBC + "\">" + title + "</span>";
        }

        return titleBC;
    }

    public string getNewsInfo(string newsAuthor, string postDate, string clickNums)
    {
        string str = string.Empty;
        if (!string.IsNullOrEmpty(newsAuthor as string))
        {
            str = "作者：" + newsAuthor + "&nbsp;&nbsp;";
        }
        str += "发布时间：" + Convert.ToDateTime(postDate).ToString("yyyy-MM-dd HH:mm:ss");
        str += "&nbsp;&nbsp;阅览数：" + clickNums;

        return str;
    }

    public string pic(string str)
    {
        return f.picDisplay(str);
    }

    protected void searchBotton_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("default.htm?keyWord=" + Server.UrlEncode(keyWords.Text.Trim()), true);
    }
}