<?php include('inc/header.php'); ?>
<h1>Settings - Site: Item Shop</h1>
<div class="container">
<h2>Shop Items</h2>
<?php
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
	if(!isset($_GET['page'])) { $_GET['page'] = 1; }
	$page = $_GET['page'];
	$perpage = 20;


	if(isset($_POST['shopitemsbm'])) {
		if($_POST['item'] == '' || $_POST['description'] == '' || $_POST['price'] == '' || $_POST['count'] == '' || $_POST['category'] == '' || $_POST['icon'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			if(!is_numeric($_POST['currency'])) {
				$_POST['currency'] = 'NULL';
			}
			odbc_exec($odbc_connect, '
			INSERT INTO [SHOP_ITEMLIST](
				itemid, name,description,price,
				count,category,icon,effects,currency
			) VALUES(
				\''.mssql_escape_string($_POST['itemid']).'\', \''.mssql_escape_string($_POST['item']).'\', \''.mssql_escape_string($_POST['description']).'\', \''.mssql_escape_string($_POST['price']).'\',
				\''.mssql_escape_string($_POST['count']).'\', \''.mssql_escape_string($_POST['category']).'\', \''.mssql_escape_string($_POST['icon']).'\', \''.mssql_escape_string($_POST['effects']).'\', '.mssql_escape_string($_POST['currency']).'
			)');
			echo '<div class="success">The item has successfully been added!</div>';
		}
	}
	elseif(isset($_POST['saveshopitemsbm'])) {
		if($_POST['edit_item'] == '' || $_POST['edit_description'] == '' || $_POST['edit_price'] == '' || $_POST['edit_count'] == '' || $_POST['edit_category'] == '' || $_POST['edit_icon'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			if(!is_numeric($_POST['edit_currency'])) {
				$_POST['edit_currency'] = 'NULL';
			}
			odbc_exec($odbc_connect, '
				UPDATE [SHOP_ITEMLIST] SET
					itemid=\''.mssql_escape_string($_POST['edit_itemid']).'\',
					name=\''.mssql_escape_string($_POST['edit_item']).'\',
					description=\''.mssql_escape_string($_POST['edit_description']).'\',
					price=\''.mssql_escape_string($_POST['edit_price']).'\',
					count=\''.mssql_escape_string($_POST['edit_count']).'\',
					category=\''.mssql_escape_string($_POST['edit_category']).'\',
					icon=\''.mssql_escape_string($_POST['edit_icon']).'\',
					effects=\''.mssql_escape_string($_POST['edit_effects']).'\',
					currency=' . mssql_escape_string($_POST['edit_currency']) . '
				WHERE mid=\''.mssql_escape_string($_POST['edit_mid']).'\'
			');
			echo '<div class="success">The item has successfully been changed!</div>';
		}
	}


	echo '<span id="text"></span>';
	echo '<div id="box"></div>';
	echo '<table><tr><td><a href="javascript:shopitem_addform();" title="Add" rel="tooltip"><img src="../images/add.png" /></a></td><td>Add Item</td></tr></table>';
	$select = odbc_exec($odbc_connect, 'SELECT * FROM [SHOP_ITEMLIST] ORDER BY mid DESC');
	$resultset = array();
	$counter = 0;
	while($row = odbc_fetch_array($select)) {
		$resultset[$counter++] = $row;
	}
	$all = odbc_exec($odbc_connect, 'SELECT COUNT(*) as count FROM [SHOP_ITEMLIST]');
	$countall = odbc_result($all, 'count');
	$countpages = $countall / $perpage;
	$categories = odbc_exec($odbc_connect, '
		SELECT sub.[sid], sub.[name], main.[name] AS "mainCat" FROM [SHOP_CATEGORIES_SUB] AS "sub"
			LEFT JOIN [SHOP_CATEGORIES_MAIN] AS "main" ON main.[mid] = sub.[mid]
		WHERE sub.[active] = 1 AND main.[active] = 1
		ORDER BY main.mid ASC, sub.[sid] ASC
	');
	echo '<form method="post" id="shopitemaddform" style="display: none;">
		<table id="table">
			<tr>
				<td id="key">Item ID:</td>
				<td id="value"><input type="text" name="itemid" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key">Itemname:</td>
				<td id="value"><input type="text" name="item" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key" style="vertical-align: top;">Beschreibung:</td>
				<td id="value"><textarea name="description" style="width: 204px; height:100px;"></textarea></td>
			</tr>
			<tr>
				<td id="key" style="vertical-align: top;">Effekte:</td>
				<td id="value"><textarea name="effects" style="width: 204px; height:60px;"></textarea></td>
			</tr>
			<tr>
				<td id="key">Währung:</td>
				<td id="value"><select name="currency"><option value="optional">Optional</option><option value="0">dPs</option><option value="1">Votepoints</option></select></td>
			</tr>
			<tr>
				<td id="key">Preis:</td>
				<td id="value"><input type="text" name="price" style="width: 50px;" /></td>
			</tr>
			<tr>
				<td id="key">Menge:</td>
				<td id="value"><input type="text" name="count" style="width: 50px;" /></td>
			</tr>
			<tr>
				<td id="key">Kategorie:</td>
				<td id="value">
					<select name="category" style="width: 200px;">';
				while($category = odbc_fetch_array($categories)) {
					echo '<option value="'.$category['sid'].'">'.$category['mainCat'].' &raquo; '.$category['name'].'</option>';
				}
				echo '</select>
				</td>
			</tr>
			<tr>
				<td id="key">Icon:</td>
				<td id="value"><input type="text" name="icon" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td></td>
				<td id="value"><input type="submit" name="shopitemsbm" value="Add Item" /></td>
			</tr>
		</table>
	</form><br/>';
	echo '<div id="pageinc">';
	echo '<table id="table" width="100%">
	<thead>
		<tr>
			<td>ID</td>
			<td>Name</td>
			<td>Beschreibung</td>
			<td align="center">Menge</td>
			<td align="center">Preis</td>
			<td align="center">Kategorie</td>
			<td align="center">Währung</td>
			<td align="center">Icon</td>
			<td></td>
		</tr>
		</thead>';
	for($i = $perpage * ($page-1);$i < $perpage*$page;$i++)
	{
		if(isset($resultset[$i])) {



			$resCategory = odbc_exec($odbc_connect, 'SELECT sub.[name], main.[name] AS "mainCat" FROM [SHOP_CATEGORIES_SUB] AS "sub" LEFT JOIN [SHOP_CATEGORIES_MAIN] AS "main" ON main.[mid] = sub.[mid] WHERE [sid] = \''.$resultset[$i]['category'].'\'');






			$description = $resultset[$i]['description'];
			if(strlen($description) > 30) {
				$description = substr($description, 0, 30).'...';
			}
			if(is_null($resultset[$i]['currency'])) {$currency = '-';} elseif($resultset[$i]['currency'] == 0) {$currency = '<img src="../../images/icons/shop/price.png" alt="Donatepoints"/>';} elseif($resultset[$i]['currency'] == 1) {$currency = '<img src="../../images/icons/shop/votepoints.png" alt="Votepoints"/>';}
			echo '<tr id="mid_'.$resultset[$i]['mid'].'" class="row">
				<td id="value">'.$resultset[$i]['itemid'].'</td>
				<td id="value">'.$resultset[$i]['name'].'</td>
				<td id="value"><textarea>'.$description.'</textarea></td>
				<td id="value" align="center">'.$resultset[$i]['count'].'</td>
				<td id="value" align="center">'.$resultset[$i]['price'].'</td>
				<td id="value" align="center">'.odbc_result($resCategory, 'mainCat').' &raquo; '.odbc_result($resCategory, 'name').'</td>
				<td id="value" align="center">'.$currency.'</td>
				<td id="value" align="center"><a href="../../images/icons/shop/'.$resultset[$i]['icon'].'" target="_blank"><img src="../../images/icons/shop/'.$resultset[$i]['icon'].'" style="max-width: 20px; max-height: 20px;"></a></td>
				<td id="value"><a href="javascript:shopitem_edit('.$resultset[$i]['mid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png" /></a> <a href="javascript:shopitem_delete('.$resultset[$i]['mid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png" /></a></td>
			</tr>';
		}
	}
	echo '</table><br/>';
	for($i=0; $i < $countpages; $i++) {
		$k = $i + 1;
		if($page == $k) {
			echo '<span id="page"><b>'.$k.'</b></span>';
		} else {
			echo '<span id="page"><a href="shop_items.php?page='.$k.'">'.$k.'</a></span>';
		}
	}
	echo '</div><br />';
?>
</div>
<?php include('inc/footer.php'); ?>