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

namespace PanRoor
{
    public partial class WTOSYSadmin_Products_Products : XMLX
    {
        //图片上传
        string picmm="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["WtoRightName"] == null)
            {
                Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
            }
            if (!IsPostBack)
            {
                switch (Request.QueryString["do"])
                {
                    case "del":
                        Doadmin.Exec("delete from Caseinfo where id=" + Request.QueryString["id"].Trim());
                        Response.Redirect("../Case/Casemag.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 scd = new SqlCommand("select * from Caseinfo where id=" + id, connfz);
                        SqlDataReader sdr = scd.ExecuteReader();
                        sdr.Read();
                        Products = sdr.GetString(3).ToString().Trim();
                        this.Products.Text = Products;
                        ProductsNo = sdr.GetString(4).ToString().Trim();
                        this.ProductsNo.Text = ProductsNo;
                        ISPublished = sdr.GetString(5).ToString().Trim();
                        if (ISPublished == "y")
                        {
                            this.open.Checked = true;
                        }
                        else
                        {
                            this.close.Checked = true;
                        }

                        //加入修改值
                        Doadmin.ddldatabind(BigProduct, "select * from BigCaseCategory order by Sort", "BigCategory", "id");
                        
                        this.BigProduct.SelectedValue = (Convert.ToString(sdr.GetInt32(1))).Trim();
                        
                        Description = sdr.GetString(6).ToString().Trim();
                        this.Description.Text = Description;
                        this.Content.Text = Server.HtmlDecode(sdr.GetString(15).ToString().Trim());
                        //第二个标签

                        Doadmin.ddldatabind(xlddl, "select * from SmallCaseCategory where BigCategoryId=" + this.BigProduct.SelectedValue.Trim(), "SmallCategory","id");
                
                        this.Slab.Text = sdr.GetString(7);
                        this.piclable.Text = sdr.GetString(8);

                        if (sdr.GetString(10).Trim() == "nopic.jpg")
                        {
                            this.RemotePicURLt.Text = "";
                        }
                        else
                        {
                            this.RemotePicURLt.Text = sdr.GetString(10).Trim();
                        }
                        if (sdr.GetString(11).Trim() == "nopic.jpg")
                        {
                            this.RemotePicURLc.Text = "";
                        }
                        else
                        {
                            this.RemotePicURLc.Text = sdr.GetString(11).Trim();
                        }
                        //第四标签
                        this.PageTitle.Text = sdr.GetString(12).Trim();
                        this.META_KEYWORDS.Text = sdr.GetString(13).Trim();
                        this.META_DESCRIPTION.Text = sdr.GetString(14).Trim();
                        sdr.Close();
                        connfz.Close();
                        #endregion
                        break;

                }



            }

        }

        //保存
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            //取值------------------------------------------------------------------------------------------------------------------
            //第一标签[基本信息] Products|产品名称$　ProductsNo|货号 ISPublished|open/close|是否发布 ISpf|批发 Brand|品牌 Description|简单描述
            //Unit|计量单位 Unitweight|单位重量 Stockvolume|库存量 StockWarning|isy/isns|库存警告 IsNew|最新 IsHot|最热 IsRecommended|推荐 IsSpecials|特价
            //Nature|商品属性 Content|详细信息

            #region 第一标签取值
            string Products, ProductsNo, ISPublished, ISpf, Brand, Description, Unit, Unitweight, Stockvolume, StockWarning, IsNew, IsHot, IsRecommended, IsSpecials, Nature, Content;
            Products = Request["Products"].ToString().Trim();
            ProductsNo = Request["ProductsNo"].ToString().Trim();
            if (this.open.Checked)
            {
                ISPublished = "y";
            }
            else
            {
                ISPublished = "n";
            }
            Description = Request["Description"].ToString().Trim();


