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)
    {
        if (Request.QueryString["type"] != null)
        {
            if (Session["Wtouser"] == "vipadmin")
            {
                SqlConnection conn = CDatabase.wtoconn();
                conn.Open();
                SqlCommand smd;
                SqlDataReader sdr;
                smd = new SqlCommand("select Stockvolume from ProductInfo where id=" + Request.QueryString["id"].Trim(), conn);
                string stock = Convert.ToString(smd.ExecuteScalar()).Trim();
                if (stock != "0")
                {
                    smd = new SqlCommand("select * from ProductInfo where id=" + Request.QueryString["id"].Trim(), conn);
                    sdr = smd.ExecuteReader();
                    sdr.Read();
                    int intj = 0, Pid = 0;
                    string ProductName = "", pic = "";
                    if (sdr["Stockvolume"].ToString() != "0")
                    {
                        //产品信息取值
                        intj = Convert.ToInt32(sdr["intj"].ToString());
                        Pid = Convert.ToInt32(sdr["id"].ToString());
                        ProductName = sdr["Products"].ToString();
                        pic = sdr["ThumbnailsPic"].ToString();

                    }
                    sdr.Close();
                    smd = new SqlCommand("select * from MemberVIP where id=" + Session["userid"], conn);
                    sdr = smd.ExecuteReader();
                    sdr.Read();
                    int vipint = Convert.ToInt32(sdr["Integral"].ToString());
                    string Ordername = sdr["vipname"].ToString();
                    string OrderEmail = sdr["vipemail"].ToString();
                    string OrderTel = sdr["Telephone"].ToString();
                    string OrderMobi = sdr["MobilePhone"].ToString();
                    string OrderAdd = sdr["vipAddress"].ToString();
                    string OrderPost = sdr["Post"].ToString();
                    string OrderMone = "这是积分所换的订单";
                    string OrderPayM = "积分";
                    string OrderSonF = "配送";
                    string Orderaear = "10000012";
                    int OrderTatal = 0;
                    string OrderAddDate = DateTime.Now.ToString();
                    string OrderState = "22";//积分订单
                    string isvip = "y";
                    sdr.Close();
                    if (vipint >= intj)
                    {
                        //会员信息取值
                        string Orderno = "INT" + DateTime.Now.ToString("yymmddssffssss");
                        string OrderUserId = Session["userid"].ToString();

                        string sql1 = "insert into Orderw(Orderno,OrderUserId,Ordername,OrderEmail,OrderTel,OrderMobi,OrderAdd,OrderPost,Orderaear,OrderPostTime,OrderMone,OrderPayM,OrderSonF,OrderTatal,OrderAddDate,OrderState,isvip,b3) ";
                        sql1 += "values ('" + Orderno + "'," + OrderUserId + ",'" + Ordername + "','" + OrderEmail + "','" + OrderTel + "','" + OrderMobi + "','" + OrderAdd + "','" + OrderPost + "','" + Orderaear + "','" + DateTime.Now.ToString() + "','" + OrderMone + "','" + OrderPayM + "','" + OrderSonF + "'," + Convert.ToDecimal(OrderTatal) + ",'" + OrderAddDate + "'," + OrderState + ",'" + isvip + "'," + intj + ")";
                        if (Doadmin.Exec(sql1))
                        {
                            Doadmin.Execsql("update MemberVIP set Integral=Integral-" + intj + ",intout='"+DateTime.Now.ToString()+"' where id=" + Session["userid"]);
                            Doadmin.Execsql("update ProductInfo set Stockvolume=Stockvolume-1 where id=" + Request.QueryString["id"].Trim());
                            Session["intj"] = Convert.ToInt32(Session["intj"]) - intj;
                            smd = new SqlCommand("select id from Orderw where Orderno='" + Orderno + "'", conn);
                            int OrderID = Convert.ToInt32(smd.ExecuteScalar());

                            TOPdo.Execsql("insert into OrderInfo(OrderID,Pid,ProductName,price,Amount,Attr,pic) values (" + OrderID + "," + Pid + ",'" + ProductName + "',0,1,'','" + pic + "')");
                            Response.Write("<script>alert('兑换成功!我们会在最短时间为您送到! \\n 积分赠送详情请到商城服务的积分制度。');window.location.href='/china/intproductsmall.aspx?SmallId=" + Request.QueryString["SmallId"] + "'</script>");

                        }
                        else
                        {
                            Response.Write("<script>alert('兑换失败! \\n 积分赠送详情请到商城服务的积分制度。');window.location.href='/china/intproductsmall.aspx?SmallId=" + Request.QueryString["SmallId"] + "'</script>");

                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('您积分不够，请购买本网的商品获取更多积分! \\n 积分赠送详情请到商城服务的积分制度。');window.location.href='/china/intproductsmall.aspx?SmallId=" + Request.QueryString["SmallId"] + "'</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('此赠品已送完，请购买本网的商品获取更多积分! \\n 积分赠送详情请到商城服务的积分制度。');window.location.href='/china/intproductsmall.aspx?SmallId=" + Request.QueryString["SmallId"] + "'</script>");
                }
                conn.Close();

            }
            else
            {
                Response.Write("<script>alert('请登录，只有会员凭积分换此赠品! \\n积分赠送详情请到商城服务的积分制度。');window.location.href='/china/intproductsmall.aspx?SmallId=" + Request.QueryString["SmallId"]+"</script>");
            }
        }
            this.databind();
            this.HyperLink2.NavigateUrl = "~/china/intproductsmall.aspx?pcount=20&SmallId=" + Request.QueryString["SmallId"];
            this.HyperLink3.NavigateUrl = "~/china/intproductsmall.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 BigProductID=10000034 and SmallProductId=" + Request.QueryString["SmallId"] + " order by AddDate";
                break;
            case "timedesc":
                sqlstr = "select * from ProductInfo where ISPublished='y' and BigProductID=10000034  and SmallProductId=" + Request.QueryString["SmallId"] + " order by AddDate desc";
                break;
            case "price":
                sqlstr = "select * from ProductInfo where ISPublished='y' and BigProductID=10000034  and SmallProductId=" + Request.QueryString["SmallId"] + " order by intj";
                break;
            case "pricedesc":
                sqlstr = "select * from ProductInfo where ISPublished='y' and BigProductID=10000034  and SmallProductId=" + Request.QueryString["SmallId"] + " order by intj 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;
        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();
                laj = (Label)prductpt.Items[j].FindControl("intj");
                laj.Text=sdrj["intj"].ToString(); 
                hyj = (HyperLink)this.prductpt.Items[j].FindControl("dh");
                if (Session["Wtouser"] == "vipadmin")
                {
                    hyj.Text = "现在兑换";
                    hyj.NavigateUrl = "~/china/intproductsmall.aspx?type=doint&id=" + prductpt.DataKeys[j].ToString() + "&SmallId=" + Request.QueryString["SmallId"];
                }
                else
                {
                    hyj.Text = "请先登录";
                }
                sdrj.Close();

            }

   
        }
        else
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
        }
        SqlCommand smdtxt = new SqlCommand("select SmallCategory from SmallProductCategory where id=" + Request.QueryString["SmallId"], conn);
        this.txt.Text = Convert.ToString(smdtxt.ExecuteScalar());
        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";
    }

}
