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 wormstrdo;

public partial class wormadmin_admin : System.Web.UI.Page
{

    SqlConnection one = conn.sqlconn();   //建立连接数据库对象
    sqloper sqloper = new sqloper();
    strreplace str = new strreplace();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["superad"] == null || Session["superpw"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Page.Theme = "SkinFile";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            superad.Text = sqloper.loaddb("select * from superadmin", "ad");
            //superpw.Text = Encryption.Decrypt(sqloper.loaddb("select * from superadmin", "pw"));
            gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
        }

    }

    //添加前台管理员
    protected void btadd_Click(object sender, EventArgs e)
    {
        string ad_add = str.txtstr(((TextBox)myGrid.FooterRow.FindControl("ad_add")).Text.Trim());
        string pw_add = Encryption.pwencrypt(str.txtstr(((TextBox)myGrid.FooterRow.FindControl("pw_add")).Text.Trim()),1);
        string sql = "insert into admin values('" + ad_add + "','" + pw_add + "','前台管理员',0)";
        sqloper.dodata(sql, one);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('添加成功!');</script>");
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }


    //超级管理员修改
    protected void Button2_Click(object sender, EventArgs e)
    {
        string ad_add = str.txtstr(superad.Text.Trim());
        string pw_add = Encryption.pwencrypt(str.txtstr(superpw.Text.Trim()),1);
        string sql = "update superadmin set ad='" + ad_add + "',pw='" + pw_add + "'";
        sqloper.dodata(sql, one);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('修改超级管理员成功!');</script>");
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    #region 管理员表Gridview表操作----------------------------------------------------------------
    //数据绑定GridView
    //sql:数据库查询语句,conn:数据库连接,gvid:gridview控件的id,pssize:一页中数据的多少
    protected void gvbind(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        PagedDataSource ps = new PagedDataSource();
        try
        {
            conn.Open();
            adapter.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;              //ps分页数据源设置
            ps.AllowPaging = true;                                       //允许分页
            ps.PageSize = pssize;                                       //页面数据大小
            ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;    //当前页面指针
            AspNetPager1.RecordCount = ds.Tables["order"].Rows.Count;  //记录总行数赋值给控件AspNetPager1

            gvid.DataSource = ps;
            gvid.DataBind();
        }
        catch (System.Exception ee)
        {
            Response.Write("gvbind" + ee.Message.ToString());
        }
        finally
        {
            ds.Dispose();
            adapter.Dispose();
            conn.Close();
        }
    }

    //翻页数据绑定事件
    protected void pagego(object sender, EventArgs e)
    {
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview删除链接
    //单个删除,已取消这个功能
    //这个列加进去即可:<asp:CommandField HeaderText="删除" ShowDeleteButton="True" DeleteText="&lt;div  onclick=&quot;JavaScript:return confirm('确定删除吗？')&quot;&gt;删除&lt;/div&gt;"/>
    protected void mydelete(object sender, GridViewDeleteEventArgs e)
    {
        sqloper.dodata("delete from admin where id=" + myGrid.DataKeys[e.RowIndex].Value, one);
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview编辑链接
    protected void myedit(object sender, GridViewEditEventArgs e)
    {
        //用NewEidIndex取得当前编辑的行号，然后获取gridviewrow对象

        myGrid.EditIndex = e.NewEditIndex;
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview更新数据事件
    protected void myupdate(object sender, GridViewUpdateEventArgs e)
    {
        string ad = str.txtstr(((TextBox)myGrid.Rows[e.RowIndex].FindControl("ad")).Text.Trim());
        string pw = Encryption.pwencrypt(str.txtstr(((TextBox)myGrid.Rows[e.RowIndex].FindControl("pw")).Text.Trim()),1);
        //string ad = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("ad")).Text;
        //string pw = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("pw")).Text;

        string sql = "update admin set ad='" + ad + "',pw='" + pw + "',adlei='前台管理员',addeng=0 where id=" + myGrid.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        myGrid.EditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新前台管理员成功!');</script>");
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview取消编辑事件
    protected void mycancel(object sender, GridViewCancelEditEventArgs e)
    {
        myGrid.EditIndex = -1;
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }


    //选中删除按钮事件
    protected void btdel_Click(object sender, EventArgs e)
    {
        //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>confirm('是否确认删除此留言?');</script>");
        //Response.Write("<SCRIPT LANGUAGE='javascript'>if(confirm('是否确认删除此评论?')){return true;}return false;</SCRIPT>");
        int i;
        string sql = "";
        string myKeyid;
        CheckBox mycheck = new CheckBox();
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                sql = "DELETE FROM admin where id=" + myKeyid;
                sqloper.dodata(sql, one);
            }
        }
        gvbind("select * from admin order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //全选checkbox按钮
    protected void delall_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked == true)
            {
                ch.Checked = true;
            }
            else
            {
                ch.Checked = false;
            }
        }

    }

    //全选button按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (ch.Checked == false)
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = true;
                ch.Checked = true;
            }
            else
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = false;
                ch.Checked = false;
            }
        }
    }

    public string jiemi(string password)
    {
        return  Encryption.Decrypt(password);
    }
    #endregion--------------------------------------------------------------------------------------
    

}
