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_ManagerBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            N38_Catalog myCL = new N38_Catalog();
            myCL.rDropDownList(ddlcatalog);
            BindData();
        }
    }
    /*private string GetString()
    {
        string sb = "select * from n38_25175_book ";
        if (ddlcatalog.SelectedValue != "请选择")
        {
            string catalog = ddlcatalog.SelectedValue;
            sb = sb + "where n38_book_catalog='" + catalog + "'";
            if (tbname.Text != "")
            {
                sb = sb + "and n38_book_name like '%" + tbname.Text.Trim() + "%'";
            }
        }
        else
        {
            if (tbname.Text != "")
            {
                sb = sb + "where n38_book_name like '%" + tbname.Text.Trim() + "%'";
            }
        }
        return sb;
    }*/
    private void BindData()
    {
        N38_GetDataSet myDS = new N38_GetDataSet();
        anpcatalog.RecordCount = myDS.GetBookSearchCount(tbname, ddlcatalog);
        int pageindex = anpcatalog.CurrentPageIndex - 1;
        int pagesize = 5;
        DataSet ds = myDS.GetBookSearchData(tbname, ddlcatalog, pageindex, pagesize);
        gvbook.DataSource = ds;
        gvbook.DataKeyNames = new string[] { "n38_book_id" };
        gvbook.DataBind();
    }
    /*private void BindData2()
    {
        N38_GetDataSet myDS = new N38_GetDataSet();
        anpcatalog.RecordCount = myDS.GetBookCount();
        int pageindex = anpcatalog.CurrentPageIndex - 1;
        int pagesize = 5;
        DataSet ds = myDS.GetBookData(pageindex, pagesize);
        gvbook.DataSource = ds;
        gvbook.DataKeyNames = new string[] { "n38_book_id" };
        gvbook.DataBind();
    }*/
    protected void anpcatalog_PageChanged(object sender, EventArgs e)
    {
        /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
        {
            BindData2();
        }
        else
        {*/

        BindData();
        
        //}
    }
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        
        /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
        {
            Panel1.Visible = true;
            if (!IsPostBack)
            {
                BindData2();
            }
        }
        else
        {*/
            //Panel1.Visible = true;

        BindData();
       // }
    }
   
    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.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)
    {
        gvbook.EditIndex = -1;
        /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
        {*/
        BindData();
        /*}
        else
        {
            BindData2();
        }*/
    }
    protected void gvbook_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvbook.EditIndex = e.NewEditIndex;
        /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
        {*/
        BindData();
        /*}
        else
        {
            BindData2();
        }*/
    }
    protected void gvbook_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(gvbook.DataKeys[e.RowIndex].Value.ToString());
        N38_Book myBook = new N38_Book();
        string name = myBook.GetBookName(id);
        bool result = myBook.DeleteBook(id, name);
        if (result)
        {
            /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
            {*/
            BindData();
           /* }
            else
            {
                BindData2();
            }*/
        }
        else
        {
            if (myBook.BookBorrow(name))
            {
                Response.Write("<script>alert('图书已借出，不能删除')</script>");
            }
            else
            {
                if (myBook.BookLost(name))
                {
                    Response.Write("<script>alert('图书已挂失，不能删除')</script>");
                }
            }
        }
    }
    
    protected void gvbook_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gvbook.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = gvbook.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[2].Controls[0])).Text.Trim();
        DropDownList ddl1 = (DropDownList)row.FindControl("ddledit");
        //string catalog = ((DropDownList)(row.Cells[2].Controls[0])).SelectedValue;
        string cat = ddl1.SelectedValue;
        string isbn = ((TextBox)(row.Cells[4].Controls[0])).Text.Trim();
        string price = ((TextBox)(row.Cells[5].Controls[0])).Text.Trim();
        string amount =((TextBox)(row.Cells[6].Controls[0])).Text.Trim();
        if (name.Length > 25)
        {
            Response.Write("<script>alert('名称长度不能超过25个字')</script>");
            gvbook.EditIndex = -1;
        }
        else
        {
            if (myDS.QuickValidate("\\S{0,15}", isbn) == false)
            {
                Response.Write("<script>alert('ISBN长度不能超过15个字')</script>");
                gvbook.EditIndex = -1;
            }
            else
            {
                if (myDS.QuickValidate("^\\d+(\\.\\d+)?$", price) == false)
                {
                    Response.Write("<script>alert('请输入正确的价格')</script>");
                    gvbook.EditIndex = -1;
                }
                else
                {
                    if (myDS.QuickValidate("\\d+$", amount) == false)
                    {
                        Response.Write("<script>alert('请输入正确的数量')</script>");
                        gvbook.EditIndex = -1;
                    }
                    else
                    {
                        if (cat == "请选择")
                        {
                            Response.Write("<script>alert('请选择分类')</script>");
                            gvbook.EditIndex = -1;
                        }
                        else
                        {
                            float Price = float.Parse(price);
                            int Amount = int.Parse(amount);
                            if (myBook.ExitBook(name) && (oldname.Trim() != name))
                            {
                                Response.Write("<script>alert('书名已经存在')</script>");
                            }
                            else
                            {
                                bool result=myBook.UpdateBook(id, name, cat, Price, Amount, isbn);
                                if (result)
                                {
                                    gvbook.EditIndex = -1;
                                    /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
                                    {*/
                                    BindData();
                                    /*}
                                    else
                                    {
                                        BindData2();
                                    }*/
                                }
                                else
                                {
                                    Response.Write("<script>alert('修改失败')</script>");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    protected void gvbook_DataBinding(object sender, EventArgs e)
    {

    }
    protected void cball_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < gvbook.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)gvbook.Rows[i].FindControl("cbsel");
            if (cball.Checked)
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
        }
    }
    protected void ibtndel_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gvbook.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)gvbook.Rows[i].FindControl("cbsel");
            if (cb.Checked)
            {
                int id = int.Parse(gvbook.DataKeys[i].Value.ToString());
                N38_Book myBook = new N38_Book();
                string name = myBook.GetBookName(id);
                bool result = myBook.DeleteBook(id, name);
                if (result)
                {
                    /*if (tbname.Text == "" && ddlcatalog.SelectedIndex == 0)
                    {
                        gvbook.Visible = true;
                        anpcatalog.Visible = true;
                        BindData2();
                    }
                    else
                    {*/
                        gvbook.Visible = true;
                        anpcatalog.Visible = true;
                        BindData();
                    //}
                }
                else
                {
                    if (myBook.BookBorrow(name))
                    {
                        Response.Write("<script>alert('" + name.Trim() + "已借出，不能删除')</script>");
                    }
                    else
                    {
                        if (myBook.BookLost(name))
                        {
                            Response.Write("<script>alert('" + name.Trim() + "已挂失，不能删除')</script>");
                        }
                    }
                }
            }
        }
    }
}
