<?php require_once("database.php");
ob_start();

function cat_beheer() //category admin
{
	$returns = "";
	if(empty($_GET['do']))
	{
		$nr = 0;

		$tableinfo = "<tr class='titel'>
		<td class='liitem'>Naam Categorie</td>
		<td class='liitem'>wijzig</td><td class='liitem'>verwijder</td></tr>";

		$returns .= "<h1 class='bhrtitle'>Beheer van categoriëen</h1>";

		$categorie = [];

		$categorie = database_categorys(0);

		$returns .= "<table class='itemlist'>";
		$returns .= $tableinfo;
		for($i = 0; $i < count($categorie); $i++)
		{
			$rowClass = 'oneven';
            if ($nr % 2 > 0) {
                $rowClass = 'even';
            }

            $returns .= "<tr class='" . $rowClass . "'>";
			$returns .= "<td class='liitem'>".$categorie[$i]["Naam"]."</td>";
			$returns .= "<td class='liitem'><a href='beheer.php?b=cat&catid=".$categorie[$i]["ID"]."&do=wijzig'>wijzig</a></td>";
			$returns .= "<td class='liitem'><a href='beheer.php?b=cat&catid=".$categorie[$i]["ID"]."&do=verwijder'>verwijder</a></td>";
			$returns .= "</tr>";
			$nr++;
		}
		$returns .= $tableinfo;
		$returns .= "</table>";
		
		return $returns. "" . goback() ."". addnew("beheer.php?b=cat&do=invroegen", "categorie");
	
	}else if(isset($_GET['do'])){

		if($_GET['do'] == 'invroegen'){
			if(isset($_POST['submit']))
			{
				database_create(0);
				exit(header("Location: beheer.php?b=cat"));
			}else{
				$returns .= "<div id='full'><form id='form' action='beheer.php?b=cat&do=invroegen' method='post'>
					<input type='text' name='inputnaam' value='' placeholder='naam categorie' required autofocus>
					<input type='submit' value='Submit' name='submit'>
				</form>";
			}
			return $returns . "" . goback("beheer.php?b=cat"). "</div>";
		}

		else if($_GET['do'] == 'wijzig'){
			if(isset($_POST['submit']))
			{
				database_modify(0, $_GET['catid']);
				exit(header("Location: beheer.php?b=cat"));
			}else{
				$categorie = [];

				$categorie = database_categorys(1, $_GET['catid']);
	
				for($i = 0; $i < count($categorie); $i++)
				{
					if($categorie[$i]["ID"] == $_GET['catid'])
					{
						$returns .= "<div id='full'><form id='form' action='beheer.php?b=cat&catid=".$categorie[$i]["ID"]."&do=wijzig' method='post'>
							<input type='text' name='inputnaam' value='".$categorie[$i]["Naam"]."' required autofocus>
							<input type='submit' value='Submit' name='submit'>
						</form>";
					}
				}
			}
			return $returns . "" . goback("beheer.php?b=cat"). "</div>";
		}

		else if($_GET['do'] == 'verwijder'){
			$categorie = [];
			$categorie = database_categorys(1, $_GET['catid']);

			for($i = 0; $i < count($categorie); $i++)
			{
				if($categorie[$i]["ID"] == $_GET['catid'])
				{
					$returns .= "<div class='tinycontent'>".$categorie[$i]["Naam"]." is verwijderd</div>";
					database_delete(0, $_GET['catid']);
				}
			}
			return $returns .= goback("beheer.php?b=cat");
		}
	}
}

