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.Text.RegularExpressions;

public partial class SYSadmin_Products_RelatedProduct : System.Web.UI.Page
{    
   
    public string sqlwhere = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        this.Label3.Text =Request.QueryString["tit"].ToString()+"--商品管理";
        Session["revalue"] = "";
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {    
            
            Session["gid"] = Request.QueryString["gid"].ToString().Trim();
            SqlConnection reconn = CDatabase.wtoconn();
            reconn.Open();
            SqlCommand resmd = new SqlCommand("select pidall from Package where id=" + Request.QueryString["gid"], reconn);
            this.reval.Value = Convert.ToString(resmd.ExecuteScalar());
            Session["revalue"] = reval.Value;
            reconn.Close();
            Doadmin.ddldatabind(BigProduct, "select * from BigProductCategory where  id<>10000034 and  id<>10000043 order by Sort", "BigCategory", "id");
            Doadmin.ddldatabind(smalllb, "select * from SmallProductCategory where BigCategoryId=" + BigProduct.SelectedValue.ToString().Trim(), "SmallCategory", "id");
            #region 快捷搜索
            switch (this.sql.SelectedValue.Trim())
            {
                case "allpro":
                    databind("high");
                    break;
                case "all":
                    sqlwhere = "all";
                    databind("all");
                    break;
                case "new":
                    sqlwhere = "new";
                    databind("new");
                    break;
                case "tj":
                    sqlwhere = "tj";
                    databind("tj");
                    break;
                case "tjt":
                    sqlwhere = "tjt";
                    databind("tjt");
                    break;
                case "hot":
                    sqlwhere = "hot";
                    databind("hot");
                    break;
                case "fping":
                    sqlwhere = "fping";
                    databind("fping");
                    break;
                case "nofp":
                    sqlwhere = "nofp";
                    databind("nofp");
                    break;
            }
            #endregion
        }
        if (Request.QueryString["id"] != null & Request.QueryString["type"] != null)
        {

                string type = Request.QueryString["type"];
                string sql1 = "";
                LinkButton l1;
                switch (type)
                {
                    case "one":    
                        if ((this.reval.Value.Length - this.reval.Value.Replace(Request.QueryString["id"].ToString(), String.Empty).Length)==0)
                        {
                        this.reval.Value = reval.Value.ToString().Trim() + Request.QueryString["id"] + "$";
                        Session["revalue"] = this.reval.Value;
                        }
                        break;
                    case "can":
                        this.reval.Value = (reval.Value.ToString().Replace(Request.QueryString["id"] + "$", ""));
                        Session["revalue"] = this.reval.Value;
                        break;
                }

            #region 快捷搜索
            switch (this.sql.SelectedValue.Trim())
            {
                case "allpro":
                    databind("high");
                    break;
                case "all":
                    sqlwhere = "all";
                    databind("all");
                    break;
                case "new":
                    sqlwhere = "new";
                    databind("new");
                    break;
                case "tj":
                    sqlwhere = "tj";
                    databind("tj");
                    break;
                case "tjt":
                    sqlwhere = "tjt";
                    databind("tjt");
                    break;
                case "hot":
                    sqlwhere = "hot";
                    databind("hot");
                    break;
                case "fping":
                    sqlwhere = "fping";
                    databind("fping");
                    break;
                case "nofp":
                    sqlwhere = "nofp";
                    databind("nofp");
                    break;
            }
            #endregion
        }
    }
    public void databind(string whe)
    {
        string sqlstr = "";
        switch (whe)
        {
            case "all":
                sqlstr = "select * from ProductInfo where BigProductID<>10000034 and  BigProductID<>10000043  order by AddDate desc";
                break;
            case "new":
                sqlstr = "select * from ProductInfo where IsNew='y' and  BigProductID<>10000034 and  BigProductID<>10000043   order by AddDate desc";
                break;
            case "tj":
                sqlstr = "select * from ProductInfo where IsSpecials='y'  and  BigProductID<>10000034 and  BigProductID<>10000043   order by AddDate desc";
                break;
            case "tjt":
                sqlstr = "select * from ProductInfo where IsRecommended='y' and  BigProductID<>10000034 and  BigProductID<>10000043    order by AddDate desc";
                break;
            case "hot":
                sqlstr = "select * from ProductInfo where IsHot='y' and  BigProductID<>10000034 and  BigProductID<>10000043    order by AddDate desc";
                break;
            case "fping":
                sqlstr = "select * from ProductInfo where ISPublished='y'  and  BigProductID<>10000034 and  BigProductID<>10000043  order by AddDate desc";
                break;
            case "nofp":
                sqlstr = "select * from ProductInfo where ISPublished='n'  and  BigProductID<>10000034 and  BigProductID<>10000043   order by AddDate desc";
                break;
            case "high":
                sqlstr = "select * from  ProductInfo where BigProductID=" + this.BigProduct.SelectedValue.Trim() + "  and  BigProductID<>10000034 and  BigProductID<>10000043   and SmallProductId=" + this.smalllb.SelectedValue.Trim() + " and Products like '%" + this.pro.Text.Trim() + "%' order by AddDate desc";
                break;
        }
        Label isfb, isnew, ishot, isT, isTprice;
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sdr = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        sdr.Fill(ds, "cu");
        this.Productmag.DataSource = ds;
        this.Productmag.DataKeyNames = new string[] { "id" };
        this.Productmag.DataBind();
        this.Label1.Text = "当前商品数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.Productmag.PageCount + "</b></font>页显示.当前为<font color='red'><b>" + Convert.ToString(Convert.ToInt32(this.Productmag.PageIndex.ToString()) + 1) + "</b></font>页";
        if (Productmag.Rows.Count == 0)
        {
            this.mslable.Visible = true;
        }
        else
        {
            this.mslable.Visible = false;
        }
        //关联
        string txt = this.reval.Value;
        int k;
        int c1 = 0;
        LinkButton li1;
        for (int j = 0; j <= Productmag.Rows.Count - 1; j++)
        {
            //c1 = txt.IndexOf(Productmag.DataKeys[j].Value.ToString());//系统自带
            if (txt.Length - txt.Replace(Productmag.DataKeys[j].Value.ToString(), String.Empty).Length > 0)
            {
                li1 = (LinkButton)Productmag.Rows[j].FindControl("onere");
                li1.Visible = false;
                li1 = (LinkButton)Productmag.Rows[j].FindControl("canre");
                li1.Visible = true;
            }
        }
        //END关联
        //图片显示[功能图片]
        SqlCommand smd2;
        SqlDataReader sdr2;
        Image img;
        LinkButton l1;
        Label ll1;
        HyperLink hhy;
        string txtr = "";
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            DataRowView mydrv = ds.Tables["cu"].DefaultView[i];

            smd2 = new SqlCommand("select ISPublished,IsNew,IsHot,IsRecommended,IsSpecials,ThumbnailsPic,id,Products,ProductsNo from ProductInfo where id=" + Productmag.DataKeys[i].Value, conn);
            sdr2 = smd2.ExecuteReader();
            sdr2.Read();
            img = (Image)Productmag.Rows[i].FindControl("imgpic");
            img.ImageUrl = "~/upfile/" + sdr2["ThumbnailsPic"];
            img.Height = 30;
            img.Width = 50;
            txtr = "/upfile/" + sdr2["ThumbnailsPic"];
            img.Attributes.Add("onmousemove", "ab('" + txtr + "')");
            img.Attributes.Add("onmouseout", "ac()");
            //是否发布
            if (sdr2.GetString(0) == "y")
            {
                isfb = (Label)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "√";
            }
            if (sdr2.GetString(0) == "n")
            {
                isfb = (Label)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "×";
            }
            //最新
            if (sdr2.GetString(1) == "y")
            {
                isnew = (Label)Productmag.Rows[i].FindControl("isnew");
                isnew.Text = "√";
            }
            if (sdr2.GetString(1) == "n")
            {
                isnew = (Label)Productmag.Rows[i].FindControl("isnew");
                isnew.Text = "×";
            }
            //最热
            if (sdr2.GetString(2) == "y")
            {
                ishot = (Label)Productmag.Rows[i].FindControl("ishot");
                ishot.Text = "√";
            }
            if (sdr2.GetString(2) == "n")
            {
                ishot = (Label)Productmag.Rows[i].FindControl("ishot");
                ishot.Text = "×";
            }

            //推荐
            if (sdr2.GetString(3) == "y")
            {
                isT = (Label)Productmag.Rows[i].FindControl("isT");
                isT.Text = "√";
            }
            if (sdr2.GetString(3) == "n")
            {
                isT = (Label)Productmag.Rows[i].FindControl("isT");
                isT.Text = "×";
            }
            //特价
            if (sdr2.GetString(4) == "y")
            {
                isTprice = (Label)Productmag.Rows[i].FindControl("isTprice");
                isTprice.Text = "√";

            }
            if (sdr2.GetString(4) == "n")
            {
                isTprice = (Label)Productmag.Rows[i].FindControl("isTprice");
                isTprice.Text = "×";
            }
            hhy = (HyperLink)Productmag.Rows[i].FindControl("HyperLink1");
            hhy.Text = SubStr(sdr2["Products"].ToString(), 15);
            ll1 = (Label)Productmag.Rows[i].FindControl("Label1");
            ll1.Text = SubStr(sdr2["ProductsNo"].ToString(), 15);
            l1 = (LinkButton)Productmag.Rows[i].FindControl("onere");
            l1.PostBackUrl = "PackagePeidt.aspx?type=one&tit="+Request.QueryString["tit"]+"&id=" + sdr2["id"];
            l1 = (LinkButton)Productmag.Rows[i].FindControl("canre");
            l1.PostBackUrl = "PackagePeidt.aspx?type=can&tit=" + Request.QueryString["tit"] + "&id=" + sdr2["id"];
            sdr2.Close();

        }
        conn.Close();
    }
    protected void Productmag_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < Productmag.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion
    }
    protected void Productmag_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Productmag.PageIndex = e.NewPageIndex;
        #region 快捷搜索
        switch (this.sql.SelectedValue.Trim())
        {
            case "allpro":
                databind("high");
                break;
            case "all":
                sqlwhere = "all";
                databind("all");
                break;
            case "new":
                sqlwhere = "new";
                databind("new");
                break;
            case "tj":
                sqlwhere = "tj";
                databind("tj");
                break;
            case "tjt":
                sqlwhere = "tjt";
                databind("tjt");
                break;
            case "hot":
                sqlwhere = "hot";
                databind("hot");
                break;
            case "fping":
                sqlwhere = "fping";
                databind("fping");
                break;
            case "nofp":
                sqlwhere = "nofp";
                databind("nofp");
                break;
        }
        #endregion
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        #region 快捷搜索
        switch (this.sql.SelectedValue.Trim())
        {
            case "allpro":
                databind("high");
                break;
            case "all":
                sqlwhere = "all";
                databind("all");
                break;
            case "new":
                sqlwhere = "new";
                databind("new");
                break;
            case "tj":
                sqlwhere = "tj";
                databind("tj");
                break;
            case "tjt":
                sqlwhere = "tjt";
                databind("tjt");
                break;
            case "hot":
                sqlwhere = "hot";
                databind("hot");
                break;
            case "fping":
                sqlwhere = "fping";
                databind("fping");
                break;
            case "nofp":
                sqlwhere = "nofp";
                databind("nofp");
                break;
        }
        #endregion
    }
    protected void BigProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        Doadmin.ddldatabind(smalllb, "select * from SmallProductCategory where BigCategoryId=" + BigProduct.SelectedValue.ToString().Trim(), "SmallCategory", "id");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        this.sql.SelectedValue = "allpro";
        databind("high");
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        string sy ="...";
        sNewStr = sNewStr + sy;
        return sNewStr;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (Doadmin.Exec("update Package set pidall='"+this.reval.Value.ToString().Trim()+"' where id="+Session["gid"].ToString()))
        {
            Response.Write("<script>alert('操作成功');window.location.href='Package.aspx'</script>"); 
        }
        else
        {
            Response.Write("<script>alert('此次操作不成功')</script>"); 
        }
    }
}
