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_ViewBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        N38_GetDataSet myDS = new N38_GetDataSet();
        anpcatalog.RecordCount = myDS.GetBookCount();
        int pageindex = anpcatalog.CurrentPageIndex - 1;
        int pagesize = 5;
        DataSet ds = myDS.GetBookData(pageindex, pagesize);
        gvbook2.DataSource = ds;
        gvbook2.DataKeyNames = new string[] { "n38_book_id" };
        gvbook2.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.RowState   ==   DataControlRowState.Edit   ||   e.Row.RowState   ==   (DataControlRowState.Alternate|DataControlRowState.Edit)) 
            {
                
                    DropDownList ddl = (DropDownList)e.Row.Cells[2].FindControl("ddledit");
                    N38_Catalog myCL = new N38_Catalog();
                    myCL.rDropDownList(ddl);
                
            }
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Attributes.Add("style", "background-image:url('n38_image/ht_10.gif');height:22px");
        }
    }
    protected void gvbook_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvbook2.EditIndex = -1;
        BindData();
    }
    protected void gvbook_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvbook2.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gvbook_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(gvbook2.DataKeys[e.RowIndex].Value.ToString());
        N38_Book myBook = new N38_Book();
        string name = myBook.GetBookName(id);
        bool result = myBook.DeleteBook(id, name);
        if (result)
        {
            BindData();
        }
        else
        {
            Response.Write("<script>alert('删除失败，请检查图书是否借出或者挂失')</script>");
        }
    }
    protected void gvbook_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gvbook2.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = gvbook2.Rows[e.RowIndex];
        N38_Book myBook = new N38_Book();
        N38_GetDataSet myDS = new N38_GetDataSet();
        string oldname = myBook.GetBookName(id);
        string name = ((TextBox)(row.Cells[1].Controls[0])).Text.Trim();
        DropDownList ddl1 = (DropDownList)gvbook2.Rows[e.RowIndex].FindControl("ddledit");
        string cat = ddl1.SelectedValue;
        string isbn = ((TextBox)(row.Cells[3].Controls[0])).Text.Trim();
        string price = ((TextBox)(row.Cells[4].Controls[0])).Text.Trim();
        string amount = ((TextBox)(row.Cells[5].Controls[0])).Text.Trim();
        if (name.Length > 25)
        {
            Response.Write("<script>alert('名称长度不能超过25个字')</script>");
            gvbook2.EditIndex = -1;
        }
        else
        {
            if (myDS.QuickValidate("\\S{0,15}", isbn) == false)
            {
                Response.Write("<script>alert('ISBN长度不能超过15个字')</script>");
                gvbook2.EditIndex = -1;
            }
            else
            {
                if (myDS.QuickValidate("^\\d+(\\.\\d+)?$", price) == false)
                {
                    Response.Write("<script>alert('请输入正确的价格')</script>");
                    gvbook2.EditIndex = -1;
                }
                else
                {
                    if (myDS.QuickValidate("^[1-9]\\d?$", amount) == false)
                    {
                        Response.Write("<script>alert('请输入正确的数量')</script>");
                        gvbook2.EditIndex = -1;
                    }
                    else
                    {
                        float Price = float.Parse(price);
                        int Amount = int.Parse(amount);
                        myBook.UpdateBook(id, name, cat, Price, Amount, isbn);
                        gvbook2.EditIndex = -1;
                        BindData();
                    }
                }
            }
        }
    }
    protected void anpcatalog_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
}
