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;
using System.Configuration;
using System.IO;
namespace norke.admin
{
	/// <summary>
	/// productDetail 的摘要说明。
	/// </summary>
	public partial class productDetail : System.Web.UI.Page
	{
        public static string ImgName;
    	protected void Page_Load(object sender, System.EventArgs e)
		{
            //权限检查
            HttpCookie cookie = Request.Cookies["norkeyangjian"];
            if (cookie == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }

            if( !IsPostBack )
            {
                getCategory();//绑定类别下拉列表              
                if( Request.QueryString["id"] == null)
                {
                    Response.Write("没有这个产品");
                    Response.End();
                }             
                string strID = Request.QueryString["id"].ToString().Trim();
                if( strID == String.Empty )
                {
                    strID = "-1";
                }
                read();
            }

           
		}
        private void read()
        {
            DBConn myDB = new DBConn();
            string sql = "select * from Products where uname='" + Request.Cookies["norkeyangjian"].Value+ "' and pid=" + Request.QueryString["id"].ToString();
            SqlDataReader dr = myDB.getDataReader(sql);
            if (dr.Read())
            {
                txtName.Text = dr["PName"].ToString();
                ddlCategory.SelectedIndex = Convert.ToInt32(dr["cid"].ToString());
                txtCPrice.Text = dr["PNPrice"].ToString();
                txtFPrice.Text = dr["pggxh"].ToString();
                TextBox2.Text = dr["PStock"].ToString();
                TextBox1.Text = dr["PBewrite"].ToString();
                ImgName = dr["PPicture"].ToString();
            }
            dr.Close();           
        }
        private void getCategory()//绑定类别下拉列表
        {
            DBConn myDB = new DBConn();
            string sql="select * from Category";
            ddlCategory.DataSource  = myDB.getDataReader(sql);
            ddlCategory.DataTextField = "CName";
            ddlCategory.DataValueField = "CID";
            ddlCategory.DataBind();
            myDB.Close();
        }
     
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

        }
		#endregion


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strName = txtName.Text.Trim();
            string strCID = ddlCategory.SelectedValue;
            string strCPrice = txtCPrice.Text.Trim();
            string strFPrice = txtFPrice.Text.Trim();
            string strID = Request.QueryString["id"].ToString();
            try
            {              
                    if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
                    {
                        Response.Write("<Script>alert('上传的图片格式必须为gif或jpg!!')</Script>");
                        return;
                    }
                    if (uploadFile.PostedFile.FileName.Trim() != String.Empty)
                    {
                        if (ImgName != "showimg.gif")
                        {
                            System.IO.File.Delete(base.Server.MapPath(".\\UPloadImg\\") + ImgName);
                        }
                        string nam = uploadFile.PostedFile.FileName;
                        //取得文件名(抱括路径)里最后一个"."的索引
                        int i = nam.LastIndexOf(".");
                        //取得文件扩展名
                        string newext = nam.Substring(i);
                        //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                        DateTime now = DateTime.Now;
                        string newname = now.DayOfYear.ToString() + uploadFile.PostedFile.ContentLength.ToString();
                        uploadFile.PostedFile.SaveAs(Server.MapPath(".\\UPloadImg\\" + newname + newext));
                        string imgname = newname + newext;
                        string sql;
                        sql = "update Products set PName='" + strName + "',CID='" + strCID + "',PNPrice='" + strCPrice + "',pggxh='" + strFPrice + "',PBewrite='" + TextBox1.Text.Trim() + "',PStock='" + TextBox2.Text.Trim() + "',PPicture='" + imgname + "' where uname='" + Request.Cookies["norkeyangjian"].Value + "' and pid=" + strID;
                        DBConn myDB = new DBConn();
                        myDB.Update(sql);
                        myDB.Close();
                        Response.Redirect("productalter.aspx");                   
                }
                else
                {
                    string sql;
                    sql = "update Products set PName='" + strName + "',CID='" + strCID + "',PNPrice='" + strCPrice + "',pggxh='" + strFPrice + "',PBewrite='" + TextBox1.Text.Trim() + "',PStock='" + TextBox2.Text.Trim() + "' where uname='" + Request.Cookies["norkeyangjian"].Value + "' and pid=" + strID;
                    DBConn myDB = new DBConn();
                    myDB.Update(sql);
                    myDB.Close();
                    Response.Redirect("productalter.aspx");

                }
            }
            catch
            {               
            }
        }
      
        protected void btnReset_Click(object sender, EventArgs e)
        {
           

        }
}
}
