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_Inputproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
            BindToDrp();
        }
    }
    //绑定下拉列表
    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()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "SELECT ProductID,Title FROM Product WHERE Folder=1 ORDER BY Showorder DESC";
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        SqlDataReader dr = null;
        try
        {
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    //插入产品
    public int Insertproduct(int nFolder, int nParrentID, string sTitle, string sContent, string sImgurl)
    {
        int nResult = -1;
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "Insert INTO Product(Folder,ParrentID,Title,Content,Imgurl,Createdate) VALUES(" +
              nFolder + "," +
              nParrentID + ",'" +
              sTitle + "','" +
              sContent + "','" +
              sImgurl + "',GetDate())";
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            nResult = myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return nResult;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (imageUpload.PostedFile.FileName == "")
            {
                Response.Write("<Script>alert('请先选择上传的图片')</Script>");
                return;
            }
            else
            {
                string year = System.DateTime.Now.Year.ToString();
                string month = System.DateTime.Now.Month.ToString();
                string day = System.DateTime.Now.Day.ToString();
                string hour = System.DateTime.Now.Hour.ToString();
                string minute = System.DateTime.Now.Minute.ToString();
                string second = System.DateTime.Now.Second.ToString();
                string xfilename = "X" + year + month + day + hour + minute + second;
                string dfilename = "D" + year + month + day + hour + minute + second;

                string filePath = imageUpload.PostedFile.FileName;
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1);
                string serverpath1 = Server.MapPath(@"~\Uploadpics\") + xfilename + "." + fileEx;
                string serverpath2 = Server.MapPath(@"~\Uploadpics\productpics\") + dfilename + "." + fileEx;


                //判断图片格式
                if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif" || fileEx == "jpeg")
                {

                    System.Drawing.Image image, newimage;
                    //保存大图
                    imageUpload.SaveAs(serverpath2);
                    //生成略图
                    image = System.Drawing.Image.FromFile(serverpath2);
                    System.Drawing.Image.GetThumbnailImageAbort callb = null;
                    newimage = image.GetThumbnailImage(120, 85, callb, new System.IntPtr());
                    //把略图保存到指定的虚拟路径
                    newimage.Save(serverpath1);
                    //释放image对象占用资源
                    image.Dispose();
                    newimage.Dispose();
                    lblImage.Text = "<img src='../Uploadpics/" + xfilename + "." + fileEx + "' width='120' height='85'/>";
                    lblImgurl.Text = xfilename + "." + fileEx;

                }
                else
                {
                    Response.Write("<Script>alert('图片格式不正确')</Script>");
                }
            }
        }
        catch
        {
            Response.Write("<Script>alert('上传失败')</Script>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int nResult = -1;
        int folder = 0;
        int parrentID = Int32.Parse(DrpLeibie.SelectedValue.ToString());
        string title = txtName.Text.ToString();
        string content = FreeTextBox1.Text.ToString();
        string imgurl = lblImgurl.Text.ToString();
        if (title.Trim().Length < 1 || content.Length < 1)
        {
            Response.Write("<Script>alert('标题和内容不能为空！')</Script>");
            return;
        }
        nResult = Insertproduct(folder, parrentID, title, content, imgurl);
        if (nResult != 1)
        {
            Response.Write("<Script>alert('发布产品失败')</Script>");
            return;
        }
        lblError.Text = "发布成功！";
        //发布后清空
        txtName.Text = "";
        FreeTextBox1.Text = "";
        DrpLeibie.SelectedIndex = 0;
        lblImage.Text = "";
        lblImgurl.Text = "";
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
