<?php include('inc/header.php'); ?>
<h1>Settings - Site: Item Shop</h1>
<div class="container">
	<span id="text"></span>
	<div id="box"></div>
<?php
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');


	if(isset($_POST['createCategoryMain'])) {

		$errorArray = array();
		$intCheckMID = odbc_result(odbc_exec($odbc_connect, 'SELECT TOP 1 [mid] FROM [SHOP_CATEGORIES_MAIN] ORDER BY [mid] DESC'), 'mid');

		if(!is_numeric($_POST['mid']) OR $_POST['mid'] == $intCheckMID) {
			$errorArray[] = 'Die ID dieser Kategorie ist bereits vergeben.';
		}
		if(!is_numeric($_POST['active']) OR $_POST['active'] > 1) {
			$errorArray[] = 'Überprüfe den Aktivitätswert.';
		}

		if(count($errorArray) > 0) {
			echo createMessage($errorArray, 'fail');
		}
		else {
			@odbc_exec($odbc_connect, '
				INSERT INTO [SHOP_CATEGORIES_MAIN] (
					[mid],
					[name]
				) VALUES (
					' . $_POST['mid'] . ',
					\'' . mssql_escape_string($_POST['name']) . '\'
				)
			');
			echo createMessage('Hauptkategorie wurde erfolgreich angefügt.', 'success');
		}
	}
	if(isset($_POST['updateCategoryMain'])) {
		odbc_exec($odbc_connect, '
			UPDATE [SHOP_CATEGORIES_MAIN]
			SET		[mid] = ' . mssql_escape_string($_POST['mid']) . ',
					[name] = \'' . mssql_escape_string($_POST['name']) . '\',
					[active] = ' . mssql_escape_string($_POST['active']) . '
			WHERE [mid] = \''.mssql_escape_string($_POST['oldmid']).'\'
		');
		echo '<div class="success">Die Hauptkategorie wurde erfolgreich abgespeichert!</div>';
	}
	if(isset($_POST['createCategorySub'])) {

		$mid = intval($_POST['mid']);

		$errorArray = array();
		$intCheckSID = odbc_result(odbc_exec($odbc_connect, 'SELECT TOP 1 [sid] FROM [SHOP_CATEGORIES_SUB] ORDER BY [sid] DESC'), 'sid');
		$intCheckMID = odbc_num_rows(odbc_exec($odbc_connect, 'SELECT TOP 1 [mid] FROM [SHOP_CATEGORIES_MAIN] WHERE [mid] = ' . $mid . ''));

		if(!is_numeric($_POST['sid']) OR $_POST['sid'] == $intCheckSID) {
			$errorArray[] = 'Die ID dieser Kategorie ist bereits vergeben.';
		}
		if($intCheckMID < 0) {
			$errorArray[] = 'Die Überkategorie existiert nicht.';
		}
		if(!is_numeric($_POST['currency']) OR $_POST['currency'] > 1) {
			$errorArray[] = 'Überprüfe den Währungswert.';
		}
		if(!is_numeric($_POST['active']) OR $_POST['active'] > 1) {
			$errorArray[] = 'Überprüfe den Aktivitätswert.';
		}

		if(count($errorArray) > 0) {
			echo createMessage($errorArray, 'fail');
		}
		else {
			@odbc_exec($odbc_connect, '
				INSERT INTO [SHOP_CATEGORIES_SUB] (
					[mid],
					[sid],
					[name],
					[currency],
					[active]
				) VALUES (
					' . $mid . ',
					' . $_POST['sid'] . ',
					\'' . mssql_escape_string($_POST['name']) . '\',
					' . $_POST['currency'] . ',
					' . $_POST['active'] . '
				)
			');
			echo createMessage('Unterkategorie wurde erfolgreich angefügt.', 'success');
		}
	}
	if(isset($_POST['updateCategorySub'])) {
		odbc_exec($odbc_connect, '
			UPDATE [SHOP_CATEGORIES_SUB]
			SET		[mid] = ' . mssql_escape_string($_POST['mid']) . ',
					[sid] = ' . mssql_escape_string($_POST['sid']) . ',
					[name] = \'' . mssql_escape_string($_POST['name']) . '\',
					[currency] = ' . mssql_escape_string($_POST['currency']) . ',
					[active] = ' . mssql_escape_string($_POST['active']) . '
			WHERE [sid] = \''.mssql_escape_string($_POST['oldsid']).'\'
		');
		echo '<div class="success">Die Unterkategorie wurde erfolgreich abgespeichert!</div>';
	}


	$intNewMID = odbc_result(odbc_exec($odbc_connect, 'SELECT TOP 1 [mid]  FROM [SHOP_CATEGORIES_MAIN] ORDER BY [mid] DESC'), 'mid') + 1;
	echo '
		<h2>Hauptkategorien</h2>
		<p><a href="javascript:openNewCategoryMainForm();" title="Add" rel="tooltip"><img src="../images/add.png" /></a> Neue Hauptkategorie hinzufügen</p>
		<form method="post" id="shopCategoryMain" style="display: none;">
			<table id="table">
				<tr>
					<td id="key">ID:</td>
					<td id="value"><input type="text" name="mid" style="width: 200px;" value="' . $intNewMID . '"/></td>
				</tr>
				<tr>
					<td id="key">Name:</td>
					<td id="value"><input type="text" name="name" style="width: 200px;" /></td>
				</tr>
				<tr>
					<td id="key">Aktiv?</td>
					<td id="value"><select name="active"><option value="0">Nein</option><option value="1">Ja</option></select></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="createCategoryMain" value="Erstelle neue Hauptkategorie" /></td>
				</tr>
			</table>
		</form>
		<br/>
		<table style="width: 100%;">
			<thead>
				<tr style="font-wegiht: bold;">
					<td id="key">ID</td>
					<td id="key">Name</td>
					<td id="key">Aktiv?</td>
					<td id="key">Unterkategorien</td>
					<td id="key" style="width: 50px;"></td>
				</tr>
			</thead>';
	$resSelectCategoriesMain = odbc_exec($odbc_connect, 'SELECT * FROM [SHOP_CATEGORIES_MAIN] ORDER BY [mid] ASC');
	while($result = odbc_fetch_array($resSelectCategoriesMain)) {
		$countRows = odbc_result(odbc_exec($odbc_connect, 'SELECT COUNT([mid]) AS "count" FROM [SHOP_CATEGORIES_SUB] WHERE [mid] = ' . $result['mid'] . ''), 'count');
		echo '
		<tr id="maincat_' . $result['mid'] . '" class="row">
			<td id="value">' . $result['mid'] . '</td>
			<td id="value">' . $result['name'] . '</td>
			<td id="value">' . ReturnMsg($result['active']) . '</td>
			<td id="value">' . $countRows . '</td>
			<td id="value">
				<a href="javascript:editCategoryMain('.$result['mid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png" /></a>
				<a href="javascript:deleteCategoryMain('.$result['mid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png" /></a>
			</td>
		</tr>';
	}
	echo '</table>';




$intNewSID = odbc_result(odbc_exec($odbc_connect, 'SELECT TOP 1 [sid]  FROM [SHOP_CATEGORIES_SUB] ORDER BY [sid] DESC'), 'sid') + 1;
$resSelectCategoriesMain = odbc_exec($odbc_connect, 'SELECT * FROM [SHOP_CATEGORIES_MAIN] ORDER BY [mid] ASC');
echo '
		<h2>Unterkategorien</h2>
		<p><a href="javascript:openNewCategorySubForm();" title="Add" rel="tooltip"><img src="../images/add.png" /></a> Neue Unterkategorie erstellen</p>
		<form method="post" id="shopCategorySub" style="display: none;">
			<table id="table">
				<tr>
					<td id="key">ID:</td>
					<td id="value"><input type="text" name="sid" style="width: 200px;" value="' . $intNewSID . '"/></td>
				</tr>
				<tr>
					<td id="key">Hauptkategorie:</td>
					<td id="value"><select name="mid">';
						while($result = odbc_fetch_array($resSelectCategoriesMain)) {echo '<option value="' . $result['mid'] . '">' . $result['name'] . '</option>';}
echo '				</td>
				</tr>
				<tr>
					<td id="key">Name:</td>
					<td id="value"><input type="text" name="name" style="width: 200px;" /></td>
				</tr>
				<tr>
					<td id="key">Währung:</td>
					<td id="value"><select name="currency"><option value="0">dPs</option><option value="1">Votepoints</option></select></td>
				</tr>
				<tr>
					<td id="key">Aktiv?</td>
					<td id="value"><select name="active"><option value="0">Nein</option><option value="1">Ja</option></select></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="createCategorySub" value="Erstelle neue Unterkategorie" /></td>
				</tr>
			</table>
		</form>
		<br/>
		<table style="width: 100%;">
			<thead>
				<tr style="font-wegiht: bold;">
					<td id="key">ID</td>
					<td id="key">Überkategorie</td>
					<td id="key">Name</td>
					<td id="key">Währung</td>
					<td id="key">Aktiv?</td>
					<td id="key" style="width: 50px;"></td>
				</tr>
			</thead>';
$select = odbc_exec($odbc_connect, '
	SELECT main.[name] AS "mainCat", [sid], sub.[name], sub.[currency], sub.[active]
	FROM [SHOP_CATEGORIES_SUB] AS "sub"
		LEFT JOIN [SHOP_CATEGORIES_MAIN] AS "main" ON main.[mid] = sub.[mid]
	ORDER BY sub.[mid] ASC, [sid] ASC
');
while($result = odbc_fetch_array($select)) {
	echo '
		<tr id="subcat_' . $result['sid'] . '" class="row">
			<td id="value">' . $result['sid'] . '</td>
			<td id="value">' . $result['mainCat'] . '</td>
			<td id="value">' . $result['name'] . '</td>
			<td id="value">' . getCurrency($result['currency']) . '</td>
			<td id="value">' . ReturnMsg($result['active']) . '</td>
			<td id="value">
				<a href="javascript:editCategorySub('.$result['sid'].');" title="Bearbeiten" rel="tooltip"><img src="../images/edit.png" /></a>
				<a href="javascript:deleteCategorySub('.$result['sid'].');" title="Löschen" rel="tooltip"><img src="../images/delete.png" /></a>
			</td>
		</tr>';
}
echo '</table>';
?>
</div>
<?php include('inc/footer.php'); ?>