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
    {
        //图片上传

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["WtoRightName"] == null)
            {
                Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
            }
            if (!IsPostBack)
            {
                Doadmin.ddldatabind(BigProduct, "select * from BigCaseCategory order by Sort", "BigCategory", "id");
                Doadmin.ddldatabind(smallid, "select * from SmallCaseCategory where BigCategoryId=" + this.BigProduct.SelectedValue.Trim() + " order by Sort", "SmallCategory", "id");
            }

        }

        //保存
        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";
            }

            ISpf ="y";
          
            Description = Request["Description"].ToString().Trim();
            Content = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));
            #endregion

            //第二标签取值开始
            //BigProduct|商品大类 SmlProduct|小类   
            //商品属性值确认表 ddlFL|属性列表大类 小类数组值
            //sale|销售价 CountALL|确认销售价  Sprice|市场价 CostPrice|成本价 CjVipPrice|初级会员价 MiddleVIPPrice|中级会员价 HiVipPrice|高级会员价
            //VIPPrice|VIP会员价
            #region 第二标签取值开始
            string BigProductID, SmallProductId, SalePrice, Marketprice, CostPrice, CjVipPrice, MiddleVIPPrice, HiVipPrice, VIPPrice,strall,sale;
            BigProductID = this.BigProduct.SelectedValue.ToString().Trim();
            SmallProductId =this.smallid.Text;
            //以上是商品表中内容.属性表还未填
            #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("文件格式错误！");
                        Response.End();
                    }

                    ThumbnailsPic = stfilename + "." + S_hz;
                    isSL = "Y";
                }
                else
                {
                    ThumbnailsPic = "nopic.jpg";
                }

            #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('.');
            dot = "." + fn[1].ToString();
            if (type[0].ToString() != "image")
            {
                Response.Write("文件格式错误！");
            }
            else
            {
                string filename = DateTime.Now.ToString("yyyyMMddHHffmmssffffss");
                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("~/WTOSYSadmin/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;
                }
                
            #endregion

                //接着取值
                RemotePicURLt = Request["RemotePicURLt"].ToString().Trim();
                RemotePicURLc = Request["RemotePicURLc"].ToString().Trim();
                if (RemotePicURLt == "")
                {
                    RemotePicURLt = "nopic.jpg";
                }
                if (RemotePicURLc == "")
                {
                    RemotePicURLc = "nopic.jpg";

                }
                OriginalPic = "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 = "insert into Caseinfo(BigProductID,SmallProductId,Products,ProductsNo,ISPublished,Description,ThumbnailsPic,ClearPic,OriginalPic,RemotePicURLt,RemotePicURLc,PageTitle,META_KEYWORDS,META_DESCRIPTION,Content,AddDate,Language)";
                                                        sqlstrWTO += " values (" + BigProductID + "," + SmallProductId + ",'" + Products + "','" + ProductsNo + "','" + ISPublished + "','" + Description + "'";
                                                        sqlstrWTO += ",'" + ThumbnailsPic + "','" + ClearPic + "','" + OriginalPic + "','" + RemotePicURLt + "','" + RemotePicURLc + "','" + PageTitle + "','" + META_KEYWORDS + "','" + META_DESCRIPTION + "','" + Content + "','"+System.DateTime.Now.ToString()+"','china')";
                                                        if (Doadmin.Exec(sqlstrWTO))
                                                        {
                                                            Response.Write("<script>alert('添加成功')</script>");
                                                        }
                                                        else
                                                        {

                                                            Response.Write("<script>alert('添加失败')</script>");
                                                        }
                                                        #endregion


                                   
                               
                      }
              }
        #region  验格式
        public bool IsAllowedExtension(HtmlInputFile hifile)
        {
            string strOldFilePath = "", strExtension = "";
            string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
            if (hifile.PostedFile.FileName != string.Empty)
            {
                strOldFilePath = hifile.PostedFile.FileName;
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
                for (int i = 0; i < arrExtension.Length; i++)
                {
                    if (strExtension.Equals(arrExtension[i]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion
        protected void BigProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            Doadmin.ddldatabind(smallid, "select * from SmallCaseCategory where BigCategoryId=" + this.BigProduct.SelectedValue.Trim() + " order by Sort", "SmallCategory", "id");

        }
}

 }
