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 WebMain_Product_Search_searchresult : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        if (Request.QueryString["bigid"].ToString() == "0" && Request.QueryString["smallid"].ToString() == "0")
        {
            this.big.Text= "您搜索的产品";
            this.small.Visible = false;
            this.Label1.Visible = false;
        }
        else
        {
            SqlCommand smd1 = new SqlCommand("select BigCategory from BigProductCategory where id=" + Request.QueryString["bigid"].ToString(), conn);
            SqlCommand smd2 = new SqlCommand("select SmallCategory from SmallProductCategory where id=" + Request.QueryString["smallid"].ToString(), conn);
            this.big.Text = Convert.ToString(smd1.ExecuteScalar());
            big.NavigateUrl = "~/china/productPP.aspx?BigID=" + Request.QueryString["bigid"].ToString();
            small.Text = Convert.ToString(smd2.ExecuteScalar());
            small.NavigateUrl = "~/china/product.aspx?SmallId=" + Request.QueryString["smallid"].ToString();
        }
        this.HyperLink2.NavigateUrl = "~/china/searchresult.aspx?pcount=20&bigid=" + Request.QueryString["bigid"].ToString() + "&smallid=" + Request.QueryString["smallid"].ToString() + "&keywords=" + Request.QueryString["keywords"].ToString()+"&";
        this.HyperLink3.NavigateUrl = "~/china/searchresult.aspx?pcount=40&bigid=" + Request.QueryString["bigid"].ToString() + "&smallid=" + Request.QueryString["smallid"].ToString() + "&keywords=" + Request.QueryString["keywords"].ToString() + "&";
        conn.Close();
        this.databind();
        
    }
    public void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sda;
        string wheretxt = "";
        switch (this.DropDownList1.SelectedValue.ToString())
        {
            case "time":
                wheretxt = "order by AddDate";
                break;
            case "timedesc":
                wheretxt = "order by AddDate desc";
                break;
            case "price":
                wheretxt = "order by VIPPrice";
                break;
            case "pricedesc":
                wheretxt = "order by VIPPrice desc";
                break;
        }
        if (Request.QueryString["bigid"] == "0" && Request.QueryString["smallid"] == "0")
        {
            sda = new SqlDataAdapter("select * from ProductInfo  where intj=0 and BigProductID<>10000043 and BigProductID<>10000034 and Products like '%" + Server.UrlDecode(Request.QueryString["keywords"]) + "%' and ISPublished='y' " + wheretxt, conn);
        }
        else
        {
            sda = new SqlDataAdapter("select * from ProductInfo  where intj=0 and BigProductID<>10000043 and BigProductID<>10000034 and BigProductID=" + Request.QueryString["bigid"] + " and SmallProductId=" + Request.QueryString["smallid"] + " and Products like '%" + Server.UrlDecode(Request.QueryString["keywords"]) + "%' and ISPublished='y'" + wheretxt, conn);
        }
       DataSet ds = new DataSet();
       sda.Fill(ds,"cu");
       if (Request.QueryString["pcount"].ToString() == "20")
       this.pp.DataSource =Doadmin.psgb(ds,Table1,20,5);
       else
       this.pp.DataSource = Doadmin.psgb(ds, Table1, 40, 5); 
       pp.DataKeyField = "id";
       this.pp.DataBind();
      

       SqlCommand smdj;
       SqlDataReader sdrj;
       Label laj;
       HyperLink hyj;
       this.pp.RepeatColumns = 4;
       if (ds.Tables[0].Rows.Count != 0)
       {
           for (int j = 0; j < pp.Items.Count; j++)
           {
               smdj = new SqlCommand("select * from ProductInfo where id=" + pp.DataKeys[j].ToString(), conn);
               sdrj = smdj.ExecuteReader();
               sdrj.Read();
               if (Session["Wtouser"] == "vipadmin")
               {
                   laj = (Label)pp.Items[j].FindControl("hui");
                   laj.Text = "会";
                   laj = (Label)pp.Items[j].FindControl("Price");
                   laj.Text = sdrj.GetDecimal(24).ToString("0.0");
                   hyj = (HyperLink)pp.Items[j].FindControl("buyto");
                   hyj.NavigateUrl = "~/china/cart.aspx?pid=" + sdrj["id"].ToString() + "&do=addPP&price=" + Convert.ToString(sdrj.GetDecimal(24));
                   hyj.Target = "_blank";
               }
               else
               {
                   laj = (Label)pp.Items[j].FindControl("hui");
                   laj.Text = "销";
                   laj = (Label)pp.Items[j].FindControl("Price");
                   laj.Text = sdrj.GetDecimal(19).ToString("0.0");
                   hyj = (HyperLink)pp.Items[j].FindControl("buyto");
                   hyj.NavigateUrl = "~/china/cart.aspx?pid=" + sdrj["id"].ToString() + "&do=addPP&price=" + Convert.ToString(sdrj.GetDecimal(19));
                   hyj.Target = "_blank";

               }
               sdrj.Close();
           }
       }
       else
       {
           this.Panel1.Visible = false;
           this.Panel2.Visible = true;
       }
       
       conn.Close();
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
}