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

    private void BindData()
    {
        N38_User myUser = new N38_User();
        N38_Book myBook = new N38_Book();
        N38_GetDataSet myDS = new N38_GetDataSet();
        string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
        string bookname = myBook.GetBookName(int.Parse(tbbookid.Text.Trim()));
        gvbook.DataSource = myDS.GetBorrowBookData(username, bookname);
        gvbook.DataKeyNames = new string[] { "n38_borrow_id" };
        gvbook.DataBind();
    }
    private void BindData2()
    {
        N38_User myUser = new N38_User();
        N38_GetDataSet myDS = new N38_GetDataSet();
        string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
        gvbook.DataSource = myDS.GetUserBorrowData(username);
        gvbook.DataKeyNames = new string[] { "n38_borrow_id" };
        gvbook.DataBind();
    }
    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");
        }
    }
    protected void ibtnok_Click(object sender, ImageClickEventArgs e)
    {
        if (tbuserid.Text == "")
        {
            Response.Write("<script>alert('请输入读者编号')</script>");
        }
        else
        {
            if (tbbookid.Text == "")
            {
                N38_User myUser = new N38_User();
                N38_Book myBook = new N38_Book();
                N38_Config myConfig = new N38_Config();
                bool exit = myUser.UserExitByID(int.Parse(tbuserid.Text.Trim()));
                if (exit)
                {
                    gvbook.Visible = true;
                    //ibtnrefresh.Visible = true;
                    BindData2();
                }
                else
                {
                    Response.Write("<script>alert('用户不存在')</script>");
                }
            }
            else
            {
                N38_User myUser = new N38_User();
                N38_Book myBook = new N38_Book();
                N38_Config myConfig = new N38_Config();
                bool exit = myUser.UserExitByID(int.Parse(tbuserid.Text.Trim()));
                if (exit)
                {
                    string bookname = myBook.GetBookName(int.Parse(tbbookid.Text.Trim()));
                    string type = myUser.GetUserType(int.Parse(tbuserid.Text.Trim()));
                    string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
                    DateTime time = myBook.GetBorrowDate(bookname, username);
                    int day = myConfig.GetBookDay(type);
                    myBook.OvertimeBorrowBook(day, username, bookname, time);
                    gvbook.Visible = true;
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('用户不存在')</script>");
                }
            }
        }
    }
    protected void gvbook_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "back")
        {
            GridViewRow gvr=(GridViewRow)(((ImageButton)(e.CommandSource)).NamingContainer);
            int index=gvr.RowIndex;
            N38_User myUser = new N38_User();
            N38_Book myBook = new N38_Book();
            string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
            //string bookname = myBook.GetBookName(int.Parse(tbbookid.Text.Trim()));
            string bookname = gvbook.Rows[index].Cells[0].Text.Trim();
            N38_BookOperate myBookOP = new N38_BookOperate();
            bool result=myBookOP.BackBook(username, bookname);
            if (result)
            {
                if (tbbookid.Text == "")
                {
                    Response.Write("<script>alert('读者还书成功')</script>");
                    BindData2();
                }
                else
                {
                    Response.Write("<script>alert('读者还书成功')</script>");
                    BindData();
                }
            }
            else
            {
                Response.Write("<script>alert('用户借书超期，还书失败')</script>");
            }
        }
        if (e.CommandName == "normal")
        {
            N38_User myUser=new N38_User();
            GridViewRow gvr = (GridViewRow)(((ImageButton)(e.CommandSource)).NamingContainer);
            int id = int.Parse(gvbook.DataKeys[gvr.RowIndex].Value.ToString());
            string bookname = gvbook.Rows[gvr.RowIndex].Cells[0].Text.Trim();
            string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
            N38_Book myBook = new N38_Book();
            myBook.NormalBorrowBook(bookname, username);
            if (tbbookid.Text == "")
            {
                BindData2();
            }
            else
            {
                BindData();
            }
        }
    }
    protected void ibtnrefresh_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gvbook.Rows.Count; i++)
        {
            /*gvbook.Columns[3].Visible = false;
            gvbook.Columns[4].Visible = false;*/
            N38_User myUser = new N38_User();
            N38_Book myBook = new N38_Book();
            N38_Config myConfig = new N38_Config();
            N38_GetDataSet myDS = new N38_GetDataSet();
            string bookname = gvbook.Rows[i].Cells[0].Text.Trim();
            string type = myUser.GetUserType(int.Parse(tbuserid.Text.Trim()));
            string username = myUser.GetUserName(int.Parse(tbuserid.Text.Trim()));
            DateTime time = myBook.GetBorrowDate(bookname, username);
            int day = myConfig.GetBookDay(type);
            myBook.OvertimeBorrowBook(day, username, bookname, time);
            BindData2();
        }
    }
}