function items_beheer() //stoffen admin
{
	$returns = "";
	if(empty($_GET['do']))
	{
		$nr = 0;

		$tableinfo = "<tr class='titel'>
		<td class='liitem'>Plaatje</td>
		<td class='liitem'>Naam Stof</td>
		<td class='liitem'>Type Stof</td>
		<td class='liitem'>Beschrijving</td>
		<td class='liitem'>Meters beschikbaar</td>
		<td class='liitem'>Meters totaal</td>
		<td class='liitem'>Prijs in &euro;</td>
		<td class='liitem'>wijzig</td></tr>";

		$returns .= "<h1 class='bhrtitle'>Beheer van items</h1>";

		$items = database_shopping(0);

		$returns .= "<table class='itemlist'>";
		$returns .= $tableinfo;
		for($i = 0; $i < count($items); $i++)
		{
			$beschrijving = "";

			//haalt de overgebleven meters op
			$metersavaible = database_getMetersAv($items[$i]["ID"]);

			$totaalmeters = number_format($items[$i]["Meters"], '2', ',', '');
			$prijs = number_format($items[$i]["Prijs"], '2', ',', '');
			$metersavaible = number_format($metersavaible, '2', ',', '');
			
			$cuts = explode(" ", $items[$i]["Beschrijving"]);
			
			if(count($cuts) > 6)
			{
				for($j = 0; $j <= 6; $j++)
				{
					$beschrijving .= $cuts[$j]." ";
				}
				$beschrijving .= $cuts[$j]."...";
			}else{
				$beschrijving = $items[$i]["Beschrijving"];
			}

			/*if ($nr % 10 > 0) {
                $returns .= $tableinfo;
            }*/

			$rowClass = 'oneven';
            if ($nr % 2 > 0) {
                $rowClass = 'even';
            }

            $returns .= "<tr class='" . $rowClass . "'>";
			$returns .= "<td class='liitem'><img src='img/stoffen/".$items[$i]["Plaatje"]."'/></td>";
			$returns .= "<td class='liitem'>".$items[$i]["Naam"]."</td>";
			$returns .= "<td class='liitem'>".$items[$i]["Categorie"]."</td>";
			$returns .= "<td class='liitem'>".$beschrijving."</td>";
			$returns .= "<td class='liitem'>".$metersavaible."m</td>";
			$returns .= "<td class='liitem'>".$totaalmeters."m</td>";
			$returns .= "<td class='liitem'>&euro; ".$prijs."</td>";
			$returns .= "<td class='liitem'><a href='beheer.php?b=items&item=".$items[$i]["ID"]."&do=menu'>wijzig</a></td>";
			$returns .= "</tr>";
			$nr++;
		}
		$returns .= $tableinfo;
		$returns .= "</table>";
		
		return $returns. "" . goback() ."". addnew("beheer.php?b=items&do=invroegen", "stof");
	
	}else if(isset($_GET['do'])){
		if($_GET['do'] == 'menu')
		{
			$items = [];

			$items = database_shopping(1, $_GET['item']);
			
			for($i = 0; $i < count($items); $i++)
			{
				if($items[$i]["ID"] == $_GET['item'])
				{
					require_once("php/shoppingfunc.php"); 

					$returns .= itemBlock(0, $items[$i]["ID"], $items[$i]["Naam"], $items[$i]["Plaatje"], $items[$i]["Beschrijving"], $items[$i]["Meters"], $items[$i]["Prijs"], $items[$i]["Type"], $items[$i]["Categorie"]);

					$returns .= "<div class='item'>";
					$returns .= "<p><a href='beheer.php?b=items&item=".$items[$i]["ID"]."&do=wijzig'>Wijzig</a></p>";
					$returns .= "<p><a href='beheer.php?b=items&item=".$items[$i]["ID"]."&do=verwijder'>Verwijder</a></p>";
					$returns .= "</div>";
					return $returns. "" . goback();
				}
			}

		} else if($_GET['do'] == 'invroegen'){
			if(isset($_POST['submit']))
			{
				database_create(1);
				exit(header("Location: beheer.php?b=items"));
			}else{

				$categorie = [];

				$categorie = database_categorys(0);

				$returns .= "<div id='full'><form id='form' action='beheer.php?b=items&do=invroegen' method='post'>
					<input type='text' name='inputnaam' value='' placeholder='Naam' required autofocus>";
				
				//zet alle beschikbare categoriëen in een lijst
				$returns .= "<select name='inputstoftype'>";

				for($j = 0; $j < count($categorie); $j++)
				{
					$returns .= "<option value='".$categorie[$j]['ID']."'>".$categorie[$j]['Naam']."</option>";
				}
				$returns .= "</select>";
				
				$returns .= "<input type='text' name='inputplaatje' value='default.jpg' required>
					<input type='text' name='inputbeschrijving' value='' placeholder='beschrijving' required>
					<input type='text' name='inputmeters' value='' placeholder='1,00 meter' required>
					<input type='text' name='inputprijs' value='' placeholder='1,00 euro' required>
					<input type='submit' value='Submit' name='submit'>
				</form>";
			}
			return $returns . "" . goback("beheer.php?b=items"). "</div>";

		}else if($_GET['do'] == 'wijzig'){
			if(isset($_POST['submit']))
			{
				database_modify(1, $_GET['item']);

				exit(header("Location: beheer.php?b=items&item=".$_GET['item']."&do=menu"));
			}else{
				$items = [];
				$categorie = [];

				$items = database_shopping(1, $_GET['item']);

				$categorie = database_categorys(0);
	
				for($i = 0; $i < count($items); $i++)
				{
					if($items[$i]["ID"] == $_GET['item'])
					{
						$meters = number_format($items[$i]["Meters"], '2', ',', '');
						$prijs = number_format($items[$i]["Prijs"], '2', ',', '');
						$returns .= "<div id='full'><form id='form' action='beheer.php?b=items&item=".$items[$i]["ID"]."&do=wijzig' method='post'>
							<input type='text' name='inputnaam' value='".$items[$i]["Naam"]."' required autofocus>";
						
						//zet alle beschikbare categoriëen in een lijst
						$returns .= "<select name='inputstoftype'>";

						for($j = 0; $j < count($categorie); $j++)
						{
							$returns .= "<option value='".$categorie[$j]['ID']."'>".$categorie[$j]['Naam']."</option>";
						}
						$returns .= "</select>";
						
						$returns .= "<input type='text' name='inputplaatje' value='".$items[$i]["Plaatje"]."' required>
							<input type='text' name='inputbeschrijving' value='".$items[$i]["Beschrijving"]."' required>
							<input type='text' name='inputmeters' value='".$meters."' required>
							<input type='text' name='inputprijs' value='".$prijs."' required>
							<input type='hidden' name='id' value='".$items[$i]["ID"]."'>
							<input type='submit' value='Submit' name='submit'>
						</form>";
					}
				}
			}
			return $returns . "" . goback("beheer.php?b=items&item=".$_GET['item']."&do=menu"). "</div>";
		}else if($_GET['do'] == 'verwijder'){
			$items = [];
			$items = database_shopping(1, $_GET['item']);

			for($i = 0; $i < count($items); $i++)
			{
				if($items[$i]["ID"] == $_GET['item'])
				{
					$returns .= "<div class='tinycontent'>".$items[$i]["Naam"]." is verwijderd</div>";
					database_delete(1, $_GET['item']);
				}
			}
			return $returns .= goback("beheer.php?b=items");
		}
	}
}

