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_Member_viplogin : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ValidentCode(this.chkcode.Text.ToString().Trim()))
        {
            if (TOPdo.checkUser(this.user.Text.ToString().Trim(), this.pwd.Text.ToString().Trim()))
            {
                string UserName = this.user.Text.ToString().Trim();
                string PassWord = this.pwd.Text.ToString().Trim();
                SqlConnection conn = CDatabase.wtoconn();
                conn.Open();
                SqlCommand smdd = new SqlCommand("select intwhere1,intwhere2 from SystemBasicInformation", conn);
                SqlDataReader sdrd = smdd.ExecuteReader();
                sdrd.Read();
                int intwhere1 = sdrd.GetInt32(0);
                int intwhere2 = sdrd.GetInt32(1);
                sdrd.Close();
                if (Session["Wtouser"] != "vipadmin")
                {
                    string sqlstr = "update MemberVIP set logintimes=logintimes+1,enddate='" + DateTime.Now.ToString() + "' where vipusername='" + UserName + "' and vipuserpwd='" + PassWord + "'";
                    TOPdo.Execsql(sqlstr);
                }
                SqlCommand smd = new SqlCommand("select * from  MemberVIP where vipusername='" + UserName + "' and vipuserpwd='" + PassWord + "'", conn);
                SqlDataReader sdr = smd.ExecuteReader();
                sdr.Read();
                string sqlstr2 = "update cart set userid=" + sdr.GetInt32(0) + " where userid=" + Session["userid"];
                TOPdo.Execsql(sqlstr2);
                DateTime d1 = sdr.GetDateTime(24).AddMonths(intwhere1);
                DateTime d2 = sdr.GetDateTime(25).AddMonths(intwhere2);
                if (DateTime.Now > d1 || DateTime.Now > d2)
                {
                    Session["intj"] = "0";
                }
                else
                {
                    Session["intj"] = sdr["Integral"].ToString();
                }
                if (DateTime.Now > d1)
                {
                    TOPdo.Execsql("update MemberVIP set Integral=0 where id=" + sdr.GetInt32(0));
                    Session["time1"] = DateTime.Now.AddMonths(intwhere1).ToString("d");

                }
                else
                {
                    Session["time1"] = d1.ToString("d");

                }
                if (DateTime.Now > d2)
                {
                    TOPdo.Execsql("update MemberVIP set Integral=0 where id=" + sdr.GetInt32(0));
                    Session["time2"] = DateTime.Now.AddMonths(intwhere2).ToString("d");
                }
                else
                {
                    Session["time2"] = d2.ToString("d");
                }
                Session["username"] = sdr.GetString(3);
                Session["vipright"] = sdr.GetString(17).Trim();
                Session["userid"] = Convert.ToString(sdr.GetInt32(0));
                Session["times"] = "0";
                Session["times2"] = "0";
                Session["Wtouser"] = "vipadmin";
                Session["wtouserid"] = sdr.GetInt32(0);
                Session["vipname"] = sdr.GetString(2);
                Session["id"] = Convert.ToString(sdr.GetInt32(0));
                Session["zk"] = sdr["vipzk"].ToString();

                //int tme = 1;
                //if (tme == 1)
                //{
                //    Response.Write("<script>alert('亲爱的" + sdr.GetString(2) + ",感谢您莅临我们购物网站\\n您上次登录日期：" + sdr.GetDateTime(20) + "。\\n共登录次数：" + Convert.ToString(sdr.GetInt32(16)) + "')</script>");
                //    tme = 2;
                //}
                sdr.Close();
                SqlCommand smdj;
                smdj = new SqlCommand("select * from cart where userid=" + Session["userid"].ToString(), conn);
                SqlDataReader sdrj;
                sdrj = smdj.ExecuteReader();
                string pidstr = "";
                while (sdrj.Read())
                {
                    pidstr += sdrj["id"].ToString() + "$";
                }
                sdrj.Close();
                string[] txt = pidstr.Split('$');
                string pidd = "";
                decimal price;
                for (int i = 0; i < txt.Length - 1; i++)
                {
                    smdj = new SqlCommand("select pid from cart where id=" + txt[i].ToString(), conn);
                    pidd = Convert.ToString(smdj.ExecuteScalar());
                    smdj = new SqlCommand("select VIPPrice from ProductInfo where id=" + pidd.Trim(), conn);
                    price = Convert.ToDecimal(smdj.ExecuteScalar());
                    Doadmin.Execsql("update cart set price=" + price + " where id=" + txt[i]);
                }
                conn.Close();
                Response.Redirect("~/china/vipmain.aspx");
            }
            else
            {
                Response.Write("<script>alert('用户名和密码错误， 或此用户禁用！');window.location.href='/china/viplogin.aspx'</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('验证码错误');history.go(-1);</script>");
        }
    }
    public bool ValidentCode(string CheckCode)
    {
        if (Session["ValidateCode"] != null)
        {
            bool str = CheckCode == Session["ValidateCode"].ToString() ? true : false;
            return str;
            Session.Timeout = 2000;
        }
        else
        {
            return false;
        }

    }

}
