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 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["id"] != "10000001")
        {

            if (Request.QueryString["do"] != null)
            {
                string sql2 = "delete from UserAdmin where id=" + Request.QueryString["id"];
                Doadmin.Exec(sql2);
                this.databind();
            }
            if (Request.QueryString["id"] != null & Request.QueryString["doing"] != null)
            {
                string doing = Request.QueryString["doing"];
                string sql1;
                if (doing == "y")
                {
                    sql1 = "update UserAdmin set IsOpen='close' where id=" + Request.QueryString["id"];
                }
                else
                {
                    sql1 = "update UserAdmin set IsOpen='open' where id=" + Request.QueryString["id"];
                }
                Doadmin.Execsql(sql1);
                databind();

            }
        }
        else
        {
            Response.Write("<script>alert('最高级管理员不能进行操作!');location.href='SysMag.aspx'</script>");
        }

        if (!IsPostBack)
        {
            databind();
        }
    }
    private void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sad = new SqlDataAdapter("select * from UserAdmin order by addtime 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;
            this.t1.Visible = false;
        }

        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 IsOpen from UserAdmin where id=" + GridView1.DataKeys[i].Value, conn);
            string isok = Convert.ToString(sad2.ExecuteScalar()).Trim();
            if (isok == "open")
            {
                lb = (LinkButton)this.GridView1.Rows[i].FindControl("doing");
                lb.Text = "运行中";
                lb.PostBackUrl = "SYSmag.aspx?id=" + GridView1.DataKeys[i].Value + "&doing=y";
            }
            else
            {
                lb = (LinkButton)this.GridView1.Rows[i].FindControl("doing");
                lb.Text = "暂屏蔽";
                lb.PostBackUrl = "SYSmag.aspx?id=" + GridView1.DataKeys[i].Value + "&doing=n";
            }
        }       
        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
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((HyperLink)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + ((HyperLink)e.Row.Cells[1].Controls[0]).Text + "\"吗?')");
            }
        } 
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }
    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)
            {
                if (GridView1.DataKeys[i].Value.ToString() != "10000001")
                {
                    string sqlstr = "delete from UserAdmin where id=" + GridView1.DataKeys[i].Value;
                    Doadmin.Execsql(sqlstr);
                }
            }
        }
        this.databind();
    }
}
