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;
using System.IO;

public partial class WKadmin_Products_DownEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("/main/error.aspx?err=非法登陆!或系统超时,请确认后重新登录");
        }
        if (!IsPostBack)
        {
            switch (Request.QueryString["do"])
            {
                case "del":
                    Doadmin.Exec("delete from downinfo where id=" + Request.QueryString["id"].Trim());
                    Response.Redirect("downlist.aspx");
                    Response.End();
                    break;
                case "edit":
                    #region 修改初始化值
                    //第一标签复值
                    string Products, ProductsNo, ISPublished, ISpf, Brandf, Description, Unit, Unitweight, Stockvolume, StockWarning, IsNew, IsHot, IsRecommended, IsSpecials, Nature, Content;
                    string id;
                    id = Request.QueryString["id"].ToString().Trim();
                    SqlConnection connfz = CDatabase.wtoconn();
                    connfz.Open();
                    SqlCommand smd = new SqlCommand("select * from downinfo where id=" + id, connfz);
                    SqlDataReader sdr = smd.ExecuteReader();
                    sdr.Read();
                    Doadmin.ddldatabind(BigProduct, "select * from DownCategory order by Sort", "BigCategory", "id");
                    this.BigProduct.SelectedValue =Convert.ToString(sdr.GetInt32(1));
                    Doadmin.ddldatabind(smalllb, "select * from SmallDownCategory where BigCategoryId=" + BigProduct.SelectedValue.ToString().Trim(), "SmallCategory", "id");
                    this.smalllb.SelectedValue =Convert.ToString(sdr.GetInt32(2));
                    this.Products.Text=sdr.GetString(3);
                    this.ProductsNo.Text =sdr.GetString(4);
                    if (sdr.GetString(5).Trim() == "y")
                    {
                        this.open.Checked = true;
                    }
                    else
                    {
                        this.close.Checked = true;
                    }
                    this.Description.Text=sdr.GetString(6);
                    this.lblpic.Text = sdr.GetString(7);
                    this.lblfile.Text = sdr.GetString(8);
                    this.lblfile.NavigateUrl = "~/upfile/upload/" + sdr.GetString(8);
                    this.lblfile.Target = "_blank";
                    this.Content.Text = Server.HtmlDecode(sdr.GetString(10));
                    this.lan.SelectedValue = sdr.GetString(12).ToString().Trim();
                    this.lblsize.Text = Convert.ToString(sdr.GetInt32(9));
                    connfz.Close();
                    break;
                    #endregion
            }



        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string hz = this.File1.PostedFile.FileName;
        string picname = "", filename = ""; 
        string stfilename = "";
        long filesize1 = 0;
        if (hz != "")
        {
            string S_hz = (hz.Substring(hz.LastIndexOf(".") + 1)).ToLower().Trim();
            string stpt = "";
           
            
            
            if (S_hz == "jpg" || S_hz == "gif")
            {
                stfilename = DateTime.Now.ToString("yyyyMMddHHmmssffffMM");
                stpt = Server.MapPath("~/upfile/") + stfilename + "." + S_hz;
                File1.SaveAs(stpt);
                picname = stfilename + "." + S_hz;

            }
            else
            {
                Response.Write("<script language='javascript'>alert('图片上传格式有误，请确认后再上传!')</script>");
            }
        }
        else
        {
            picname = this.lblpic.Text;
        }
        string hzf = this.File2.PostedFile.FileName;
        string stfilename1 = "";
        if (hzf != "")
        {
            string U_f = (hzf.Substring(hzf.LastIndexOf(".") + 1)).ToLower().Trim();
            string stpt1 = "";
            if (U_f == "rar" || U_f == "zip" || U_f == "doc" || U_f == "pdf" || U_f == "psd" || U_f == "xls")
            {
                stfilename1 = DateTime.Now.ToString("yyyyMMddHHmmssffffMM");
                stpt1 = Server.MapPath("~/upfile/upload/") + stfilename1 + "." + U_f;
                File2.SaveAs(stpt1);
                filename = stfilename1 + "." + U_f;
                FileInfo fo = new FileInfo(stpt1);
                filesize1 = fo.Length;
            }
            else
            {
                
                Response.Write("<script language='javascript'>alert('上传格式有误，请确认后再上传!')</script>");
            }
        }
        else
        {
            filesize1 =Convert.ToUInt32(this.lblsize.Text);
            filename = lblfile.Text;
        }
        string isfp = "";
        if (this.open.Checked)
        {
            isfp = "y";
        }
        else
        {
            isfp = "n";
        }
        string sql = "update Downinfo set BigProductID=" + this.BigProduct.SelectedValue.ToString() + ",SmallProductId=" + this.smalllb.SelectedValue.ToString() + ",Products='" + this.Products.Text.ToString() + "',ProductsNo='" + this.ProductsNo.Text + "',ISPublished='" + isfp + "',Description='" + this.Description.Text + "',ThumbnailsPic='" + picname + "',filename='" + filename + "',filesize=" + filesize1 + ",Content='" + Server.HtmlEncode(this.Content.Text.ToString()) + "',AddDate='" + DateTime.Now.ToString() + "',Language='" + this.lan.SelectedValue.ToString() + "' where id="+Request.QueryString["id"].Trim();
        if (Doadmin.Exec(sql))
        {
            Response.Write("<script language='javascript'>alert('提交成功');window.location.href='DownList.aspx'</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('提交失败');'</script>");
        }
        
    }
    protected void BigProduct_SelectedIndexChanged1(object sender, EventArgs e)
    {
        Doadmin.ddldatabind(smalllb, "select * from SmallDownCategory where BigCategoryId=" + BigProduct.SelectedValue.ToString().Trim(), "SmallCategory", "id");

    }
}
