<?php
session_start();
$username = $_SESSION['staff'];

include ("connect.php");

	$num = $_POST["num"];
	$ISBN = $_POST["ISBN"];
	$itemName = $_POST["itemName"];
	$itemAuthor = $_POST["itemAuthor"];
	$itemPublisher = $_POST["itemPublisher"];
	$itemDate = $_POST["itemDate"];
	$itemCategory = $_POST["itemCategory"];
	$itemPrice = $_POST["itemPrice"];
	$itemSummary = $_POST["itemSummary"];
	$itemQuantity = $_POST["itemQuantity"];
	$itemMin = $_POST["itemMin"];
	$itemLocation = $_POST["itemLocation"];
	
	if($_POST["Save"] == "Save")
	{
	$result = mysql_query("INSERT INTO item(num, ISBN, itemImage, itemName, itemAuthor, itemPublisher, itemDate, itemCategory, itemMin, itemPrice, itemSummary, itemQuantity, itemLocation)
	VALUES
	('$num', '$ISBN', '$itemImage', '$itemName', '$itemAuthor', '$itemPublisher', '$itemDate', '$itemCategory', $itemMin, '$itemPrice', '$itemSummary', '$itemQuantity', '$itemLocation')");
	
	
	$tmp_file_name = $_FILES['file']['tmp_name']; 
	$file_name = $_FILES['file']['name']; 
	$file_dir = './bookimage/'.$_POST['title'].'/';  
	if(is_dir($file_dir)) 
	{ 
	move_uploaded_file($tmp_file_name,$file_dir.$file_name); 
	$img_url = $file_dir.$file_name; 
	

	$result = mysql_query("UPDATE item SET itemImage = '$img_url' WHERE ISBN = '$ISBN'");
	
	
	$db = mysql_close();
	echo "<script language='javascript'>alert('Book Insert Success.')</script>";
}
}
if($_SESSION['staff']!= null)
{
?>
	
<html>
<head>
<title>Daily Bookstore - Management System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="calendar.js"></script>
<script type="text/javascript" src="calendar-setup.js"></script>
<script type="text/javascript" src="calendar-en.js"></script>
<style type="text/css"> @import url("calendar-win2k-cold-1.css"); </style>
<script language="Javascript">
	function showPreview(ele)
	{
		document.addbook.imgAvatar.src=ele.value;
	}
</script>
</head>

<body>

<?php
include ("stafffunctionbar.php");
?>

<center>
<h2>Daily Bookstore Management - Manage Book</h2>
</center>

<div id="mainpage">
<?php
include("bookfunction.php")
?>
</fieldset>
</td>

<td width="75%">
<center>
<form name="addbook" method="post" enctype="multipart/form-data">
<fieldset style="width:80%">
<legend><img src="Picture/add.gif" width="30" height="30">New Book Insertion Form</legend>

<table align="center">
<tr>
<td>Book No.: </td>
<?php

include("booknum.php");
echo "<td>";
echo "<input type='hidden' name='num' value='$index'/>$index";
echo "</td>";
?>
</tr>

<tr>
<td>ISBN: </td>
<td><input type="text" name="ISBN" size="17" maxlength="17"></td>
<td width='200' rowspan="5"><img id="imgAvatar" img src="bookimage/bookcover.jpg" max-width="200" max-height="200"></td>
</tr>

<tr>
<td>Book Image: </td>
<td><input type="file" name="file" OnChange="showPreview(this)">
</td>
</tr>

<tr>
<td>Book Name: </td>
<td><input type="text" name="itemName" size="40" maxlength="255"></td>
</tr>

<tr>
<td>Author: </td>
<td><input type="text" name="itemAuthor" size="40" maxlength="100"></td>
</tr>

<tr>
<td>Publisher: </td>
<td><input type="text" name="itemPublisher" size="40" maxlength="100"></td>
</tr>

<tr>
<td>Book Date: </td>
<td>
<input type="text" id="cal-field-1" name="itemDate"/>
			<button type="submit" id="cal-button-1">Date</button>
			<script type="text/javascript">
			Calendar.setup({
			inputField : "cal-field-1",
			button : "cal-button-1",
			align : "Tr"
			});
			</script>
			</td>
</tr>

<tr>
<td>Book Category: </td>
<td><select name="itemCategory">
			<option value="">SELECT ONE</option>
			<option value="HUMANITIES">HUMANITIES</option>
			<option value="BUSINESS & MANAGEMENT">BUSINESS & MANAGEMENT</option>  
			<option value="FINANCE">FINANCE</option>
			<option value="SPORT">SPORT</option> 
			<option value="LANGUAGE">LANGUAGE</option>
			<option value="SCHOOL REFERENCE">SCHOOL REFERENCE</option>  
			<option value="GENERAL">GENERAL</option>
			<option value="MAGAZINE">MAGAZINE</option>
			<option value="CHILDREN">CHILDREN</option>  
			<option value="MANDARIN">MANDARIN</option>
			<option value="MALAY">MALAY</option>
</td>			
</tr>

<tr>
<td>Book Selling Price: </td>
<td>RM <input type="text" name="itemPrice" size="10" maxlength="10"></td>
</tr>

<tr>
<td>Book Summary: </td>
<td><textarea name="itemSummary" id="itemSummary" rows="6" cols="50"></textarea></td>
</tr>

<tr>
<td>Quantity: </td>
<td><input type="text" name="itemQuantity" size="3" maxlength="3" value="0"></td>
</tr>

<tr>
<td>Mininum Quantity: </td>
<td><input type="text" name="itemMin" size="3" maxlength="3" value="0"></td>
</tr>

<tr>
<td>Location: </td>
<td><input type="text" name="itemLocation" size="10" maxlength="5"></td>
</tr>

<tr>
<td colspan="4" align="center"><input type="submit" name="Save" value="Save"></td>
</tr>
</table>

</fieldset>
</form>
</center>
</body>
</html>
<?php
}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>