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;
/*需要完善的地方
 *1.添加之后再回来原来的页面,绑定的值改变了. 通过再次跳转到页面,初始化一下.
 *  应该可以保持下拉框的状态.
 */
public partial class admin_Add_Product : System.Web.UI.Page
{
    BLL.Type_1 t_1 = new BLL.Type_1();
    BLL.UserInfo user = new BLL.UserInfo();
    BLL.Products pro = new BLL.Products();
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxServer.GetNextClass));
        Title = "添加新产品";
        if (user.IsLogin() == true && user.getUserRole() == 2)
        {
            if (!this.IsPostBack)
            {
                this.Bind();
            }
        }
        else
        {
            this.Response.Redirect("Admin_Login.aspx");
        }
    }
    protected void Bind()
    {
        DataTable dt = t_1.type_1list("");
        ddl1.Items.Add(new ListItem("选择一级类别", ""));
        ddl2.Items.Add(new ListItem("选择二级类别", ""));
        foreach (DataRow dr in dt.Rows)
        {
            ddl1.Items.Add(new ListItem(dr["TypeName"].ToString(), dr["TypeID_1"].ToString()));
        }
        ddl1.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'ddl2');");
        ddl2.Attributes.Add("onchange", "setClass(this.options[selectedIndex].value,'HF1');");
        HF1.Value = "";
    }
    
    private void ResetValue()
    {
        foreach (ListItem item in ddl1.Items)
        {
            if (item.Value == ViewState["ddl1"].ToString())
                item.Selected = true;
            else
                item.Selected = false;
        }
    }

    /// <summary>
    /// 提交保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitBt_Click(object sender, EventArgs e)
    {
        if (ddl1.SelectedIndex != 0)
        {
            if ((HF1.Value != "")&&(HF1.Value!="0"))
            {
                string savePath = @"../images/uploadfiles/";
                string savePath1 = @"../images/uploadfiles/smallPic/";
                string fileName = Server.HtmlEncode(this.getimagepath());
                string extension = System.IO.Path.GetExtension(fileName);
                string ImageName = null;
                if (fileName != "nopic.gif")
                {
                    if ((extension == ".jpg") | (extension == ".gif"))
                    {
                        ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + extension;
                        savePath += ImageName;
                        savePath1 += ImageName;
                        savePath = Server.MapPath(savePath);
                        savePath1 = Server.MapPath(savePath1);
                    }
                    else
                    {
                        Response.Write("<script>alert('文件上传格式错误');history.back(1);</script>");
                        Response.End();
                    }
                }
                else 
                {
                    ImageName = fileName;
                }
                int typeid_2 = int.Parse(this.HF1.Value);
                string productname = this.productnameTb.Text.Trim();
                int typeid_1 = int.Parse(this.ddl1.SelectedValue.ToString());
                bool recommended = this.recommendedCB.Checked;
                bool specials = this.specialsCB.Checked;
                Double price = Double.Parse(this.priceTb.Text.Trim());
                Double userprice = Double.Parse(this.userpriceTb.Text.Trim());
                Double specialsprice = Double.Parse(this.specialspriceTb.Text.Trim());
                int pointcount = 0;
                string imagepath = ImageName;
                int count = int.Parse(this.countTb.Text.Trim());
                int sellcount = int.Parse(this.sellcountTb.Text.Trim());
                string description = this.FreeTextBox1.Text;
                if (pro.createproduct(productname, typeid_1, typeid_2, recommended, specials, price, userprice,
                 specialsprice, pointcount, imagepath, count, sellcount, description) == 1)
                {
                    if (fileName != "nopic.gif")
                    {
                        picupload.SaveAs(savePath);
                        //生成小图
                        Tools.ImageClass imageClass = new Tools.ImageClass();
                        imageClass.ShowThumbnail(savePath, savePath1, 200, 153);
                    }
                    Response.Write("<script>alert('添加成功');location.href='Add_Product.aspx';</script>");
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('添加新产品失败!')</script>", false);
                }
            }
            else
            {
                Response.Write("<script>alert('请选择二级类别!');location.href='Add_Product.aspx';</script>");
                //用下面的方法会保存状态.
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('请选择二级类别!')</script>", false);
            }
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('请选择一级类别!')</script>", false);
        }
    }

    private string getimagepath()
    {
        try
        {
            return this.picupload.HasFile ? picupload.FileName : "nopic.gif";
        }
        catch
        {
            return "nopic.gif";
        }
    }
}
