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 UUN;
using System.Data.SqlClient;

public partial class Admin_LunTan_BanKuai : System.Web.UI.Page
{
    private 访问 访问;
    protected String 分类代码;
    protected String SQL = "SELECT [ID],[版块名称],[版块状态],[排序],[显示状态],[贴子数],[回贴数],[今日发贴数],[今日回贴数],[创建时间] FROM ["+程序.数据表前缀+"社区_论坛_版块]";
    protected String 数量SQL = "SELECT COUNT(ID) FROM [" + 程序.数据表前缀 + "社区_论坛_版块]";
    protected String 排序 = " [排序]";
    protected new int ID;
    protected void Page_Load(object sender, EventArgs e)
    {
        访问 = new 访问(this.Context);
        访问.开始处理("版块管理", "网站后台", "版块管理", new 访问.回调委托(ZhiXing));
        访问.关闭连接();
    }

    private void ZhiXing()
    {
        访问.验证管理员();

        ID = Convert.ToInt32(Request.QueryString["ID"]);

        if (ID == 0)
            Label1.Text = "";
        else
        {
            int 上级版块 = 0;
            SqlCommand 语句 = new SqlCommand("SELECT [所属版块] FROM [" + 程序.数据表前缀 + "社区_论坛_版块] WHERE [ID]=" + ID, 访问.连接);
            SqlDataReader 读取 = 语句.ExecuteReader();
            if (读取.Read())
                上级版块 = Convert.ToInt32(读取["所属版块"]);
            else
            {
                读取.Close();
                throw new Exception("版块不存在");
            }
            读取.Close();
            Label1.Text = "<a href=\"BanKuai.aspx?ID=" + 上级版块 + "\">返回上级版块</a>";
        }

        生成条件();
        生成排序();

        if (!this.IsPostBack)
            生成数据();
    }

    private void 生成条件()
    {
        SQL += " WHERE [所属版块]=" + ID;
        数量SQL += " WHERE [所属版块]=" + ID;
    }

    private void 生成排序()
    {
        String 字段 = Request.QueryString["zd"];
        if (字段 == null || 字段 == "")
            return;
        bool 方向 = Convert.ToBoolean(Request.QueryString["fx"]);
        排序 = " [" + 字段 + "]" + (方向 ? " DESC" : "");
        if (!this.IsPostBack)
        {
            PaiXuZiDuan.Value = 字段;
            PaiXuFangXiang.Value = 方向.ToString();
        }
    }

    private void 生成数据()
    {
        SqlCommand 语句 = new SqlCommand(数量SQL, 访问.连接);
        后台分页 分页 = new 后台分页(Convert.ToInt32(语句.ExecuteScalar()),
            Convert.ToInt32(Request.QueryString["page"]),
            10,
            "BanKuai.aspx?ID=" + ID + "&zd=" + Server.UrlEncode(PaiXuZiDuan.Value) + "&fx=" + PaiXuFangXiang.Value);

        DataSet 数据 = new DataSet();
        语句 = new SqlCommand(SQL + " ORDER BY " + 排序, 访问.连接);

        SqlDataAdapter 填充 = new SqlDataAdapter(语句);
        填充.Fill(数据, (分页.当前页 - 1) * 10, 10, "版块");

        GridView1.DataSource = 数据;
        GridView1.DataBind();

        if (数据.Tables[0].Rows.Count > 0)
            CaoZuoDaiMa.Text = "操作：<a href=\"#\" onclick=\"if(HuoQuZhi('d')!=''){"+
                "location.href='BanKuai_ZhuanYi.aspx?ID='+HuoQuZhi('d')+'&rurl="+Server.UrlEncode(Request.Url.ToString())+"'"
                +"}return false;\">转移</a>&nbsp;&nbsp;" +
                "<a href=\"#\" onclick=\"if(HuoQuZhi('d')!=''){"+
                "location.href='BanKuai_HeBing.aspx?ID='+HuoQuZhi('d')+'&rurl=" + Server.UrlEncode(Request.Url.ToString()) + "'"
                +"}return false;\">合并</a>&nbsp;&nbsp;<a href=\"#\" onclick=\""+
                "if(confirm('真的要删除吗？')){location.href='BanKuai_ShanChu.aspx?ID='+HuoQuZhi('d')+'&rurl=" + Server.UrlEncode(Request.Url.ToString()) + "';}"
                +"\">删除</a>";

        FenYeDaiMa.Text = 分页.生成代码();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        排序 = "[" + e.SortExpression + "]";

        if (PaiXuZiDuan.Value == e.SortExpression && (!Convert.ToBoolean(PaiXuFangXiang.Value)))
        {
            排序 += " DESC";
            PaiXuFangXiang.Value = "true";
        }
        else
        {
            排序 += "";
            PaiXuFangXiang.Value = "false";
        }

        PaiXuZiDuan.Value = e.SortExpression;

        访问.连接.Open();

        访问.排错调用(new 访问.回调委托(生成数据));

        访问.关闭连接();
    }
}
