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;

public partial class N38_ManagerUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        N38_GetDataSet myDS = new N38_GetDataSet();
        anpuser.RecordCount = myDS.GetUserCount(tbname,ddlstate);
        int pageindex = anpuser.CurrentPageIndex - 1;
        int pagesize = 5;
        gvuser.DataSource = myDS.GetUserData(tbname,ddlstate, pageindex, pagesize);
        gvuser.DataKeyNames = new string[] { "n38_user_id" };
        gvuser.DataBind();
    }
    protected void gvuser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d0e5f5'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
            e.Row.Attributes.Add("style", "height:22px;background-color:#ffffff");
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Attributes.Add("style", "background-image:url('n38_image/ht_10.gif');height:22px");
        }
    }
    protected void gvuser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "lock")
        {
            GridViewRow gvr = (GridViewRow)(((ImageButton)(e.CommandSource)).NamingContainer);
            int id = int.Parse(gvuser.DataKeys[gvr.RowIndex].Value.ToString());
            N38_User myUser = new N38_User();
            myUser.LockUser(id);
            BindData();
        }
        if (e.CommandName == "unlock")
        {
            GridViewRow gvr = (GridViewRow)(((ImageButton)(e.CommandSource)).NamingContainer);
            int id = int.Parse(gvuser.DataKeys[gvr.RowIndex].Value.ToString());
            N38_User myUser = new N38_User();
            myUser.UnLockUser(id);
            BindData();
        }
    }
    protected void gvuser_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(gvuser.DataKeys[e.RowIndex].Value.ToString());
        string name = gvuser.Rows[e.RowIndex].Cells[4].Text.Trim();
        N38_User myUser = new N38_User();
        bool result=myUser.DeleteUser(id, name);
        if (result)
        {
            BindData();
        }
        else
        {
            Response.Write("<script>alert('用户有借书，不能删除')</script>");
        }
    }
    protected void gvuser_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvuser.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gvuser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvuser.EditIndex = -1;
        BindData();
    }
    protected void gvuser_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gvuser.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = gvuser.Rows[e.RowIndex];
        N38_User myUser = new N38_User();
        string oldname = myUser.GetUserName(id);
        string name = ((TextBox)(row.Cells[4].Controls[0])).Text.Trim();
        DropDownList ddl=(DropDownList)row.FindControl("ddltype");
        string realname = ((TextBox)(row.Cells[2].Controls[0])).Text.Trim();
        string type = ddl.SelectedValue;
        if (name.Length > 25)
        {
            Response.Write("<script>alert('帐号长度不能超过25个字')</script>");
            gvuser.EditIndex = -1;
        }
        else
        {
            if (realname.Length > 10)
            {
                Response.Write("<script>alert('姓名不能超过10个字')</script>");
                gvuser.EditIndex = -1;
            }
            else
            {
                if (type == "请选择类型")
                {
                    Response.Write("<script>alert('请选择用户类型')</script>");
                    gvuser.EditIndex = -1;
                }
                else
                {
                    if (oldname != name)
                    {
                        if (myUser.UserExit(name))
                        {
                            Response.Write("<script>alert('用户名已经存在')</script>");
                            gvuser.EditIndex = -1;
                        }
                        else
                        {
                            bool result = myUser.UpdateUser(id, name, realname, type);
                            if (result)
                            {
                                gvuser.EditIndex = -1;
                                BindData();
                            }
                            else
                            {
                                Response.Write("<script>alert('修改用户信息失败')</script>");
                                gvuser.EditIndex = -1;
                            }
                        }
                    }
                    else
                    {
                        bool result = myUser.UpdateUser(id, name, realname, type);
                        if (result)
                        {
                            gvuser.EditIndex = -1;
                            BindData();
                        }
                        else
                        {
                            Response.Write("<script>alert('修改用户信息失败')</script>");
                            gvuser.EditIndex = -1;
                        }
                    }
                }
            }
        }
    }
    protected void anpcatalog_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void cball_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < gvuser.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)gvuser.Rows[i].FindControl("cbdel");
            if (cball.Checked)
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
        }
    }
    protected void ibtndel_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gvuser.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)gvuser.Rows[i].FindControl("cbdel");
            if (cb.Checked)
            {
                int id = int.Parse(gvuser.DataKeys[i].Value.ToString());
                string name=gvuser.Rows[i].Cells[4].Text.Trim();
                string realname = gvuser.Rows[i].Cells[2].Text.Trim();
                N38_User myUser = new N38_User();
                bool result=myUser.DeleteUser(id, name);
                if (result)
                {
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('"+realname+"还有未还的书')</script>");
                }
            }
        }
    }
    protected void ibtnok_Click(object sender, ImageClickEventArgs e)
    {
        N38_User myUser = new N38_User();
        if (myUser.UserExitByReal(tbname.Text.Trim()))
        {
            BindData();
        }
        else
        {
            Response.Write("<script>alert('用户不存在')</script>");
            tbname.Text = "";
        }
    }
}
