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;
using System.Data.OleDb;
public partial class admins_BookManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.typebind();
            this.OleDbDataSource1.SelectParameters[0].DefaultValue = this.ddlSearch.SelectedValue;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.tbType.Text.Length != 0)
        {
            string OleDb = "insert into book_type(typeName) values('" + this.tbType.Text + "')";
            OleDbConnection conn = DB.connstr();
            OleDbCommand insertcmd = new OleDbCommand(OleDb, conn);
            conn.Open();
            insertcmd.ExecuteNonQuery();
            conn.Close();
            this.tbType.Text = "";
        }
        this.typebind();
    }
    protected void typebind()
    {
        string OleDbtype = "select typeID,typeName from book_type";
        DataSet ds = DB.getdata(OleDbtype);
        this.ddlType.DataSource = ds;
        this.delType.DataSource = ds;
        this.ddlSearch.DataSource = ds;
        this.ddlType.DataTextField = "typeName";
        this.ddlType.DataValueField = "typeID";
        this.ddlType.DataBind();
        this.delType.DataTextField = "typeName";
        this.delType.DataValueField = "typeID";
        this.delType.DataBind();
        this.ddlSearch.DataTextField = "typeName";
        this.ddlSearch.DataValueField = "typeID";
        this.ddlSearch.DataBind();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string bkName = this.tbName.Text;
        int bkType = Convert.ToInt32(this.ddlType.SelectedValue);
        int num = Convert.ToInt32(this.tbNum.Text);
        double oldprc = Convert.ToDouble(this.tbOld.Text);
        double nowprc = Convert.ToDouble(this.tbNow.Text);
        string isnew = this.ddlIsnew.SelectedValue;
        string txt = this.tbTxt.Text;
        string imgfile = this.uoImg.PostedFile.FileName.ToString();
        string img = string.Empty;
        if (imgfile.Length != 0)
        {
            string imgType = imgfile.Substring(imgfile.LastIndexOf("."));
            if (imgType == ".jpg" || imgType == ".bmp" || imgType == ".gif")
            {
                string imgurl = Server.MapPath("~/newbook/");
                string imgName = DateTime.Now.Ticks.ToString();
                this.uoImg.PostedFile.SaveAs(imgurl + imgName + imgType);
                img = "~/newbook/" + imgName + imgType;
            }
            else
            {
                Response.Write("<script> alert('图片格式不正确')</script>");
            }
        }
        else
        {
            img = "";
        }
        string OleDba = "insert into book_info(typeID,book_name,book_introduce,oldprice,nowprice,book_image,isNew,inTime,book_number) ";
        string OleDbb = "values(" + bkType + ",'" + bkName + "','" + txt + "'," + oldprc + "," + nowprc + ",'" + img + "','" + isnew + "','" + DateTime.Now.ToString() + "'," + num + ")";
        string OleDbab = OleDba + OleDbb;
        OleDbConnection conn = DB.connstr();
        conn.Open();
        OleDbCommand cmd = new OleDbCommand(OleDbab, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("~/admins/BookManager.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string selOleDb = "select count(*) from book_info where typeID=" + Convert.ToInt32(this.delType.SelectedValue);
        string OleDb = "delete from book_type where typeID=" + Convert.ToInt32(this.delType.SelectedValue);
        OleDbConnection conn = DB.connstr();
        OleDbCommand insertcmd = new OleDbCommand(OleDb, conn);
        OleDbCommand selcmd = new OleDbCommand(selOleDb, conn);
        conn.Open();
        int i = Convert.ToInt32(selcmd.ExecuteScalar());
        if (i <= 0)
        {
            insertcmd.ExecuteNonQuery();
        }
        else
        {
            Response.Write("<script> alert('此种类型书不为空，不能删除')</script>");
        }
        conn.Close();
        this.typebind();
    }

    protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.OleDbDataSource1.SelectParameters[0].DefaultValue = this.ddlSearch.SelectedValue;
    }

}
