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 SystemManage_AddProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["username"]) == "")
        {
            Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = Request["name"];
        string productType = Request["productType"];
        string productFormat = Request["productFormat"];
        string price = Request["price"];
        string mome = Request["mome"];

        if (Tools.validate(name))
        {
            Response.Write("<script>alert('产品名中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(productType))
        {
            Response.Write("<script>alert('产品类型名中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(productFormat))
        {
            Response.Write("<script>alert('产品规格名中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(price))
        {
            Response.Write("<script>alert('价格中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(mome))
        {
            Response.Write("<script>alert('备注中不能含有非法字符');histort.back()</script>");
        }
        string sqlcmd1 = "select count(*) as ff from PRODUCT where name='" + name + "'";
        using (SqlConnection sqlconn = new SqlConnection(Tools.strConn))
        {
            sqlconn.Open();
            SqlCommand cmd = new SqlCommand(sqlcmd1, sqlconn);
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count > 0)
            {
                Response.Write("<script>alert('产品名已经存在，请重新输入！');histort.back()</script>");
                return;
            }
            try
            {
                string sqlcmd2 = "insert into PRODUCT (name,PRODUCT_TYPE,PRODYCT_FORMAT,price,mome)values(@name,@PRODUCT_TYPE,@PRODYCT_FORMAT,@PRICE,@mome)";
                cmd.CommandText = sqlcmd2;
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 30).Value = name;
                cmd.Parameters.Add("@PRODUCT_TYPE", SqlDbType.VarChar, 30).Value = productType;
                cmd.Parameters.Add("@PRODYCT_FORMAT", SqlDbType.VarChar, 30).Value = productFormat;
                cmd.Parameters.Add("@price", SqlDbType.Decimal, 2).Value = price;
                cmd.Parameters.Add("@mome", SqlDbType.VarChar, 30).Value = mome;
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('商品添加成功');location='ProductManage.aspx'</script>");
            }
            catch
            {
                Response.Write("<script>alert('操作失败');location='AddProduct.aspx'</script>");
            }
        }
    }
}
