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 WTOSYSadmin_vipmag_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (Request.QueryString["do"] != null)
        {
            string sql2 = "delete from MemberVIP where id=" + Request.QueryString["id"];
            Doadmin.Exec(sql2);
            Doadmin.Exec("delete from Comments where userid=" + Request.QueryString["id"].Trim());
            if (this.TextBox2.Text == "")
            {
                switch (DropDownList1.SelectedValue.ToString())
                {
                    case "0":
                        databind("all");
                        break;
                    case "1":
                        databind("pt");
                        break;
                    case "2":
                        databind("zs");
                        break;
                    case "3":
                        databind("ts");
                        break;
                }
            }
            else
            {
                databind("seach");
            }
        }
     

        if (Request.QueryString["id"] != null & Request.QueryString["doing"] != null)
        {
            string doing = Request.QueryString["doing"];
            string sql1;
            if (doing == "y")
            {
                sql1 = "update MemberVIP set isok='n' where id=" + Request.QueryString["id"];
            }
            else
            {
                sql1 = "update MemberVIP set isok='y' where id=" + Request.QueryString["id"];
            }
            Doadmin.Execsql(sql1);
            if (this.TextBox2.Text == "")
            {
                switch (DropDownList1.SelectedValue.ToString())
                {
                    case "0":
                        databind("all");
                        break;
                    case "1":
                        databind("pt");
                        break;
                    case "2":
                        databind("zs");
                        break;
                    case "3":
                        databind("ts");
                        break;
                }
            }
            else
            {
                databind("seach");
            }
        }
        if (!IsPostBack)
        {
            if (this.TextBox2.Text == "")
            {
                switch (DropDownList1.SelectedValue.ToString())
                {
                    case "0":
                        databind("all");
                        break;
                    case "1":
                        databind("pt");
                        break;
                    case "2":
                        databind("zs");
                        break;
                    case "3":
                        databind("ts");
                        break;
                }
            }
            else
            {
                databind("seach");
            }
        }
    }
    private void databind(string keywords)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sad = new SqlDataAdapter();
        switch(keywords)
        {
            case "all":
                sad = new SqlDataAdapter("select * from MemberVIP order by addtime desc", conn);
                break;
            case "seach":           
                sad = new SqlDataAdapter("select * from MemberVIP where vipusername like '%"+this.TextBox2.Text+"%' order by addtime desc", conn);
                break;
            case "pt":
                sad = new SqlDataAdapter("select * from MemberVIP where vipright='1'  order by addtime desc", conn);
                break;
            case "zs":
                sad = new SqlDataAdapter("select * from MemberVIP where vipright='2'  order by addtime desc", conn);
                break;
            case "ts":
                sad = new SqlDataAdapter("select * from MemberVIP where vipright='3' order by addtime desc", conn);
                break;
        }
        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;
            this.t1.Visible = false;
        }
        else
        {
            this.nors.Visible = false;
            this.t1.Visible = true;
            this.Label1.Text = "当前会员数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.GridView1.PageCount + "</b></font>页显示.";
        }
        LinkButton lb;
        SqlCommand sad2;
        
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv = ds.Tables["cu"].DefaultView[i];
            sad2 = new SqlCommand("select isok from MemberVIP where id=" + GridView1.DataKeys[i].Value, conn);
            string isok = Convert.ToString(sad2.ExecuteScalar());
            if (isok == "y")
            {
                lb = (LinkButton)this.GridView1.Rows[i].FindControl("doing");
                lb.Text = "运行中";
                lb.Enabled = false;
            }
            else
            {
                lb = (LinkButton)this.GridView1.Rows[i].FindControl("doing");
                lb.Text = "暂屏蔽";
                lb.Enabled = false;
            }
            
        }       
        sad.Dispose();
        conn.Close();
    }

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

            e.Row.Attributes.Add("ondblclick", "location.href='vipedit.aspx?id=" + GridView1.DataKeys[g].Value + "'");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion
        //如果是绑定数据行 
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        if (this.TextBox2.Text == "")
        {
            switch (DropDownList1.SelectedValue.ToString())
            {
                case "0":
                    databind("all");
                    break;
                case "1":
                    databind("pt");
                    break;
                case "2":
                    databind("zs");
                    break;
                case "3":
                    databind("ts");
                    break;
            }
        }
        else
        {
            databind("seach");
        }
    }
    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)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from MemberVIP where id=" + GridView1.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        if (this.TextBox2.Text == "")
        {
            switch (DropDownList1.SelectedValue.ToString())
            {
                case "0":
                    databind("all");
                    break;
                case "1":
                    databind("pt");
                    break;
                case "2":
                    databind("zs");
                    break;
                case "3":
                    databind("ts");
                    break;
            }
        }
        else
        {
            databind("seach");
        }
    }

    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedValue = "0";
        if (this.TextBox2.Text == "")
        {
            switch (DropDownList1.SelectedValue.ToString())
            {
                case "0":
                    databind("all");
                    break;
                case "1":
                    databind("pt");
                    break;
                case "2":
                    databind("zs");
                    break;
                case "3":
                    databind("ts");
                    break;
            }
        }
        else
        {
            databind("seach");
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {    
        this.TextBox2.Text = "";
        if (this.TextBox2.Text == "")
        {
            switch (DropDownList1.SelectedValue.ToString())
            {
                case "0":
                    databind("all");
                    break;
                case "1":
                    databind("pt");
                    break;
                case "2":
                    databind("zs");
                    break;
                case "3":
                    databind("ts");
                    break;
            }
        }
        else
        {
            databind("seach");
        }

    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        string txt="";
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                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;
                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);
                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();
                SqlCommand cmds = new SqlCommand("select emailFL,content from email where lx='0' and type='0' and id=" + Request.QueryString["id"], Mconn);
                SqlDataReader csdr = cmds.ExecuteReader();
                csdr.Read();
                string Mzt = csdr.GetString(0);
                Mcont = Server.HtmlDecode(csdr.GetString(1));
                csdr.Close();
                SqlCommand Vsmd = new SqlCommand("select * from MemberVIP where isok='y' and id="+GridView1.DataKeys[i].Value, Mconn);
                SqlDataReader Vsdr = Vsmd.ExecuteReader();
                MailMessage msg;
                Vsdr.Read();
                    msg = new MailMessage();
                    msg.From = Mform;  //发送方地址(如test@163.com)
                    msg.To = Vsdr.GetString(7); //接收方地址(如test@163.com)
                    msg.BodyFormat = MailFormat.Html;   //正文内容类型 
                    msg.BodyEncoding = System.Text.Encoding.Default;    //正文内容编码 
                    //msg.Attachments.Add(new MailAttachment("d:\\d.txt"));
                    //主题 
                    msg.Subject = Mzt;
                    //内容 
                    //编码定义
                    Mcont = ((Mcont.Replace("$UID$", Vsdr.GetString(3))).Replace("$PWD$", Vsdr.GetString(4)));
                    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);
                    Mcont = Mcont.Replace("$ORN$", Vsdr.GetString(2));
                    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);  //发送 
                Vsdr.Close();
                Mconn.Close();
            }

        }

    }
}
