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_PrductptPP : System.Web.UI.UserControl
{
    private int count;
    private int Rcount;
    private string Ptype;
    public string Count
    {
        set { count = Convert.ToInt32(value.ToString()); }
    }
    public string RCount
    {
        set { Rcount =Convert.ToInt32(value.ToString()); }
        get { return "1"; }
    }
    public string PType
    {
        set { Ptype = value.ToString(); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

            this.databind();
    }
    public void databind()
    {
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlDataAdapter sda;
            string[] str = Request.QueryString["BigID"].Split('|');
            if (str.Length==1)
            {
                switch(Ptype)
                {
                    case "new":
                        sda = new SqlDataAdapter("select top " + count + " * from ProductInfo where  ISPublished='y' and BigProductID=" + str[0].Trim() + " and IsNew='y' order by adddate", conn);
                      break;
                    case "hot":
                        sda = new SqlDataAdapter("select top " + count + " * from ProductInfo where  ISPublished='y' and BigProductID=" + str[0].Trim() + " and IsHot='y' order by adddate", conn);
                        break;
                    case "rec":
                        sda = new SqlDataAdapter("select top " + count + " * from ProductInfo where  ISPublished='y' and BigProductID=" + str[0].Trim() + " and IsRecommended='y' order by adddate", conn);
                        break;
                    case "spe":
                        sda = new SqlDataAdapter("select top " + count + " * from ProductInfo where  ISPublished='y' and BigProductID=" + str[0].Trim() + " and  IsSpecials='y' order by adddate", conn);
                        break;
                    default:
                        sda = new SqlDataAdapter("select top 8 * from ProductInfo where  ISPublished='y' and BigProductID=" + str[0].Trim() + " IsNew='y' and order by adddate", conn);
                        break;
                }
            }
            else
            {
                sda = new SqlDataAdapter("select * from ProductInfo where  ISPublished='y' and Brand=" + str[0] + " order by adddate", conn);
            }
        DataSet ds = new DataSet();
        sda.Fill(ds,"cu");
        this.pp.DataSource = ds;
        this.pp.DataKeyField = "id";
        this.pp.DataBind();

        this.pp.RepeatColumns =Rcount;
        SqlCommand smdj;
        SqlDataReader sdrj;
        Label laj;
        HyperLink hyj;
            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");
                    if (sdrj["Stockvolume"].ToString() != "0")
                    {
                        hyj.NavigateUrl = "~/china/cart.aspx?pid=" + sdrj["id"].ToString() + "&do=addPP&price=" + Convert.ToString(sdrj.GetDecimal(24));
                        hyj.Target = "_blank";
                    }
                    else
                    {
                        hyj.Text = "暂时缺货";
                    }
                    laj = (Label)pp.Items[j].FindControl("ShPrice");
                    laj.Text = Convert.ToString(Convert.ToDecimal(sdrj.GetDecimal(18).ToString("0")) - Convert.ToDecimal(sdrj.GetDecimal(24).ToString("0")));
                }
                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");
                    if (sdrj["Stockvolume"].ToString() != "0")
                    {
                        hyj.NavigateUrl = "~/china/cart.aspx?pid=" + sdrj["id"].ToString() + "&do=addPP&price=" + Convert.ToString(sdrj.GetDecimal(19));
                        hyj.Target = "_blank";
                    }
                    else
                    {
                        hyj.Text = "暂时缺货";
                    }
                    laj = (Label)pp.Items[j].FindControl("ShPrice");
                    laj.Text = Convert.ToString(Convert.ToDecimal(sdrj.GetDecimal(18).ToString("0")) - Convert.ToDecimal(sdrj.GetDecimal(19).ToString("0")));

                }
                sdrj.Close();
            }
        
        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;
    }
  
}
