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_product : System.Web.UI.UserControl
{
    private string pcount;
    protected void Page_Load(object sender, EventArgs e)
    {
            this.databind();
            this.HyperLink2.NavigateUrl = "~/china/productpms.aspx?pcount=20&SmallId=" + Request.QueryString["SmallId"];
            this.HyperLink3.NavigateUrl = "~/china/productpms.aspx?pcount=40&SmallId=" + Request.QueryString["SmallId"];
    }
    public void databind()
    {       
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        string sqlstr="";
        switch (this.DropDownList1.SelectedValue.ToString())
        {
            case "time":
                sqlstr = "select * from ProductInfo where ISPublished='y' and SmallProductId=" + Request.QueryString["SmallId"] + " and BigProductID=10000043 order by AddDate";
                break;
            case "timedesc":
                sqlstr = "select * from ProductInfo where ISPublished='y' and SmallProductId=" + Request.QueryString["SmallId"] + " and BigProductID=10000043 order by AddDate desc";
                break;
            case "price":
                sqlstr = "select * from ProductInfo where ISPublished='y' and SmallProductId=" + Request.QueryString["SmallId"] + " and BigProductID=10000043 order by VIPPrice";
                break;
            case "pricedesc":
                sqlstr = "select * from ProductInfo where ISPublished='y' and SmallProductId=" + Request.QueryString["SmallId"] + " and BigProductID=10000043 order by VIPPrice desc";
                break;
        }
        SqlDataAdapter sda=new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
      
        sda.Fill(ds,"cu");
        if (Request.QueryString["Pcount"] != null)
        {
            if (Request.QueryString["pcount"].ToString().Trim() == "20")
            {
                this.prductpt.DataSource = Doadmin.psgb(ds, Table1, 20, 1);//分页类调用
            }
            if (Request.QueryString["pcount"].ToString().Trim() == "40")
            {
                this.prductpt.DataSource = Doadmin.psgb(ds, Table1, 40, 1);//分页类调用
            }
        }
        else
        {
            this.prductpt.DataSource = Doadmin.psgb(ds, Table1, 20, 1);//分页类调用
        }
        prductpt.DataKeyField="id";
        this.prductpt.DataBind();
        SqlCommand smdj;
        SqlDataReader sdrj;
        Label laj;
        HyperLink hyj;
        TimeSpan dt;
        this.prductpt.RepeatColumns =4;
        if (ds.Tables[0].Rows.Count != 0)
        {
            for (int j = 0; j < prductpt.Items.Count; j++)
            {
                smdj = new SqlCommand("select * from ProductInfo where id=" + prductpt.DataKeys[j].ToString(), conn);
                sdrj = smdj.ExecuteReader();
                sdrj.Read();
                hyj = (HyperLink)prductpt.Items[j].FindControl("buyto");

                if (sdrj["Stockvolume"].ToString() != "0")
                {
                    if (sdrj.GetDateTime(42) >= DateTime.Now)
                    {
                        hyj.NavigateUrl = "~/china/cart.aspx?pid=" + sdrj["id"].ToString() + "&do=addPP&price=" + Convert.ToString(sdrj.GetDecimal(19));
                        hyj.Target = "_blank";
                        laj = (Label)prductpt.Items[j].FindControl("timeg");
                        dt = (sdrj.GetDateTime(42) - DateTime.Now);
                        laj.Text = dt.Days.ToString() + "天" + dt.Hours.ToString() + "小时";
                    }
                    else
                    {
                        hyj.Text = "促销到期";
                        laj = (Label)prductpt.Items[j].FindControl("timeg");
                        laj.Text = "促销结束";
                    }
                }
                else
                {
                    hyj.Text = "暂时缺货";
                    laj = (Label)prductpt.Items[j].FindControl("timeg");
                    laj.Text = "促销结束";
                }
                sdrj.Close();
            }
            SqlCommand smd = new SqlCommand("select BigProductID,SmallProductId from ProductInfo where ISPublished='y' and SmallProductId=" + Request.QueryString["SmallId"] + " order by AddDate", conn);
            SqlDataReader sdr = smd.ExecuteReader();
            sdr.Read();
            int bigid = sdr.GetInt32(0);
            int smallid = sdr.GetInt32(1);
            sdr.Close();
            SqlCommand smd1 = new SqlCommand("select BigCategory from BigProductCategory where id=" + bigid, conn);
            SqlCommand smd2 = new SqlCommand("select SmallCategory from SmallProductCategory where id=" + smallid, conn);
            this.big.Text = Convert.ToString(smd1.ExecuteScalar());
            big.NavigateUrl = "~/china/productpm.aspx?BigID=" + bigid;
            this.small.Text = Convert.ToString(smd2.ExecuteScalar());
            small.NavigateUrl = "~/china/productpms.aspx?SmallId=" + smallid;
        }
        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;
    }
    public string getimgurl(string no)
    {
        return "fd";
    }

}
