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

public partial class AddMaster : System.Web.UI.Page
{
    SqlDB sqldb = new SqlDB();
    Operation operate = new Operation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("index.aspx");
            }
            P2.Visible =false;
            P1.Visible = false;
            GridViewBind();
        }
    }
    private void GridViewBind()
    {
        string strSql = "select * from userInfo";
        operate.GridViewBind(GridView1, strSql);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strSql = "select * from UserInfo where username='" + txtUid.Text + "'";
        if (sqldb.ExecuteScalar(strSql).Length > 0)
        {
            //该管理员已存在、不能重复添加
            Response.Write("<script language='javaScript'>alert('该管理员已存在、不能重复添加');</script>");
        }
        else
        {
            //可以添加
            string Value = "'" + sqldb.CheckString(txtUid.Text) + "','" + sqldb.CheckString(txt_Pwd.Text) + "','" + sqldb.CheckString(txtEmail.Text) + "','" + sqldb.CheckString(txtPhone.Text) + "','" + sqldb.CheckString(txtQQ.Text) + "'";
            strSql = "insert into UserInfo(username,pwd,userEmail,userPhone,QQ) values("+Value+")";
            if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
            {
                Response.Write("<script language='javaScript'>alert('管理员添加成功，您可以使用该账号管理网站了！');</script>");
                txtEmail.Text = "";
                txtPhone.Text = "";
                txtQQ.Text = "";
                P2.Visible = false;
                GridViewBind();
            }
            else
            {
                Response.Write("<script language='javaScript'>alert('管理员添加失败，请重新添加！');</script>");
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string strSql = "update userInfo set pwd='" + sqldb.CheckString(txtEditPwd.Text.Trim()) + "',userEmail='" + sqldb.CheckString(txtEditEmail.Text.Trim()) + "',userPhone='" + sqldb.CheckString(txtEditPhone.Text.Trim()) + "',QQ='" + sqldb.CheckString(txtEditQQ.Text.Trim()) + "' where userId="+ViewState["id"].ToString();
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            Response.Write("<script language='javaScript'>alert('管理员更新成功，您可以用新的密码登录了！');</script>");
            P1.Visible = false;
            GridViewBind();
        }
        else
        {
            Response.Write("<script language='javaScript'>alert('管理员更新失败，请重新操作！');</script>");
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        P1.Visible = false;
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.txt_Pwd.Text.Trim() == this.txtRePwd.Text.Trim())
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }  
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            P2.Visible = true;
            P1.Visible = false;
        }        
    }
    protected void GridView1_OnPageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string strSql = "delete from userInfo where userId="+id;
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            Response.Write("<script language='javaScript'>alert('管理员删除成功！');</script>");
            GridViewBind();
        }
        else
        {
            Response.Write("<script language='javaScript'>alert('管理员删除失败，请重新删除！');</script>");
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    { 
        P1.Visible = true;
        P2.Visible = false;
        string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();       
        ViewState["id"] = id;
        string strSql = "select * from userInfo where userId="+id;
        DataSet ds = sqldb.GetDS(strSql);
        txtEditUid.Text = ds.Tables[0].Rows[0]["username"].ToString();        
        txtEditEmail.Text = ds.Tables[0].Rows[0]["userEmail"].ToString();
        txtEditPhone.Text = ds.Tables[0].Rows[0]["userPhone"].ToString();
        txtEditQQ.Text = ds.Tables[0].Rows[0]["QQ"].ToString();
        ds.Clear();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        P2.Visible = false;
    }
}
