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

include ("connect.php");
	$ISBN = $_POST["ISBN"];
	$itemName = $_POST["itemName"];
	$itemAuthor = $_POST["itemAuthor"];
	$itemPublisher = $_POST["itemPublisher"];
	$itemDate = $_POST["itemDate"];
	$itemCategory = $_POST["itemCategory"];
	$itemPrice = $_POST["itemPrice"];
	$itemMin = $_POST["itemMin"];
	$itemSummary = $_POST["itemSummary"];
	$itemLocation = $_POST["itemLocation"];
	$itemMin = $_POST["itemMin"];
	
	if($_POST["Save"] == "Save")
	{
	$result = mysql_query("UPDATE item SET itemName = '$itemName', itemAuthor = '$itemAuthor', itemPublisher = '$itemPublisher', itemDate = '$itemDate', itemCategory = '$itemCategory', itemMin = '$itemMin', itemPrice = '$itemPrice', itemSummary = '$itemSummary', itemLocation = '$itemLocation', itemMin = '$itemMin' WHERE ISBN = '$ISBN'");
	
	
	$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; 
	if($img_url!="./bookimage//"){
	$result = mysql_query("UPDATE item SET itemImage = '$img_url' WHERE ISBN = '$ISBN'");
	
	}
	}
	$db = mysql_close();
	echo "<script language='javascript'>alert('Book Edit Success.')</script>";
	echo "<script language='javascript'>window.location='book.php'</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>
</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">Edit Book</legend>

<table align="center">

<?php
include("connect.php");
$ISBN=$_GET['ISBN'];
$result = mysql_query("SELECT * FROM item WHERE ISBN = '$ISBN'");


$row = mysql_fetch_array($result);

echo "<tr>";
echo "<td>ISBN: </td>";
echo "<td><input type='hidden' name='ISBN' value='$row[ISBN]'>$row[ISBN]";
echo "<font color='red'>&nbsp;&nbsp;&nbsp;ISBN cannot edited.</font>";
echo "</td>";
echo "</tr>";

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

echo "<td width='200' rowspan='5'><img id='imgAvatar' img src='$row[itemImage]'></td>";

echo "</tr>";

echo "<tr>";
echo "<td>Book Name: </td>";
echo "<td><input type='text' name='itemName' size='40' maxlength='255' value='$row[itemName]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Author: </td>";
echo "<td><input type='text' name='itemAuthor' size='40' maxlength='100' value='$row[itemAuthor]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Publisher: </td>";
echo "<td><input type='text' name='itemPublisher' size='40' maxlength='100' value='$row[itemPublisher]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Book Date: </td>";
echo "<td><input type='text' id='cal-field-1' name='itemDate' value='$row[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>";
echo "</tr>";

echo "<tr>";
echo "<td>Book Category: </td>";
echo "<td><select name='itemCategory' value='$row[itemCategory]'>";
$bookArray = array("HUMANITIES", "BUSINESS & MANAGEMENT", "FINANCE", "SPORT", "LANGUAGE", "SCHOOL REFERENCE", "GENERAL", "MAGAZINE", "CHILDREN", "MANDARIN", "MALAY");
	for($j = 0; $j < 11 ; $j++)
	{if($row['itemCategory'] == $bookArray[$j])
	{echo "<option selected>".$bookArray[$j];
	}else
	{echo "<option>".$bookArray[$j];
	}
	}
	echo "</select>"; 
echo "</td>";	
echo "</tr>";

echo "<tr>";
echo "<td>Book Selling Price: </td>";
$price = number_format($row['itemPrice'], 2, '.', '');
echo "<td>RM <input type='text' name='itemPrice' size='10' maxlength='10' value='$price'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Book Summary: </td>";
echo "<td><textarea name='itemSummary' id='itemSummary' rows='6' cols='50'>$row[itemSummary]</textarea></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Location: </td>";
echo "<td><input type='text' name='itemLocation' size='10' maxlength='5' value='$row[itemLocation]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Mininum Quantity: </td>";
echo "<td><input type='text' name='itemMin' size='5' maxlength='3' value='$row[itemMin]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='4' align='center'><input type='submit' name='Save' value='Save'></td>";
echo "</tr>";
echo "</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>";
}
?>