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)
    {
        this.databind();
        this.HyperLink2.NavigateUrl = "~/china/highsearchresult.aspx?pcount=20&keyword=" + Request.QueryString["keyword"].ToString() + "&";
        this.HyperLink3.NavigateUrl = "~/china/highsearchresult.aspx?pcount=40&keyword=" + Request.QueryString["keyword"].ToString() + "&";
    }
    public void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sda;
        string[] key;
        key = Request.QueryString["keyword"].Split('|');
        string sqltxt = "select * from ProductInfo  where  ISPublished='y' and intj=0 and  BigProductID<>10000043 and BigProductID<>10000034";
        if (key[0] !="0" && key[1] !="0")
        { 
        sqltxt+= " and BigProductID='"+key[0]+"' and SmallProductId=" +key[1];
        }
        if (key[2] != "")
        {
            sqltxt += " and Products like '%" +key[2]+ "%'";
        }
        if (key[3] != "")
        {
            sqltxt += " and ProductsNo like '%" + key[3] + "%'";
        }
        if (key[4] != "")
        {
            sqltxt += " and Description like '%" + key[4] + "%'";
        }
        if (key[5] != "")
        {
            sqltxt += " and Content like '%" + key[5] + "%'";
        }
        if (key[6] != "")
        {
            sqltxt += " and Brand like '%" + key[6] + "%'";
        }
        if (key[7] != "" && key[8] != "")
        {
            sqltxt += " and  SalePrice>" + key[7] + " and SalePrice<"+key[8];
        }
        if (key[9] == "0")
        {
            sqltxt += " and  IsRecommended='y'";
        }
        if (key[9] == "1")
        {
           sqltxt += " and  IsRecommended='y'";
        }
        if (key[10] == "0")
        {
            sqltxt += " and  IsSpecials='y'";
        }
        if (key[10] == "1")
        {
            sqltxt += " and  IsSpecials='y'";
        }
        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;
        }

        sda = new SqlDataAdapter(sqltxt + 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;
    }
}