<?php
	
require_once ('includes/config.inc.php'); 
$page_title = 'Edit Item Info';
include ('includes/headr.php');

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Edit Item Info </title>
  <link href="includes/grid.css" rel="stylesheet" type="text/css">
  <link href="includes/profile.css" rel="stylesheet" type="text/css">
  <link href="includes/main.css" rel="stylesheet" type="text/css">
  <link href="includes/panels.css" rel="stylesheet" type="text/css">

 </head>
 <?php if (isset($_GET['id']) && is_numeric($_GET['id'])&& isset($_GET['c'])&& is_numeric($_GET['c'])) { //Make sure there's an item ID and its category!

	$id = (int) $_GET['id'];
	$c = (int) $_GET['c'];
	
	//Edit artwork info	
	if (isset($_POST['submitted1'])) {
		//escape the incoming data
		$title = mysqli_real_escape_string($dbc, $_POST['title']);
		$cat = (int) $_POST['category']; 
		$des = mysqli_real_escape_string($dbc, $_POST['description']);	
		
		$q1 = "UPDATE artwork SET title='$title', category='$cat', description='$des' WHERE artwork_id =$id";
		$r1 = mysqli_query ($dbc, $q1) or trigger_error("Query: $q1\n<br />MySQL Error: " . mysqli_error($dbc));
		
		// Check the results...
		if (mysqli_affected_rows($dbc) == 1) {		
			// Print a message:
			$success = "Artwork info edited successfully!"; 
			}
		else { // Error!
			$success = "Internal error, please try again!";
		}
 }//end submitted1

	//Edit print Info
	if (isset($_POST['submitted2'])) { // Handle the form.
	 //escape the incoming data 
	 $p = (float) $_POST['price'];

	 //Update print info 
		$q2 = "UPDATE canvas_prints SET price ='$p' WHERE canvas_prints.artwork_id='$id'";
		$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
	// Check the results...
		if (mysqli_affected_rows($dbc) == 1) {		
			// Print a message:
			$success = "Print edited successfully!"; 
			}
		else { // Error!
			$success = "Internal error, please try again!"; 
		}
	
}//end submitted2

//text search? redirect!	
if (isset ($_GET['sitesearch'])){
	header('Location: home.php?sitesearch='.$_GET['sitesearch'].'');
}

	
	}//end artwork id
else //if there's no id and category
	{
	// Show an error message.
	//$page_title = 'Error';
	//include ('includes/header.html');
		header('Location: Access Error.php');
	}
?>
 <body>
 <div class="panel" id="artwork">
 <h1 style="text-align:center;">Edit <?php if ($c==1) echo'Artwork'; elseif ($c==2) echo 'Print'; ?>:</h1>
 <?php require_once (MYSQL); 
 if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])) { // Make sure user's logged in!
	 

// Default query for this page:
	
	if ($c==1){//we'll edit the artwork
	$q = "SELECT * FROM artwork WHERE artwork_id = '$id'";
	$r = mysqli_query ($dbc, $q);
	$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

	print'
	<table width=90% border="0" cellpadding="0" cellspacing="5">
	<tr> 
	<td height="22" align="middle" valign="middle">';if (isset($success)) echo $success;
	print'
	<div id=subp1">';
	// Display a header:
	echo '<div align="center"><h2>'.$row['title'].'</h2></div>';	
	// Get the image information and display the image:
	 echo '<div ><img id="gallery" src="'.$row['url'].'"/></div>';			 
	 print'</div></td><td>
	 <table width=40% border="0" cellpadding="0" cellspacing="5">
	 <form enctype="multipart/form-data"  method="post" name="edit1" id="edit1">
	 <tr>
	 <td height="22" align="right" valign="middle"><label for="title" class="label">Title </label></td>
	 <td height="22" align="left" valign="middle"><input name="title" type="text" id="title" value ="'.$row['title'].'" required></td></tr>
	<tr>
	<td height="22" align="right" valign="middle"><label for="category" class="label">Select Category </label></td>
	<td height="22" align="left" valign="middle"><select name="category" id="category" required>
	<option value="">Category</option>
	<option value="1"'; if ($row['category']==1) print 'selected'; print'>Drawing</option>
	<option value="2"'; if ($row['category']==2) print 'selected'; print'>Photography</option>
	<option value="3"'; if ($row['category']==3) print 'selected'; print'>Sculpture</option>
	<option value="4"'; if ($row['category']==4) print 'selected'; print'>Digital</option>
	</select>
	</td>
	</tr> 
	<tr>
	<td height="22" align="right" valign="middle"><label for="category" class="label">
	<label for="description" class="label">Description </label></td>
	<td height="22" align="right" valign="middle">
	<textarea id="des" name = "description" rows = "5" cols = "45">';print $row['description']; print'</textarea></td>
	</tr><tr>
	<td height="22" align="right" colspan="3" valign="middle"><input type="submit" name="submit" id="submit" value="Save changes"></td></tr>
	<tr>
	<td height="22" align="right" colspan="3" valign="middle"><input type="hidden" name="submitted1" value="TRUE" /></form></td></tr>
	</table>
	</td></tr>
	  
	  </table>';
	}
	
	else if ($c==2){// We'll edit the print 
	$q1 = "SELECT title, url, canvas_prints.artwork_id, artwork.artwork_id, price, stock FROM artwork, canvas_prints WHERE canvas_prints.artwork_id=artwork.artwork_id AND artwork.artwork_id='$id'";
	$r1 = mysqli_query ($dbc, $q1) or trigger_error("Query: $q1\n<br />MySQL Error: " . mysqli_error($dbc));
	
		// Fetch the information:
		$row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC);
	
	print'
	<table width=90% border="0" cellpadding="0" cellspacing="5">
	<tr> 
	<td height="22" align="middle" valign="middle">
	<div id=subp1">';
	// Display a header:
	echo '<div align="center"><h2>'.$row1['title'].'</h2></div>';	
	// Get the image information and display the image:
	echo '<div ><img id="editi" src="'.$row1['url'].'"/></div>';			 
	 print'</div></td>
	 <td height="22" align="left" valign="middle"><div id="subp2">
	 <form method="post" name="submit_p" id="submit_p">
	 <p><b>Price:</b> <input type="text" name="price" size="10" maxlength="10" value="'; echo $row1['price']; print'" required/> <small>Do not include the dollar sign or commas.</small></p>	
		<p><b>Number of prints available:</b>'.$row1['stock'].'</p>
	  <div align="center"><input type="submit" name="submit" value="Submit Changes" /></div>
	<input type="hidden" name="submitted2" value="TRUE" />';
	if (isset($success))echo '</br>'.$success;
	print'
	  </form>
	  </div></td>
	  </tr>
	  </table>';

	}
	print'</div>';
	
 }
 else //if user sin't logged in
	{
	// Show an error message.
	//$page_title = 'Error';
	//include ('includes/header.html');
		header('Location: Access Error.php');
	}
?>
 
  <?php mysqli_close($dbc); 
 
 ?>
 </body>
 <?php include ('includes/footer.php'); ?>
</html>
