<?php
	session_start();
	include('includes/config.php');
	include('includes/functions.php');
	if ($_COOKIE['oam_username'] != md5($settings['user']) && $_COOKIE['oam_pass'] != md5($settings['pass'])) {
		header("Location:login.php");
	}
	
	
	if ($_GET['action'] == 'clear_search') {
		$_SESSION['search_phrase'] = '';
		$_SESSION['search_id'] = '';
	}
	
	if ($_GET['action'] == 'delete') {
		$delete = mysql_query('
			DELETE FROM oam_listings
			WHERE listings_id = "' . $_GET['id'] . '"');
		$result_div .= getResultDiv('Your listing has been removed','success');
	}
	
	if ($_GET['action'] == 'relist') {
		$result = mysql_query('
			SELECT *
			FROM oam_listings
			WHERE listings_id = "' . $_GET['id'] . '"');
		$prev_info = mysql_fetch_array($result);
		$relisted_times = $prev_info['listings_times_relisted'] + 1;
		$insert = mysql_query('
			INSERT INTO oam_listings SET
			listings_name = "' . $prev_info['listings_name'] . '",
			listings_notes = "(relisted from item #' . $prev_info['listings_id'] . ', time number ' . $relisted_times . ")\n" . $prev_info['listings_notes'] . '",
			listings_bought_price = "' . $prev_info['listings_bought_price'] . '",
			listings_times_relisted = "' . $relisted_times . '",
			listings_shipping_charge = "' . $prev_info['listings_shipping_charge'] . '",
			listings_date_bought = "' . $prev_info['listings_date_bought'] . '"');
		$update = mysql_query('
			UPDATE oam_listings SET
			listings_bought_price = 0,
			listings_sold_price = 0,
			listings_shipping_charge = 0,
			listings_notes = "(relisted as Listing #' . mysql_insert_id() . ")\n" . $prev_info['listings_notes'] . '",
			listings_status = 3
			WHERE listings_id = "' . $_GET['id'] . '"');
		$result_div .= getResultDiv('Your listing has been re-listed','success');
		echo mysql_error();
	}
	
	if ($_POST['action'] == 'update_listings') {
		if (strpos($_POST['update_array'],',')) {
	 		$update_array = explode(',',$_POST['update_array']);
	 	} else {
	 		$update_array[0] = $_POST['update_array'];
	 	}
		$i = 0;
		while ($update_array[$i] != '') {
			if ($_POST['unsold_' . $update_array[$i]] != '' && $_POST['sold_' .  $update_array[$i]] != '') {
				$sold_update = 'listings_sold_date = "' . date('Y-m-d H:i:s') . '",';
			} else {
				$sold_update = '';
			}
			$sql = '
				UPDATE oam_listings
				SET listings_name = "' . $_POST['name_' .  $update_array[$i]] . '",
					listings_notes = "' . magic_quote_alter($_POST['notes_' .  $update_array[$i]]) . '",
					listings_bought_price = "' . $_POST['bought_' .  $update_array[$i]] . '",
					listings_shipping_charge = "' . $_POST['ship_charge_' .  $update_array[$i]] . '",
					listings_listing_fee = "' . $_POST['list_fee_' .  $update_array[$i]] . '",
					' . $sold_update . '
					listings_shipping_fee = "' . $_POST['ship_fee_' .  $update_array[$i]] . '",
					listings_sold_price = "' . $_POST['sold_' .  $update_array[$i]] . '",
					listings_sale_fee = "' . $_POST['sale_fee_' .  $update_array[$i]] . '",
					listings_sold_to_name = "' . $_POST['sold_to_' .  $update_array[$i]] . '",
					listings_sold_to_email = "' . $_POST['sold_email_' .  $update_array[$i]] . '",
					listings_status = "' . $_POST['status_' .  $update_array[$i]] . '"
				WHERE listings_id = "' . $update_array[$i] . '"
				';
			
			$update = mysql_query($sql);
				echo mysql_error();
		
			$i++;
		}
		$result_div .= getResultDiv('Your listings have been updated','success');
		
	}
	
	if ($_POST['action'] == 'search') {
		if ($_POST['search_id'] == '') {
			$_SESSION['search_phrase'] = $_POST['search_phrase'];
			$_SESSION['search_id'] = '';
		} else {
			$_SESSION['search_id'] = $_POST['search_id'];
			$_SESSION['search_phrase'] = '';
		}
	}
	if ($_GET['action'] == 'show_type') {
		$_SESSION['show_type'] = $_GET['type'];
	}
	
	if ($_SESSION['search_phrase'] != '') {
		$search_query = ' listings_name LIKE "%' . $_SESSION['search_phrase'] . '%" OR listings_notes LIKE "%' . $_SESSION['search_phrase'] . '%" ';
	}
	$three_months_ago = time() - 60*60*24*30*3;
	$three_months_ago = date('Y-m-d H:i:s',$three_months_ago);
	if ($_SESSION['show_type'] == 'all') {
		$where_query = ' WHERE listings_status != 10 ';
	} elseif ($_SESSION['show_type'] == 'current') {
		$where_query = ' WHERE (listings_status != 3 AND listings_status != 2) ';
	} elseif ($_SESSION['show_type'] == 'threemonths') {
		$where_query = ' WHERE listings_date_bought > "' . $three_months_ago . '" ';
	} elseif ($_SESSION['show_type'] == 'sold') {
		$where_query = ' WHERE (listings_status = 1 OR listings_status = 2) ';
	} else {
		$where_query = ' WHERE listings_status != 10 ';
	}
	if ($search_query != '') {
		$where_query = $where_query . ' and (' . $search_query . ')';
	}
	if ($_SESSION['search_id'] != '') {
		$where_query = ' WHERE listings_id = "' . $_SESSION['search_id'] . '" ';
	}
	$sql = '
		SELECT *
		FROM oam_listings' . $where_query . '
		ORDER BY listings_id DESC';
	$result = mysql_query($sql);
	if (mysql_num_rows($result) < 1) {
		$no_result = '<p><strong>There are no listings matching your search criteria.</strong></p>';
	}
	$update_array = '';
	while ($row = mysql_fetch_array($result)) {
		$update_array .= $comma . $row['listings_id'];
		$comma = ',';
		$row_style = 'main-row-1';
		if ($row['listings_status'] == 1) {
			$row_style = 'main-row-green';
		} elseif ($row['listings_sold_price'] != '0.00' && $row['listings_status'] == 0) {
			$row_style = 'main-row-alert';
		} elseif ($row['listings_status'] == 2) {
			$row_style = 'main-row-done';
		} elseif ($row['listings_status'] == 3) {
			$row_style = 'main-row-not-sold';
		}
		if ($row['listings_sold_date'] != '') {
			$row['listings_sold_date'] = '-';
		} else {
			$row['listings_sold_date'] = date('M j,Y',strtotime($row['listings_sold_date']));
		}
		if ($row['listings_sold_to_email'] != '') {
			if ($row['listings_sold_to_name'] != '') {
				$name = $row['listings_sold_to_name'];
			} else {
				$name = $row['listings_sold_to_email'];
			}
			$sold_name = '<a href="mailto:' . $row['listings_sold_to_email'] . '">' . $name . '</a>';
		} else {
			$sold_name = $row['listings_sold_to_name'];
		}
		if ($row['listings_is_paid'] == 1) {
			$paid_checked = ' checked ';
		} else {
			$paid_checked = '';
		}
		if ($row['listings_is_shipped'] == 1) {
			$shipped_checked = ' checked ';
		} else {
			$shipped_checked = '';
		}
		if ($row['listings_is_unsold'] == 1) {
			$unsold_checked = ' checked ';
		} else {
			$unsold_checked = '';
		}
		if ($row['listings_sold_price']) {
			$unsold_input = '<input type="hidden" name="unsold_' . $row['listings_id'] . '" value="1" />';
		} else {
			$unsold_input = '';
		}
		if ($row['listings_date_bought'] != '0000-00-00 00:00:00') {
			$row['listings_date_bought'] = date('M j,Y',strtotime($row['listings_date_bought']));
		} else {
			$row['listings_date_bought'] = '';
		}
		if ($row['listings_sold_date'] != '0000-00-00 00:00:00') {
			$row['listings_sold_date'] = date('M j,Y',strtotime($row['listings_sold_date']));
		} else {
			$row['listings_sold_date'] = '';
		}
		$result_table .=
			'<tr>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . $row['listings_id'] . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '"><strong>' . $row['listings_name'] . '</strong></td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_bought_price']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_shipping_charge']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_sold_price']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_listing_fee']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_shipping_fee']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . replaceZeros($row['listings_sale_fee']) . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' .$row['listings_date_bought'] . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . $row['listings_date_sold'] . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . $sold_name . '</td>
				<td onclick="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');" class="' . $row_style . '">' . nl2br($row['listings_notes']) . '</td>
				<td class="' . $row_style . '">
				
				<a href="javascript:toggleLayer(\'item_div_' . $row['listings_id'] . '\');"><img src="images/plus.gif" id="listing_img_' .$row['listings_id'] . '" name="listing_img_' .$row['listings_id'] . '" alt="Click to expand" title="Click to expand"></a>
				<div class="hidden" id="item_div_' . $row['listings_id'] . '">
					<div class="update-table-div">' . $unsold_input . '
						<table>
							<tr>
								<td>Name:</td>
								<td><input type="text" name="name_' . $row['listings_id'] . '" size="15" value="' .$row['listings_name'] . '" /></td>
							</tr>
							<tr>
								<td>Bought:</td>
								<td>$<input type="text" name="bought_' . $row['listings_id'] . '" size="7" value="' .$row['listings_bought_price'] . '" /></td>
							</tr>
							<tr>
								<td>Ship Charge:</td>
								<td>$<input type="text" name="ship_charge_' . $row['listings_id'] . '" size="7" value="' . replaceZeros($row['listings_shipping_charge'],1) . '" /></td>
							</tr>
							<tr>
								<td>Sold Price:</td>
								<td>$<input type="text" name="sold_' . $row['listings_id'] . '" size="7" value="' . replaceZeros($row['listings_sold_price'],1) . '" /></td>
							</tr>
							<tr>
								<td>List Fee:</td>
								<td>$<input type="text" name="list_fee_' . $row['listings_id'] . '" size="7" value="' . replaceZeros($row['listings_listing_fee'],1) . '" /></td>
							</tr>
							<tr>
								<td>Ship Fee:</td>
								<td>$<input type="text" name="ship_fee_' . $row['listings_id'] . '" size="7" value="' . replaceZeros($row['listings_shipping_fee'],1) . '" /></td>
							</tr>
							<tr>
								<td>Sale Fee:</td>
								<td>$<input type="text" name="sale_fee_' . $row['listings_id'] . '" size="7" value="' . replaceZeros($row['listings_sale_fee'],1) . '" /></td>
							</tr>
							<tr>
								<td>Sold To:</td>
								<td><input type="text" name="sold_to_' . $row['listings_id'] . '" size="15" value="' . $row['listings_sold_to_name'] . '" /></td>
							</tr>
							<tr>
								<td>Sold Email:</td>
								<td><input type="text" name="sold_email_' . $row['listings_id'] . '" size="15" value="' . $row['listings_sold_to_email'] . '" /></td>
							</tr>
							<tr>
								<td>Status:</td>
								<td>
									<select name="status_' . $row['listings_id'] . '">
										<option value="0" ' . showSelected($row['listings_status'],0) . '>Current Listing</option>
										<option value="1" ' . showSelected($row['listings_status'],1) . '>Paid For</option>
										<option value="2" ' . showSelected($row['listings_status'],2) . '>Shipped</option>
										<option value="3" ' . showSelected($row['listings_status'],3) . '>Unsold</option>
									</select>
								</td>
							</tr>
							<tr>
								<td>Notes:</td>
								<td><textarea name="notes_' . $row['listings_id'] . '" cols="20" rows="4">' . $row['listings_notes'] . '</textarea></td>
							</tr>
						</table>
						<p class="align-center"><input type="submit" value="Save All" /></p>
						<p class="align-center"><a href="index.php?action=relist&id=' . $row['listings_id'] . '">Relist</a></p>
					</div>
				</div>
				</td>
				<td class="' . $row_style . '"><a href="index.php?action=delete&id=' .$row['listings_id'] . '"  onclick="return confirm(\'Are you sure you want to delete this listing?\')">DELETE</a></td>
			</tr>
				';
	}
	if ($_SESSION['search_phrase'] != '' || $_SESSION['search_id'] != '') {
		$clear = ' <a href="index.php?action=clear_search">Clear Search</a> ';
	}
?>
<html>
<head>
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="includes/styles.css">
<script type="text/javascript" src="includes/toggle_div.js"></script>
</head>

<body>
<?php echo $result_div; ?>
<h2>Manage Listings</h2>
<p><a href="add-listings.php">Add New Listings</a> | <a href="index.php">Manage Listings</a> | <a href="stats.php">Earnings Statistics</a> | <a href="login.php">Log Out</a> | <a href="instructions.php">Instructions</a></p>
<?php echo $no_result; ?>
	<form action="index.php" method="post">
		<input type="hidden" name="action" value="search" />
		<div class="search-div">
			<table class="wide">
				<tr>
					<td class="half-width"></td>
					<td>Search Phrase: </td>
					<td><input type="text" size="30" name="search_phrase" value="<?php echo $_SESSION['search_phrase']; ?>" /></td>
					<td> or, ID: </td>
					<td><input type="text" size="6" name="search_id" value="<?php echo $_SESSION['search_id']; ?>" /></td>
					<td><input type="submit" value="Search" /> <?php echo $clear; ?></td>
					<td class="half-width"></td>
				</tr>
			</table>
			<table>
				<tr>
					<td class="half-width"></td>
					<td>
						<a href="index.php?action=show_type&type=threemonths">Show All (3 Months Back)</a> | 
						<a href="index.php?action=show_type&type=current">Show Current Listings</a> | 
						<a href="index.php?action=show_type&type=sold">Show Sold Listings</a> | 
						<a href="index.php?action=show_type&type=all">Show All Listings</a>
					</td>
					<td class="half-width"></td>
				</tr>
			</table>
		</div>
	</form>
	<form action="index.php" method="post">
		<input type="hidden" name="action" value="update_listings" />
		<input type="hidden" name="update_array" value="<?php echo $update_array; ?>" />
		<table class="wide main-table">
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Bought</th>
				<th>Ship<br />Charge</th>
				<th>Sold<br />For</th>
				<th>List<br />Fee</th>
				<th>Ship<br />Fee</th>
				<th>Sale<br />Fee</th>
				<th>List<br />Date</th>
				<th>Sold<br />Date</th>
				<th>Sold<br />To</th>
				<th>Notes</th>
				<th>Edit</th>
				<th>Delete</th>
			</tr>
			<?php echo $result_table; ?>
		</table>
	</form>
	<p>Auction Business Manager Developed by <a href="http://www.small-business-ideas.net">www.Small-Business-Ideas.net</a></p>
</body>
</html>
