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_BorrowBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lbtnok_Click(object sender, ImageClickEventArgs e)
    {
        if (tbid.Text == "")
            Response.Write("<script>alert('请输入读者编号')</script>");
        else
        {
            N38_User myUser = new N38_User();
            int id = int.Parse(tbid.Text.Trim());
            myUser.GetUserData(id, lblid, lblname, lbltype, lblamount);
        }
    }
    protected void ibtnbook_Click(object sender, ImageClickEventArgs e)
    {
        if (tbbook.Text == "")
            Response.Write("<script>alert('请输入图书编号')</script>");
        else
        {
            int id=int.Parse(tbbook.Text.Trim());
            N38_GetDataSet myDS = new N38_GetDataSet();
            gvbook.Visible = true;
            gvbook.DataSource = myDS.GetBookByID(id);
            gvbook.DataBind();
        }
    }
    protected void ibtnname_Click(object sender, ImageClickEventArgs e)
    {
        if (tbname.Text == "")
            Response.Write("<script>alert('请输入图书名称')</script>");
        else
        {
            N38_GetDataSet myDS = new N38_GetDataSet();
            gvbook.Visible = true;
            gvbook.DataSource = myDS.GetBookByName(tbname.Text.Trim());
            gvbook.DataBind();
        }
    }

    protected void gvbook_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "borrow")
        {
            if (tbid.Text==""||lblid.Text == "该读者不存在")
            {
                Response.Write("<script>alert('请输入读者编号')</script>");
            }
            else
            {
                GridViewRow gvr = (GridViewRow)(((ImageButton)(e.CommandSource)).NamingContainer);
                int index = gvr.RowIndex;
                N38_User myUser = new N38_User();
                N38_Config myConfig=new N38_Config();
                string username = myUser.GetUserName(int.Parse(tbid.Text.Trim()));
                string type = myUser.GetUserType(int.Parse(tbid.Text.Trim()));
                string bookname = gvbook.Rows[index].Cells[1].Text.Trim();
                N38_BookOperate myBookOP = new N38_BookOperate();
                bool borrow = myBookOP.BorrowBook(username, type, bookname);
                if (borrow)
                {
                    Response.Write("<script>alert('借书成功')</script>");
                }
                else
                {
                    if (myUser.UserLock(username))
                    {
                        Response.Write("<script>alert('用户被锁定，借书失败')</script>");
                    }
                    else
                    {
                        int num=myUser.GetBorrowAmount(username);
                        int amount=myConfig.GetBookAmount(type);
                        if (num >= amount)
                        {
                            Response.Write("<script>alert('用户借书数量不能超过最大借书量')</script>");
                        }
                    }
                }
            }
        }
    }
    protected void gvbook_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");
        }
    }
}
