using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JuSNS.Model;
using JuSNS.UI.Control;

namespace JuSNS.Web.system.news
{
    public partial class index : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "news";
        JuSNS.Home.News ns = new JuSNS.Home.News();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.QueryString["Type"];
            if (type == "del")
            {
                if (ns.DelNews(Convert.ToInt32(Request.QueryString["id"]),0) > 0)
                {
                    Response.Redirect("index.aspx");
                }
            }
            else if (type == "delAll")
            {
                if (ns.DelNews(0, 1) > 0)
                {
                    Response.Redirect("index.aspx");
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    LoadList();
                    DataTable dt = ns.GetNewsClassD(0);
                    string lStr = "<a href=\"index.aspx\">所有新闻</a> &nbsp; ";
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            lStr += "<a href=\"index.aspx?c=" + dt.Rows[i]["ID"].ToString() + "\">" + dt.Rows[i]["className"].ToString() + "</a> &nbsp; ";
                        }
                    }
                    dt.Dispose();
                    this.ClassList.InnerHtml = lStr;
                }
            }
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            int r, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            string c = Request.QueryString["c"];
            DataTable dt = null;
            if (!string.IsNullOrEmpty(c))
            {
                SqlConditionInfo st = new SqlConditionInfo("@c", Convert.ToInt32(c), TypeCode.Int32);
                dt = JuSNS.Home.UtilPage.GetPage("Mange_NewsC_aspx", PageIndex, PAGESZ, out r, out p, st);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.Key.Text))
                {
                    SqlConditionInfo[] st = new SqlConditionInfo[1];
                    st[0] = new SqlConditionInfo("@Key", Key.Text, TypeCode.String);
                    st[0].Blur = 3;
                    dt = JuSNS.Home.UtilPage.GetPage("Mange_NewsL_aspx", PageIndex, PAGESZ, out r, out p, st);
                }
                else
                {
                    dt = JuSNS.Home.UtilPage.GetPage("Mange_News_aspx", PageIndex, PAGESZ, out r, out p, null);
                }
            }
            if (dt != null)
            {
                Rpt_NewsrList.DataSource = dt;
                Rpt_NewsrList.DataBind();
            }
            //判断是否调用分页
            if (r > PAGESZ)
            {
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "index.aspx");
            }
            else
            {
                DivPage.InnerHtml = "";
            }
        }

        /// <summary>
        /// 执行搜索过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
                LoadList();
        }
    }
}
