using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace norke.admin
{
	/// <summary>
	/// productAlter 的摘要说明。
	/// </summary>
	public partial class productAlter : System.Web.UI.Page
	{
    
		protected void Page_Load(object sender, System.EventArgs e)
		{
            //权限检查
            HttpCookie cookie = Request.Cookies["norkeyangjian"];
            if (cookie == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }

            if( !IsPostBack )
            {
                getData();
                getddlClassData();
            }
		}

      
        private void getData()//绑定数据
        {
            DBConn myDB = new DBConn();
            string sql = "select * from Products Left Outer join Category on  Products.CID = Category.CID where uname='"+Request.Cookies["norkeyangjian"].Value+"' order by PID desc";
            DataGrid1.DataSource = myDB.getDataReader(sql);
            DataGrid1.DataBind();
            mySqlPager.setAttribute( SqlPager1, "DataGrid1", sql, "1000000-PID", 20);                
        }

        private void getddlClassData()//绑定 类别选框
        {
            DBConn myDB = new DBConn();
            string sql="select * from Category order by CID desc";
            ddlClass.DataSource  = myDB.getDataReader(sql);
            ddlClass.DataTextField = "CName";
            ddlClass.DataValueField = "CID";
            ddlClass.DataBind();
            myDB.Close();
            
            ddlClass.Items.Insert(0,new ListItem("所有分类","-1"));
        }

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		public void InitializeComponent()
		{    
             this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

        }
		#endregion

        private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
          
        }
      
        protected void btnSelect_Click(object sender, System.EventArgs e)
        {
            string strSelect = CleanString.htmlInputText( txtSelect.Text );
            string mySql="select * from Products Left Outer join Category on  Products.CID = Category.CID order by PID desc";
            string strClass = ddlClass.SelectedValue;

            if( strClass.Equals("-1") )
            {
                switch( ddlSelect.SelectedValue )
                {
                    case "0":
                        mySql="select * from Products Left Outer join Category on  Products.CID = Category.CID where PID like '%" + strSelect + "%' order by PID desc";
                        break;
                    case "1":
                        mySql="select * from Products Left Outer join Category on  Products.CID = Category.CID where PName like '%" + strSelect + "%' order by PID desc";
                        break;
                }
            }
            else
            {
                switch( ddlSelect.SelectedValue )
                {
                    case "0":
                        mySql="select * from Products Left Outer join Category on  Products.CID = Category.CID where Products.CID=" + strClass + " and PID like '%" + strSelect + "%' order by PID desc";
                        break;
                    case "1":
                        mySql="select * from Products Left Outer join Category on  Products.CID = Category.CID where Products.CID=" + strClass + " and PName like '%" + strSelect + "%' order by PID desc";
                        break;
                }
            }          
            mySqlPager.setAttribute( SqlPager1, "DataGrid1", mySql, "1000000-PID", 20);
        }

        protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            string strid = e.Item.Cells[0].Text;
            DBConn myDB = new DBConn();
            string sql = "select PPicture from Products where PID=" + strid;
            SqlDataReader dr = myDB.getDataReader(sql);
            if (dr.Read())
            {
                Label1.Text = dr["PPicture"].ToString();
            }
            dr.Close();
            System.IO.File.Delete(base.Server.MapPath(".\\UPloadImg\\") + Label1.Text);
            string mySql = "Delete from Products where PID=" + strid;
            myDB.Delete(mySql);
            myDB.Close();
            getData();
        }
}
}