            Content = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));
            #endregion


            //第二标签取值开始
            //BigProduct|商品大类 SmlProduct|小类   
            #region 第二标签取值开始
            string BigProductID, SmallProductId, SalePrice, Marketprice, CostPrice, CjVipPrice, MiddleVIPPrice, HiVipPrice, VIPPrice,strall;
            BigProductID = this.BigProduct.SelectedValue.ToString().Trim();
            SmallProductId = this.xlddl.SelectedValue.ToString().Trim();
            //以上是商品表中内容.属性表还未填
            #endregion

            #region 第三标签取值开始------------------------------------------------------------------------------------------------------------------
            //  isslpic|开启本地上传缩略图 ThumbnailsPic|本地上传缩略图 fup_img|清晰图 RemotePicURLt|缩略图远程链接地址 RemotePicURLc|清晰图远程链接地址
            //更多图片:存放在商品图片更多表中[取数组值]
            //OriginalPic|原始图

            string fpath, ftype, fname, dot="";
            string ThumbnailsPic, ClearPic, OriginalPic, RemotePicURLt, RemotePicURLc="";
            string isSL = "";


                //取缩略图片
                string picPath = Server.MapPath("~/upfile");
                HttpPostedFile file = this.ThumbnailsPic.PostedFile;
                
                string hz = this.ThumbnailsPic.PostedFile.FileName;
                if (hz != "")
                {
                    string S_hz = (hz.Substring(hz.LastIndexOf(".") + 1)).ToLower().Trim();
                    string stpt = "";
                    string stfilename = "";
                    if (S_hz == "jpg" || S_hz == "gif")
                    {
                        stfilename = DateTime.Now.ToString("yyyyMMddHHmmssffffMM");
                        stpt = Server.MapPath("~/upfile/") + stfilename + "." + S_hz;
                        file.SaveAs(stpt);

                    }
                    else
                    {
                        Response.Write("<script>alert('文件格式错误！');</script>");
                        Response.End();
                    }

                    ThumbnailsPic = stfilename + "." + S_hz;
                    isSL = "Y";
                }
                else
                {
                    ThumbnailsPic = this.Slab.Text;
                }

            #region 图片上传
            fname = this.fup_imgb.FileName;
            fpath = this.fup_imgb.PostedFile.FileName;
            ftype = this.fup_imgb.PostedFile.ContentType;
            string[] type = ftype.Split('/');
            string[] fn = fname.Split('.');
            string S_hC = (fpath.Substring(fpath.LastIndexOf(".") + 1)).ToLower().Trim();

            if (S_hC != "jpg" & S_hC!="" & S_hC!="gif" &S_hC!="bmp"&S_hC!="png")
            {
                Response.Write("<script>alert('文件格式错误！');window.location.href('ProductsEdit.aspx?id=" + Request.QueryString["id"].Trim() + "')</script>");
            }
            else
            {
                if (fpath != "")
                {
                    dot = "." + fn[1].ToString();
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                    string pt = Server.MapPath("~/upfile/") + filename + dot;
                    fup_imgb.SaveAs(pt);
                    fup_imgb.Dispose();
                    if (XMLRead("NoAndOff") == "true")
                    {
                        picmark.ImageModification wm = new ImageModification();
                        wm.DrawedImagePath = Server.MapPath("~/SYSadmin/Products/Watermark/logo_cn.gif"); //水印图片
                        wm.ModifyImagePath = pt;    //图片的路径
                        wm.RightSpace = Convert.ToInt32(XMLRead("RightSpace"));          //水印位置 宽 相对图片右下角
                        wm.BottoamSpace = Convert.ToInt32(XMLRead("BottoamSpace"));       //水印位置 高
                        wm.LucencyPercent = Convert.ToInt32(XMLRead("LucencyPercent"));      //透明度
                        wm.OutPath = Server.MapPath("~/upfile/") + filename + "_new" + dot;
                        wm.DrawImage();

                        if (File.Exists(pt))
                        {
                            File.Delete(pt);
                        }

                        ClearPic = filename + "_new" + dot;
                    }
                    else
                    {
                        ClearPic = filename+ dot;
                    }


                    OriginalPic = filename + dot;
                    if (isSL == "N")
                    {
                        ThumbnailsPic = filename + dot;
                    }
                }
                else
                {
                    ClearPic = this.piclable.Text;
                    OriginalPic = ClearPic.Substring(0,18).ToString().Trim()+ ".jpg";
                }
            #endregion
                //接着取值
                RemotePicURLt = Request["RemotePicURLt"].ToString().Trim();
                RemotePicURLc = Request["RemotePicURLc"].ToString().Trim();
                if (RemotePicURLt == "")
                {
                    RemotePicURLt = "nopic.jpg";
                }
                if (RemotePicURLc == "")
                {
                    RemotePicURLc = "nopic.jpg";

                }
//暂定为无图，如果有保留原始图，则为filename直接加上.jpg就OK了
                //更多图片上传取值开始
            
                                                        //图片上传完成

    
              #endregion
                                                    //第四标签[暂留]---------------------------------------------------------------------------------------------------------              

               #region 第五标签---------------------------------------------------------------------------------------------------------------
                                                        string PageTitle, META_KEYWORDS, META_DESCRIPTION;
                                                        PageTitle = Request["PageTitle"].ToString();
                                                        META_KEYWORDS = Request["META_KEYWORDS"].ToString();
                                                        META_DESCRIPTION = Request["META_DESCRIPTION"].ToString();
                                                        #endregion

               #region 整合上传
                                                        string sqlstrWTO;
                                                        sqlstrWTO = "update Caseinfo set BigProductID=" + BigProductID + ",SmallProductId=" + SmallProductId + ",Products='" + Products + "',ProductsNo='" + ProductsNo + "',ISPublished='" + ISPublished + "',Description='" + Description + "'";
                                                        sqlstrWTO += ",ThumbnailsPic='" + ThumbnailsPic + "',ClearPic='" + ClearPic + "',OriginalPic='" + OriginalPic + "',RemotePicURLt='" + RemotePicURLt + "',RemotePicURLc='" + RemotePicURLc + "',PageTitle='" + PageTitle + "',META_KEYWORDS='" + META_KEYWORDS + "',META_DESCRIPTION='" + META_DESCRIPTION + "',Content='" + Content + "',AddDate='" + System.DateTime.Now.ToString() + "',Language='china' where id=" +Request.QueryString["id"].Trim();
                                                         
                                                        
                                                        if (Doadmin.Exec(sqlstrWTO))
                                                        {
                                                            Response.Write("<script>alert('修改成功');location.href('../Case/Casemag.aspx')</script>");
                                                        }
                                                        else
                                                        {
                                                            Response.Write("<script>alert('修改失败')</script>");
                                                            Response.Write(sqlstrWTO);
                                                        }
                                                        #endregion



                 }
                        
              }
        protected void BigProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            Doadmin.ddldatabind(xlddl, "select * from SmallCaseCategory where BigCategoryId=" + this.BigProduct.SelectedValue.Trim(), "SmallCategory", "id");
        }
}
 }