function reserv_beheer() //not done yet
{
	$returns = "";
	if(empty($_GET['do']))
	{
		return $returns. "" . goback();
	
	}else{
		return "actions not yet here".goback();
	}
}

function users_beheer() //user admin
{
	$returns = "";
	if(empty($_GET['do']))
	{
		$nr = 0;

		$tableinfo = "<tr class='titel'>
		<td class='liitem'>Email</td>
		<td class='liitem'>Admin</td>
		<td class='liitem'>wijzig</td>
		<td class='liitem'>verwijder</td>
		</tr>";
		/*<td class='liitem'>Naam Gebruiker</td>
		<td class='liitem'>Beschrijving</td>
		<td class='liitem'>Foto</td>*/

		$returns .= "<h1 class='bhrtitle'>Beheer van users</h1>";

		$gebruiker = [];

		$gebruiker = database_users(0);

		$returns .= "<table class='itemlist'>";
		$returns .= $tableinfo;
		for($i = 0; $i < count($gebruiker); $i++)
		{
			/*$beschrijving = "";
			$cuts = explode(" ", $items[$i]["Beschrijving"]);
			
			if(count($cuts) > 6)
			{
				for($j = 0; $j <= 6; $j++)
				{
					$beschrijving .= $cuts[$j]." ";
				}
				$beschrijving .= $cuts[$j]."...";
			}else{
				$beschrijving = $items[$i]["Beschrijving"];
			}*/

			$rowClass = 'oneven';
            if ($nr % 2 > 0) {
                $rowClass = 'even';
            }

            $returns .= "<tr class='" . $rowClass . "'>";
			//$returns .= "<td class='liitem'>".$items[$i]["Naam"]."</td>";
			$returns .= "<td class='liitem'>".$gebruiker[$i]["Email"]."</td>";
			//$returns .= "<td class='liitem'>".$beschrijving."</td>";
			//$returns .= "<td class='liitem'><img src='img/stoffen/".$items[$i]["Foto"]."'/></td>";
			$returns .= "<td class='liitem'>".$gebruiker[$i]["Admin"]."</td>";
			$returns .= "<td class='liitem'><a href='beheer.php?b=users&user=".$gebruiker[$i]["ID"]."&do=wijzig'>Wijzig</a></td>";
			$returns .= "<td class='liitem'><a href='beheer.php?b=users&user=".$gebruiker[$i]["ID"]."&do=verwijder'>Verwijder</a></td>";
			$returns .= "</tr>";
			$nr++;
		}
		$returns .= $tableinfo;
		$returns .= "</table>";
		
		return $returns. "" . goback();
	
	}else if(isset($_GET['do'])){
		if($_GET['do'] == 'wijzig'){
			if(isset($_POST['submit']))
			{
				database_modify(3, $_GET['user']);
				exit(header("Location: beheer.php?b=users"));
			}else{
				$gebruiker = [];
				
				$gebruiker = database_users(1, $_GET['user']);
	
				for($i = 0; $i < count($gebruiker); $i++)
				{
					if($gebruiker[$i]["ID"] == $_GET['user'])
					{
						$returns .= "<div id='full'><form id='form' action='beheer.php?b=users&user=".$gebruiker[$i]["ID"]."&do=wijzig' method='post'>
							<h3>wijzig ".$gebruiker[$i]["Email"]." z'n administratie rechten (0 is user, 1 is admin)</h3>
							<input type='text' name='inputadmin' value='".$gebruiker[$i]["Admin"]."' required autofocus>
							<input type='submit' value='Submit' name='submit'>
						</form>";
					}
				}
			}
			return $returns . "" . goback("beheer.php?b=users"). "</div>";

		}else if($_GET['do'] == 'verwijder'){
			$gebruiker = [];
			
			$gebruiker = database_users(1, $_GET['user']);

			for($i = 0; $i < count($gebruiker); $i++)
			{
				if($gebruiker[$i]["ID"] == $_GET['user'])
				{
					$returns .= "<div class='tinycontent'>".$gebruiker[$i]["Email"]." is verwijderd</div>";
					database_delete(3, $_GET['user']);
				}
			}
			return $returns .= goback("beheer.php?b=users");
		}
	}
}

//terug button
function goback($dir = "")
{
	if($dir == "" || empty($dir))
	{
		return "<br/><a class='gobackbtn' href='beheer.php'>go back to beheer</a>";
	}else{
		return "<br/><a class='gobackbtn' href='".$dir."'>go back</a>";
	}
	
}

//nieuwe rij button
function addnew($url, $name)
{
	return "<a class='new' href='".$url."'>Maak nieuwe ".$name." aan</a>";
}

ob_flush();