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;
using System.Web.Mail;
public partial class WebMain_Member_vipmain : System.Web.UI.UserControl
{
    private Label l1 = new Label();
    private Label l2 = new Label();
    private Label l3 = new Label();
    private Label l4 = new Label();
    private Label l5 = new Label();
    private Label l6 = new Label();
    private Label l7 = new Label();
    private int times = 0;
    TableRow tr;
    TableCell tc;
    ImageButton img1;
    Label lbl1 = new Label();
    TableCell tc2;
    TextBox txtBox;
    LinkButton lb;
    private decimal counttotal = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Wtouser"] != "vipadmin")
        {
            Response.Write("<script>alert('请在会员栏登录后方可进入。');location.href='/default.aspx'</script>");
        }
        else
        {
            if (Request.QueryString["st"] == "y")
            {
                Session["times2"] = "0";
                Response.Redirect("~/china/vipmain.aspx?doing=order");
            }

        if (Request.QueryString["shoucan"] != null)
        {
           SqlConnection connshou = CDatabase.wtoconn();
           connshou.Open();
           switch(Request.QueryString["shoucan"])
           {
               case "addcart":
               string a1, a2,a3;
               int a4, a5, a6;
               decimal a7;
               SqlCommand smdshou = new SqlCommand("select * from Collection  where userid=" + Session["userid"]+" and id="+Request.QueryString["id"],connshou);
               SqlDataReader sd = smdshou.ExecuteReader();
               sd.Read();
               a4 = sd.GetInt32(1);
               a5 = sd.GetInt32(2);
               a6 = sd.GetInt32(3);
               a7 = sd.GetDecimal(4);
               a1 = sd.GetString(5);
               a2 = sd.GetString(6);
               a3 = sd.GetString(7);
               sd.Close();
               connshou.Close();
               if (TOPdo.Execsql("insert into cart(userid,pid,amount,price,Attribute,pic,pname) values ("+a4+","+a5+","+a6+","+a7+",'"+a1+"','"+a2+"','"+a3+"')"))
               {
                   TOPdo.Execsql("delete from Collection where id="+Request.QueryString["id"]);
                   Response.Write("<script>alert('加入购物车成功!可到购物车中完成交易!!');location.href='vipmain.aspx?doing=shoucan'</script>");
               }
               else
               { 
               
               }
               break;
           case "delcart":
               TOPdo.Execsql("delete from Collection where id=" + Request.QueryString["id"]);
               Response.Write("<script>alert('删除成功!!');location.href='vipmain.aspx?doing=shoucan'</script>");
                   break;
          }
        
        }
        if (Request.QueryString["cz"] != null)
        {
            string cz = Request.QueryString["cz"];
            switch (cz)
            { 
                case "sele":
                    break;
                case "del":
                    SqlConnection conncz = CDatabase.wtoconn();
                    conncz.Open();
                    SqlCommand smdcz = new SqlCommand("select OrderState from Orderw where id="+Request.QueryString["id"], conncz);
                    int orderstate = Convert.ToInt32(smdcz.ExecuteScalar());
                    if (orderstate <= 1)
                    {
                        Doadmin.Exec("update Orderw set OrderState=20 where id=" + Request.QueryString["id"]);
                        Response.Redirect("vipmain.aspx?doing=order&st=y");
                    }
                    else
                    {
                        if (orderstate == 8 || orderstate == 23)
                        {

                            Doadmin.Exec("update Orderw set OrderState=21 where id=" + Request.QueryString["id"]);
                            Response.Redirect("vipmain.aspx?doing=order&st=y");
                        }
                        else
                        {
                            Response.Write("<script>alert('此订单正在处理中,不能删除!');location.href='vipmain.aspx?doing=order&st=y'</script>");
                        }
                    }
                    databind();
                    break;
            }

        }
        if (Request.QueryString["zt"] != null)
        {
            int doing = Convert.ToInt32(Request.QueryString["zt"]);
            string sql1 = "";
            switch (doing)
            {
                case 0:
                    sql1 = "update Orderw set OrderState=1 where id=" + Request.QueryString["id"];
                    break;
                case 1:
                    sql1 = "update Orderw set OrderState=2 where id=" + Request.QueryString["id"];
                    break;
                case 6:
                    sql1 = "update Orderw set OrderState=7 where id=" + Request.QueryString["id"];
                    break;
            }
            Session["times2"] = "0";
            Doadmin.Execsql(sql1);
            databind();
        }

            databind();
            Session.Timeout = 2000;
        
    }
    }

    protected void databind()
    { 
    string doing=Request.QueryString["doing"];
    switch (doing)
    {
        # region 主界面
        case null:
            this.Panel1.Visible = true;
            this.Panel3.Visible = false;
            this.Panel4.Visible = false;
            this.Panel5.Visible = false;
            this.Panel6.Visible = false;
            Session["times"] = "0";
            Session["times2"] = "0";
            this.viptruename.Text =Session["vipname"].ToString();
            this.Label3.Text = Session["time1"].ToString();
            this.Label4.Text = Session["time2"].ToString();
            SqlConnection connint=CDatabase.wtoconn();
            connint.Open();
            SqlCommand smdint = new SqlCommand("select Integral from MemberVIP where id="+Session["userid"].ToString(),connint);
            this.vipint.Text =Convert.ToString(smdint.ExecuteScalar());
            connint.Close();
            break;
        #endregion
        # region 订单
        case "order":
            Session["times"] = "0";
            this.Panel1.Visible = false;
            this.Panel3.Visible = true;
            this.Panel4.Visible = false;
            this.Panel5.Visible = false;
            this.Panel6.Visible = false;
                SqlConnection conn = CDatabase.wtoconn();
                conn.Open();
                if (Request.QueryString["cz"] != null)
                {
                    if (Session["times2"] == "0")
                    {
                        this.t1.Visible = false;
                        this.t2.Visible = true;
                        Session["times2"] = "0";
                        Doadmin.ddldatabind(Orderaear, "select * from ServiceArea", "AreaName", "id");
                        SqlCommand smddo = new SqlCommand("select * from Orderw where OrderUserId=" + Session["userid"] + " and  OrderState<>21  and  OrderState<>20 and id=" + Request.QueryString["id"] + "  order by OrderAddDate desc", conn);
                        SqlDataReader sdrdo = smddo.ExecuteReader();
                        sdrdo.Read();
                        this.Orderno.Text = sdrdo.GetString(1);
                        this.Ordername.Text = sdrdo.GetString(3);
                        this.OrderEmail.Text = sdrdo.GetString(4);
                        this.OrderTel.Text = sdrdo.GetString(5);
                        this.OrderMobi.Text = sdrdo.GetString(6);
                        this.Orderaear.SelectedValue= sdrdo.GetString(9);
                        this.OrderAdd.Text = sdrdo.GetString(7);
                        this.OrderPost.Text = sdrdo.GetString(8);
                        this.OrderPostTime.Text = sdrdo.GetString(10);
                        this.OrderPayM.Text = sdrdo.GetString(12);
                        this.OrderSonF.Text = sdrdo.GetString(13);
                        this.OrderAddDate.Text = Convert.ToString(sdrdo.GetDateTime(15));
                        this.OrderMone.Text = sdrdo.GetString(11);
                        this.OrderTatal.Text = "￥ " + Convert.ToString(sdrdo.GetDecimal(14).ToString("0.00"));
                        this.Label1.Text = sdrdo["b3"].ToString();
                        int orderid = sdrdo.GetInt32(0);
                        int sta = sdrdo.GetInt32(16);
                        switch (sta)
                        {
                            case 0:
                                this.sta.Text = "等待确认";
                                this.Label5.Visible = true;
                                break;
                            case 1:
                                this.sta.Text = "请付款";
                                this.Label5.Visible = true;
                                break;
                            case 2:
                                this.sta.Text = "等待处理";
                                this.Label5.Visible = true;
                                break;
                            case 3:
                                this.sta.Text = "已付款";
                                break;
                            case 4:
                                this.sta.Text = "未发货";
                                break;
                            case 5:
                                this.sta.Text = "送货中，注意查收";
                                break;
                            case 6:
                                this.sta.Text = "等待确认收货";
                                break;
                            case 7:
                                this.sta.Text = "已确认收货";
                                break;
                            case 8:
                                this.sta.Text = "√完成";
                                break;
                            case 20:
                                this.sta.Text = "√完成";
                                break;
                            case 21:
                                this.sta.Text = "×作废[VIP清除]";
                                break;
                            case 22:
                                this.sta.Text = "积分订单未处理";
                                break;
                            case 23:
                                this.sta.Text = "积分订单完成";
                                break;

                        }
                        sdrdo.Close();
                        //----------------------------------------------------------------------------

                        # region 表格TB创建[动态]
                        l1.Text = "图片";
                        l2.Text = "商品名称";
                        l3.Text = "优惠价格";
                        l4.Text = "属性配件";
                        l5.Text = "数量";
                        l6.Text = "小计";

                        tr = new TableRow();
                        tr.HorizontalAlign = HorizontalAlign.Center;
                        tc = new TableCell();
                        tc.Controls.Add(l1);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l2);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l3);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l4);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l5);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l6);
                        tr.Controls.Add(tc);
                        tc = new TableCell();
                        tc.Controls.Add(l7);
                        tr.Controls.Add(tc);
                        TB.Controls.Add(tr);
                        TB.HorizontalAlign = HorizontalAlign.Center;
                        TB.CellSpacing = 0;
                        tr.CssClass = "carttr";

                        ImageButton img1;

                        SqlCommand smd2 = new SqlCommand("select * from OrderInfo  where OrderID=" + orderid, conn);
                        SqlDataReader sdr2 = smd2.ExecuteReader();

                        Label lbl1 = new Label();
                        TableCell tc2;
                        TextBox txtBox;
                        HyperLink hy2;
                        while (sdr2.Read())
                        {
                            img1 = new ImageButton();
                            img1.Width = 60;
                            img1.Height = 60;
                            img1.ImageUrl = "~/upfile/" + sdr2.GetString(7);
                            tr = new TableRow();
                            tr.CssClass = "carttrM";
                            tc = new TableCell();
                            tc.Controls.Add(img1);
                            tr.Controls.Add(tc);
                            hy2 = new HyperLink();
                            hy2.Text = SubStr(sdr2.GetString(3), 15);
                            tc = new TableCell();
                            tc.Controls.Add(hy2);
                            tr.Controls.Add(tc);

                            l1 = new Label();
                            l1.Text = "￥：" + Convert.ToString(sdr2.GetDecimal(4).ToString("0.00"));
                            tc = new TableCell();
                            tc.Controls.Add(l1);
                            tr.Controls.Add(tc);


                            l1 = new Label();
                            l1.Text = SubStr(sdr2.GetString(6), 15);
                            tc = new TableCell();
                            tc.Controls.Add(l1);
                            tr.Controls.Add(tc);
                            l1 = new Label();

                            l1 = new Label();
                            l1.Text = Convert.ToString(sdr2.GetInt32(5));
                            tc = new TableCell();
                            tc.Controls.Add(l1);
                            tr.Controls.Add(tc);


                            l1 = new Label();
                            l1.Text = "￥：" + Convert.ToString(sdr2.GetInt32(5) * Convert.ToDecimal(sdr2.GetDecimal(4).ToString("0.00")));
                            counttotal += sdr2.GetInt32(5) * sdr2.GetDecimal(4);
                            tc = new TableCell();
                            tc.Controls.Add(l1);
                            tr.Controls.Add(tc);

                            TB.Controls.Add(tr);
                        }
                        sdr2.Close();
                        # endregion


                        Session["times2"] = "100";
                    }

                }
                else
                {
                    this.t1.Visible = true;
                    this.t2.Visible = false;
                }


                if (Session["times2"] == "0")
                {
                    SqlDataAdapter sad = new SqlDataAdapter("select * from Orderw where OrderUserId=" + Session["userid"] + " and  OrderState<>21    and  OrderState<>20 order by OrderAddDate desc", conn);
                    DataSet ds = new DataSet();
                    sad.Fill(ds, "cu");
                    this.GridView1.DataSource = ds;
                    this.GridView1.DataKeyNames = new string[] { "id" };
                    this.GridView1.DataBind();
                    if (GridView1.Rows.Count == 0)
                    {
                        this.nors.Visible = true;
                    }
                    else
                    {
                        this.nors.Visible = false;
                    }
                    this.Label2.Text = "当前订单数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.GridView1.PageCount + "</b></font>页显示.";
                    HyperLink lb;
                    SqlCommand sad2;
                    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                    {
                        DataRowView mydrv = ds.Tables["cu"].DefaultView[i];
                        sad2 = new SqlCommand("select OrderState from Orderw where id=" + GridView1.DataKeys[i].Value, conn);
                        int isok = Convert.ToInt32(sad2.ExecuteScalar());
                        switch (isok)
                        {
                            case 0:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "等待确认";
                                lb.ForeColor = System.Drawing.Color.Red;
                                break;
                            case 1:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "请付款";
                                lb.ForeColor = System.Drawing.Color.Blue;
                                break;
                            case 2:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.ForeColor = System.Drawing.Color.Green;
                                lb.Text = "等待处理";

                                break;
                            case 3:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.ForeColor = System.Drawing.Color.FromArgb(128, 0, 128);
                                lb.Text = "已付款";

                                break;
                            case 4:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "未发货";

                                break;
                            case 5:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.ForeColor = System.Drawing.Color.FromArgb(255, 0, 255);
                                lb.Text = "送货中，注意查收";
                                break;
                            case 6:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "等待确认收货";
                                break;
                            case 7:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.ForeColor = System.Drawing.Color.FromArgb(128, 0, 0);
                                lb.Text = "已确认收货";
                                break;
                            case 8:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.ForeColor = System.Drawing.Color.FromArgb(255, 153, 0);
                                lb.Text = "√完成";
                                break;
                            case 20:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "√完成";
                                break;
                            case 21:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "×作废[VIP清除]";
                                break;
                            case 22:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "积分订单未处理";
                                lb.ForeColor = System.Drawing.Color.Blue;
                                break;
                            case 23:
                                lb = (HyperLink)this.GridView1.Rows[i].FindControl("HyperLink3");
                                lb.Text = "积分订单完成";
                                break;
                        }
                    }
                    Session["times2"] = "100";

                    sad.Dispose();
                }
            conn.Close();
            break;
        #endregion
        # region 收藏
        case "shoucan":
            this.Panel1.Visible = false;
            this.Panel3.Visible = false;
            this.Panel4.Visible = true;
            this.Panel5.Visible = false;
            this.Panel6.Visible = false;
            Session["times"] = "0";
            Session["times2"] = "0";
            SqlConnection connsc = CDatabase.wtoconn();
            connsc.Open();
            SqlCommand smd8 = new SqlCommand("select * from Collection  where userid=" + Session["userid"], connsc);
            SqlDataReader sdr8 = smd8.ExecuteReader();
            if (sdr8.HasRows == false)
            {
                this.msg.Visible = true;
                sdr8.Close();
                connsc.Close();
            }
            sdr8.Close();
            connsc.Close();
            //从购物车中清除单一产品
            l1.Text = "加入购物车";
            l2.Text = "商品名称";
            l3.Text = "优惠价格";
            l5.Text = "数量";
            l6.Text = "小计";
            l7.Text = "删除";
            tr = new TableRow();
            tr.HorizontalAlign = HorizontalAlign.Center;
            tc = new TableCell();
            tc.Controls.Add(l1);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l2);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l3);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l5);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l6);
            tr.Controls.Add(tc);
            tc = new TableCell();
            tc.Controls.Add(l7);
            tr.Controls.Add(tc);
            TB2.Controls.Add(tr);
            TB2.HorizontalAlign = HorizontalAlign.Center;
            tr.CssClass = "carttr";


            SqlConnection conn1 = CDatabase.wtoconn();
            conn1.Open();
            SqlCommand smd = new SqlCommand("select * from Collection where userid=" + Session["userid"], conn1);
            SqlDataReader sdr = smd.ExecuteReader();
            HyperLink hy1;
            while (sdr.Read())
            {
                
                tr = new TableRow();
                tr.HorizontalAlign = HorizontalAlign.Center;
                tr.CssClass = "carttrM";
                tc = new TableCell();
                img1 = new ImageButton();
                img1.ImageUrl = "~/images/logo_32.gif";
                img1.PostBackUrl = "~/china/vipmain.aspx?shoucan=addcart&doing=shoucan&id=" + sdr.GetInt32(0);
                tc.Controls.Add(img1);
                tr.Controls.Add(tc);


                hy1 = new HyperLink();
                hy1.Text = SubStr(sdr.GetString(7), 15);
                hy1.NavigateUrl="~/china/productlist.aspx?id="+sdr.GetInt32(2);
                hy1.Target = "_blank";

                tc = new TableCell();
                tc.Controls.Add(hy1);
                tr.Controls.Add(tc);

                l1 = new Label();
                l1.Text = " ￥：" + Convert.ToString(sdr.GetDecimal(4).ToString("0.00"));
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);


                l1 = new Label();
                l1.Text = Convert.ToString(sdr.GetInt32(3));
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);


                l1 = new Label();
                l1.Text = "￥：" + Convert.ToString(sdr.GetInt32(3) * Convert.ToDecimal(sdr.GetDecimal(4).ToString("0.00")));
                counttotal += sdr.GetInt32(3) * sdr.GetDecimal(4);
                tc = new TableCell();
                tc.Controls.Add(l1);
                tr.Controls.Add(tc);

                lb = new LinkButton();
                lb.PostBackUrl = "~/china/vipmain.aspx?shoucan=delcart&doing=shoucan&id=" + sdr.GetInt32(0);
                lb.Text = "删除";
                tc = new TableCell();
                tc.Controls.Add(lb);
                tr.Controls.Add(tc);
                TB2.Controls.Add(tr);
            }
            TB2.CellPadding = 0;
            TB2.CellSpacing = 0;
            TB2.CssClass = "cartTB2";
            sdr.Close();
            conn1.Close();
            break;
        #endregion 
        # region 编辑个人信息
        case "editgr":
                 Session["times2"] = "0";
                this.Panel1.Visible = false;
                this.Panel3.Visible = false;
                this.Panel4.Visible = false;
                this.Panel5.Visible = true;
                this.Panel6.Visible = false;
                if (Session["times"] == "0")
                {
                SqlConnection conns = CDatabase.wtoconn();
                conns.Open();
                SqlCommand smds = new SqlCommand("select * from MemberVIP where id=" + Session["userid"], conns);
                SqlDataReader sdrs = smds.ExecuteReader();
                sdrs.Read();
                string vipsex = sdrs.GetString(8);
                if (sdrs.GetString(17) != "0")
                {
                    this.vipname.Text = sdrs.GetString(2);
                    this.vipusername.Text = sdrs.GetString(3);
                    this.vipuserpwd.Text = sdrs.GetString(4);
                    this.PWDQuestions.Text = sdrs.GetString(5);
                    this.PWDAnswer.Text = sdrs.GetString(6);
                    this.vipemail.Text = sdrs.GetString(7);
                    this.Birthday.Text= Convert.ToString(sdrs.GetDateTime(9));
                    this.viparear.Text = sdrs.GetString(10);
                    this.vipAddress.Text = sdrs.GetString(11);
                    this.Post.Text = sdrs.GetString(12);
                    this.MobilePhone.Text = sdrs.GetString(13);
                    this.Telephone.Text = sdrs.GetString(14);

                }
                else
                {
                    this.vipusername.Text = sdrs.GetString(3);
                    this.vipuserpwd.Text = sdrs.GetString(4); 
                    this.vipemail.Text = sdrs.GetString(7);

                }
                if (vipsex == "n")
                {
                    this.isn.Checked = true;
                }
                else
                {
                    this.isv.Checked = true;
                }
                sdrs.Close();
                conns.Close();
                Session["times"] = "100";
            }
            break;
        #endregion 
    }
    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.databind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f5f5f5'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#f5f5f5'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion

        //如果是绑定数据行 
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{

        //    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        //    {
        //        ((HyperLink)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + ((LinkButton)e.Row.Cells[1].Controls[0]).Text + "\"吗?')");
        //    }
        //} 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //取值开始
        string vipname, vipusername, vipuserpwd, PWDQuestions, PWDAnswer, vipsex, vipemail, vipBirthday, viparear, vipAddress, Post, MobilePhone, Telephone;
        //*********************        
        vipuserpwd = this.vipuserpwd.Text.Trim();
        PWDQuestions = this.PWDQuestions.Text.Trim();
        PWDAnswer = this.PWDAnswer.Text.Trim();
        vipemail = this.vipemail.Text.Trim();
        vipBirthday = this.Birthday.Text;
        viparear = this.viparear.Text.Trim();
        vipAddress = this.vipAddress.Text.Trim();
        Post = this.Post.Text.Trim();
        MobilePhone = this.MobilePhone.Text.Trim();
        Telephone = this.Telephone.Text.Trim();
        if (this.isn.Checked)
        {
            vipsex = "n";
        }
        else
        {
            vipsex = "v";
        }
        string userip = "";
        if (Request.ServerVariables["HTTP_VIA"] != null)
        {

            userip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

        }
        else
        {

            userip = Request.ServerVariables["REMOTE_ADDR"].ToString();

        }

        string sqlstr = "update MemberVIP set vipname='"+this.vipname.Text+"',vipuserpwd='" + vipuserpwd + "',PWDQuestions='" + PWDQuestions + "',PWDAnswer='" + PWDAnswer + "',vipemail='" + vipemail + "',vipsex='" + vipsex + "',vipBirthday='" + vipBirthday + "',viparear='" + viparear + "',vipAddress='" + vipAddress + "',Post='" + Post + "',MobilePhone='" + MobilePhone + "',Telephone='" + Telephone + "',loginIP='" + userip + "' where id=" + Session["userid"];
        if (Doadmin.Exec(sqlstr))
        {
            //邮件发送

            SqlConnection Mconn = CDatabase.wtoconn();
            Mconn.Open();
            SqlCommand Msmd = new SqlCommand("select * from SystemBasicInformation", Mconn);
            SqlDataReader Msdr = Msmd.ExecuteReader();
            Msdr.Read();
            string Mform, Muid, Mpwd, Mstmp, Misopen, Mcont, MCompanyName, MWEBName, MTel, MFax, MEmail, MQQ, MAddress, MMSN, TBWW, MLOGOpic, Memailto;
            Mform = Msdr.GetString(7);
            Muid = Mform;
            Mpwd = Msdr.GetString(26);
            Mstmp = Msdr.GetString(25);
            Misopen = Msdr.GetString(1).ToString().Trim();
            MCompanyName = Msdr.GetString(2);
            MWEBName = Msdr.GetString(3);
            MTel = Msdr.GetString(5);
            MFax = Msdr.GetString(6);
            MEmail = Msdr.GetString(7);
            MQQ = Msdr.GetString(8);
            MAddress = Msdr.GetString(10);
            MMSN = Msdr.GetString(13);
            TBWW = Msdr.GetString(14);
            Memailto = Msdr.GetString(22).Trim();
            MLOGOpic = "<a href=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "><img src=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "/images/" + Msdr.GetString(15) + " border=0></a>";
            Msdr.Close();
            if (Memailto == "y")
            {
                SqlCommand Msmd2 = new SqlCommand("select content from email where lx='3'", Mconn);
                Mcont = Server.HtmlDecode(Convert.ToString(Msmd2.ExecuteScalar()));
                SqlCommand Msmd3 = new SqlCommand("select title from email where lx='3'", Mconn);
                string Mti = Convert.ToString(Msmd3.ExecuteScalar());
                MailMessage msg = new MailMessage();
                msg.From = Mform;  //发送方地址(如test@163.com)
                msg.To = vipemail; //接收方地址(如test@163.com) 
                msg.BodyFormat = MailFormat.Html;   //正文内容类型 
                msg.BodyEncoding = System.Text.Encoding.Default;    //正文内容编码 
                //msg.Attachments.Add(new MailAttachment("d:\\d.txt"));
                //主题 
                msg.Subject = Mti;
                //内容 
                //编码定义
                Mcont = (Mcont.Replace("$UID$",this.vipusername.Text).Replace("$PWD$",vipuserpwd));
                Mcont = Mcont.Replace("$CMY$", MCompanyName);
                Mcont = Mcont.Replace("$WEBN$", MWEBName);
                Mcont = Mcont.Replace("$TEL$", MTel);
                Mcont = Mcont.Replace("$FAX$", MFax);
                Mcont = Mcont.Replace("$EMA$", MEmail);
                Mcont = Mcont.Replace("$QQ$", MQQ);
                Mcont = Mcont.Replace("$ADD$", MAddress);
                Mcont = Mcont.Replace("$MSN$", MMSN);
                Mcont = Mcont.Replace("$TBW$", TBWW);
                Mcont = Mcont.Replace("$LOGO$", MLOGOpic);
                msg.Body = Mcont;
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //设置为需要用户验证 
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Muid); //设置验证用户名 
                msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Mpwd); //设置验证密码 
                SmtpMail.SmtpServer = Mstmp;  //邮件服务器地址(如smtp.163.com) 
                SmtpMail.Send(msg);  //发送 

            }
            Mconn.Close();
            Response.Write("<script>alert('修改成功!');window.location.href='vipmain.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('此次提交失败!')</script>");
        }
    }
    #region 截取字符
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    #endregion
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }
    //合并订单
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Session["times2"] = "100";
        string orderid = "";
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd;
        SqlDataReader sdr;
        string ist = "y";
        string[] tt1;
        string[] tt2;
        string[] tt3;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                smd = new SqlCommand("select OrderState from Orderw where id=" + GridView1.DataKeys[i].Value, conn);
                if (Convert.ToInt32(smd.ExecuteScalar()) == 0)
                {
                    orderid += GridView1.DataKeys[i].Value.ToString() + "|";
                }
                else
                {
                    Response.Write("<script>alert('你所选择的订单中,其中有订单正在处理中,不能进行此操作!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                    break;
                }
            }
        }
        tt1 = orderid.Split('|');
        
        if (tt1.Length == 1)
        {
            ist = "n";
            Response.Write("<script>alert('请选择需要操作订单,不能进行此操作!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
        }
        else
        {
            if (ist == "y")
            {
                decimal total = 0;
                decimal alltotal = 0;
                smd = new SqlCommand("select freesonfei from SystemBasicInformation", conn);
                int freeint = Convert.ToInt32(smd.ExecuteScalar());
                smd = new SqlCommand("select sonfei from SystemBasicInformation", conn);
                int sonfei = Convert.ToInt32(smd.ExecuteScalar());
                for (int j = 0; j < tt1.Length - 1; j++)
                {
                    smd = new SqlCommand("select OrderTatal from Orderw where id=" + tt1[j], conn);
                    total = Convert.ToDecimal(smd.ExecuteScalar());
                    if (total > freeint)
                    {
                        alltotal += total;
                    }
                    else
                    {
                        total = total - sonfei;
                        alltotal += total;
                    }
                }
                smd = new SqlCommand("select * from Orderw where id=" + tt1[0], conn);
                sdr = smd.ExecuteReader();
                sdr.Read();
                string Orderno, OrderUserId, Ordername, OrderEmail, OrderTel, OrderMobi, OrderAdd, OrderPost, Orderaear, OrderPostTime, OrderMone, OrderPayM, OrderSonF, OrderState, isvip, a2, a3, b1, b2, b3, text1;
                DateTime OrderAddDate = DateTime.Now;
                Orderno = "CTSALE" + DateTime.Now.ToString("yyyymmddssss");
                OrderUserId = sdr["OrderUserId"].ToString();
                Ordername = sdr["Ordername"].ToString();
                OrderEmail = sdr["OrderEmail"].ToString();
                OrderTel = sdr["OrderTel"].ToString();
                OrderMobi = sdr["OrderMobi"].ToString();
                OrderAdd = sdr["OrderAdd"].ToString();
                OrderPost = sdr["OrderPost"].ToString();
                Orderaear = sdr["Orderaear"].ToString();
                OrderPostTime = sdr["OrderPostTime"].ToString();
                OrderMone = sdr["OrderMone"].ToString();
                OrderPayM = sdr["OrderPayM"].ToString();
                OrderSonF = sdr["OrderSonF"].ToString();
                OrderState = sdr["OrderState"].ToString();
                isvip = sdr["isvip"].ToString();
                a2 = sdr["a2"].ToString();
                a3 = sdr["a3"].ToString();
                b1 = sdr["b1"].ToString();
                b2 = sdr["b2"].ToString();
                b3 = sdr["b3"].ToString();
                text1 = sdr["text1"].ToString();
                sdr.Close();
                string sqltxt = "insert into Orderw(Orderno,OrderUserId,Ordername,OrderEmail,OrderTel,OrderMobi,OrderAdd,OrderPost,Orderaear,OrderPostTime,OrderMone,OrderPayM,OrderSonF,OrderTatal,OrderAddDate,OrderState,isvip,a2,a3,b1,b2,b3,text1) values (";
                sqltxt += "'" + Orderno + "'," + OrderUserId + ", '" + Ordername + "', '" + OrderEmail + "', '" + OrderTel + "', '" + OrderMobi + "', '" + OrderAdd + "', '" + OrderPost + "', '" + Orderaear + "', '" + OrderPostTime + "', '" + OrderMone + "', '" + OrderPayM + "', '" + OrderSonF + "'," + alltotal + ",'" + OrderAddDate + "'," + OrderState + ", '" + isvip + "', '" + a2 + "', '" + a3 + "', " + b1 + ", " + b2 + ", " + b3 + ", '" + text1 + "')";
                if (Doadmin.Exec(sqltxt))
                {
                    smd = new SqlCommand("select id from Orderw where Orderno='" + Orderno + "'", conn);
                    int idd = Convert.ToInt32(smd.ExecuteScalar());
                    for (int g = 0; g < tt1.Length - 1; g++)
                    {
                        Doadmin.Execsql("update OrderInfo set OrderID=" + idd + " where OrderID=" + tt1[g].Trim());
                        Doadmin.Execsql("delete from Orderw where id=" + tt1[g]);
                    }
                    Response.Write("<script>alert('操作成功,有些数据是系统自动生,需您再对合并后进行修改订单!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                }
                else
                {
                    Response.Write("<script>alert('操作不成功,请使用其他方式合并!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                }
            }
            }

            conn.Close();

        
    }
    //复制订单
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Session["times2"] = "100";
        int orderid =0,times=0;
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd;
        SqlDataReader sdr;
        string ist = "y";
        string[] tt1;
        string[] tt2;
        string[] tt3;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                times++;
                if (times > 1)
                {
                    ist = "n";
                    Response.Write("<script>alert('复制操作只能针对一个订单,不能多个一起复制!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                }
                else
                {
                    smd = new SqlCommand("select OrderState from Orderw where id=" + GridView1.DataKeys[i].Value, conn);
                    if (Convert.ToInt32(smd.ExecuteScalar()) == 0)
                    {
                        orderid = Convert.ToInt32(GridView1.DataKeys[i].Value);
                    }
                    else
                    {
                        ist = "n";
                        Response.Write("<script>alert('你所选择的订单中,其中有订单正在处理中,不能进行此操作!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                    }

                }
               
            }
        }
        if (orderid == 0)
        {
            Response.Write("<script>alert('请选择操作的订单!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
        }
        else
        {
            if (ist == "y")
            {
                smd = new SqlCommand("select * from Orderw where id=" + orderid, conn);
                sdr = smd.ExecuteReader();
                sdr.Read();
                string Orderno, OrderUserId, Ordername, OrderEmail, OrderTel, OrderMobi, OrderAdd, OrderPost, Orderaear, OrderPostTime, OrderMone, OrderPayM, OrderSonF, OrderState, isvip, a2, a3, b1, b2, b3, text1;
                DateTime OrderAddDate = DateTime.Now;
                decimal OrderTatal;
                Orderno = "CTSALE" + DateTime.Now.ToString("yyyymmddssss");
                OrderUserId = sdr["OrderUserId"].ToString();
                Ordername = sdr["Ordername"].ToString();
                OrderEmail = sdr["OrderEmail"].ToString();
                OrderTel = sdr["OrderTel"].ToString();
                OrderMobi = sdr["OrderMobi"].ToString();
                OrderAdd = sdr["OrderAdd"].ToString();
                OrderPost = sdr["OrderPost"].ToString();
                Orderaear = sdr["Orderaear"].ToString();
                OrderPostTime = sdr["OrderPostTime"].ToString();
                OrderMone = sdr["OrderMone"].ToString();
                OrderPayM = sdr["OrderPayM"].ToString();
                OrderSonF = sdr["OrderSonF"].ToString();
                OrderState = sdr["OrderState"].ToString();
                isvip = sdr["isvip"].ToString();
                a2 = sdr["a2"].ToString();
                a3 = sdr["a3"].ToString();
                b1 = sdr["b1"].ToString();
                b2 = sdr["b2"].ToString();
                b3 = sdr["b3"].ToString();
                OrderTatal = Convert.ToDecimal(sdr["OrderTatal"].ToString());
                text1 = sdr["text1"].ToString();
                sdr.Close();
                string sqltxt = "insert into Orderw(Orderno,OrderUserId,Ordername,OrderEmail,OrderTel,OrderMobi,OrderAdd,OrderPost,Orderaear,OrderPostTime,OrderMone,OrderPayM,OrderSonF,OrderTatal,OrderAddDate,OrderState,isvip,a2,a3,b1,b2,b3,text1) values (";
                sqltxt += "'" + Orderno + "'," + OrderUserId + ", '" + Ordername + "', '" + OrderEmail + "', '" + OrderTel + "', '" + OrderMobi + "', '" + OrderAdd + "', '" + OrderPost + "', '" + Orderaear + "', '" + OrderPostTime + "', '" + OrderMone + "', '" + OrderPayM + "', '" + OrderSonF + "'," + OrderTatal + ",'" + OrderAddDate + "'," + OrderState + ", '" + isvip + "', '" + a2 + "', '" + a3 + "', " + b1 + ", " + b2 + ", " + b3 + ", '" + text1 + "')";
                if (Doadmin.Exec(sqltxt))
                {
                    smd = new SqlCommand("select id from Orderw where Orderno='" + Orderno + "'", conn);
                    int idd = Convert.ToInt32(smd.ExecuteScalar());
                    smd = new SqlCommand("select * from OrderInfo where OrderID='" + orderid + "'", conn);
                    sdr = smd.ExecuteReader();
                    string OrderID, Pid, ProductName, Amount, Attr, pic,st;
                    decimal price;
                    string sqltxt2 = "";
                    while (sdr.Read())
                    {
                        Pid = sdr["Pid"].ToString();
                        ProductName = sdr["ProductName"].ToString();
                        Amount = sdr["Amount"].ToString();
                        Attr = sdr["Attr"].ToString();
                        pic = sdr["pic"].ToString();
                        price = sdr.GetDecimal(4);
                        st = sdr["OrderState"].ToString();
                        sqltxt2 = "insert into OrderInfo(OrderID,Pid,ProductName,price,Amount,Attr,pic,addtime,st) values(" + idd + "," + Pid + ",'" + ProductName + "'," + price + "," + Amount + ",'" + Attr + "','" + pic + "','"+DateTime.Now.ToString()+"',"+st+")";
                        Doadmin.Execsql(sqltxt2);
                    }
                    sdr.Close();

                    Response.Write("<script>alert('操作成功,有些数据是系统自动生,需您再对合并后进行修改订单!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                }
                else
                {
                    Response.Write("<script>alert('操作不成功,请使用其他方式复制!!');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
                }

            }
        }
        }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd = new SqlCommand("select OrderState from Orderw where id="+Request.QueryString["id"], conn);
        if (Convert.ToInt32(smd.ExecuteScalar())== 0)
        {
            string sqltxt = "update Orderw set Ordername='" + this.Ordername.Text + "',OrderEmail='" + this.OrderEmail.Text + "',OrderTel='" + this.OrderTel.Text + "',OrderMobi='" + this.OrderMobi.Text + "',OrderAdd='" + this.OrderAdd.Text + "',OrderPost='" + this.OrderPost.Text + "',Orderaear='" + this.Orderaear.SelectedValue.ToString() + "',OrderPostTime='" + this.OrderPostTime.Text + "',OrderMone='" + this.OrderMone.Text + "' where id=" + Request.QueryString["id"];
            if (Doadmin.Exec(sqltxt))
            {
                Response.Write("<script>alert('操作成功');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
            }
            else
            {
                Response.Write("<script>alert('操作失败');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('此订单在处理中,不能进行修改;如需修改,请致电我司.');window.location.href='/china/vipmain.aspx?doing=order&st=y';</script>");
        }
        conn.Close();

    }
}
