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 System.Data.SqlClient;
public partial class manage_Manageproduct : System.Web.UI.Page
{
    static DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
            Bindproduct();
            BindToDrp();
        }
    }
    //绑定产品控件
    private void Bindproduct()
    {
        ds = Getproduct();
        DataView dv = new DataView();
        dv = ds.Tables["aa"].DefaultView;
        dv.RowFilter = "Folder=0";
        ProductGridView.DataSource = dv;
        ProductGridView.DataBind();
    }
   
    //打开产品数据库
    public DataSet Getproduct()
    {
        INews shownews = new Mynews();
        DataSet ds = shownews.DsGetinfo("SELECT ProductID,ParrentID,Title,Folder,Showorder,Hit,Createdate FROM Product ORDER BY Showorder DESC");
       
        return ds;
    }
    //判断产品类别
    protected string Myleibie(int nParrentID)
    {
        if (nParrentID == 0)
        {
            return ("根目录");
        }
        else
        {
            DataRow[] row = ds.Tables["aa"].Select("ProductID=" + nParrentID);
            return (row[0]["Title"].ToString());
        }
        
    }
    //翻页
    protected void ProductGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ProductGridView.PageIndex = e.NewPageIndex;
        Bindproduct();
    }
    //绑定下拉列表
    private void BindToDrp()
    {
        SqlDataReader dr = Getleibie();
        DrpLeibie.Items.Clear();
        DrpLeibie.Items.Add(new ListItem("根目录下", "0"));
        while (dr.Read())
        {
            DrpLeibie.Items.Add(new ListItem(dr["Title"].ToString(), dr["ProductID"].ToString()));
        }
        dr.Close();
    }
    //打开类表
    public SqlDataReader Getleibie()
    {
        INews shownews = new Mynews();
        SqlDataReader dr = shownews.SqlGetinfo("SELECT ProductID,Title FROM Product WHERE Folder=1 ORDER BY Showorder DESC");
        
        return dr;
    }
    //按ID删除产品
    private void Deleteproduct(int nProductID)
    {
        INews shownews = new Mynews();
        shownews.Executeinfo("DELETE FROM Product WHERE ProductID=" + nProductID);
        
    }
    //修改目录
    private void Updatemulu(int nProductID,int nParrentID)
    {
        INews shownews = new Mynews();
        shownews.Executeinfo("UPDATE Product SET ParrentID=" + nParrentID + " WHERE ProductID=" + nProductID);
   
    }
    protected void movBtn_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in ProductGridView.Rows)
        {
            CheckBox cbox = (CheckBox)row.FindControl("chkBox");
            if (cbox != null)
            {
                if (cbox.Checked == true)
                {
                    int productid = Int32.Parse(ProductGridView.DataKeys[row.RowIndex].Value.ToString());
                    int parrentid = Int32.Parse(DrpLeibie.SelectedValue.ToString());
                    Updatemulu(productid, parrentid);
                }
            }
        }
        Bindproduct();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in ProductGridView.Rows)
        {
            CheckBox cbox = (CheckBox)row.FindControl("chkBox");
            if (cbox != null)
            {
                if (cbox.Checked == true)
                {
                    int productid = Int32.Parse(ProductGridView.DataKeys[row.RowIndex].Value.ToString());
                    Deleteproduct(productid);
                }
            }
        }
        Bindproduct();
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= ProductGridView.Rows.Count-1; i++)
        {
            CheckBox cbox = (CheckBox)ProductGridView.Rows[i].FindControl("chkBox");
            if (CheckBox1.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
        
        
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
