<?php
session_start();

include "../functions.php";

include "../conn.php";

checkAdmin(session_id());

$title = "Categories";
include "header.php";

$catid = $_GET["catid"];
$parentid = $catid;

$res = mysql_query("select catname from cats where catid='$catid'");
if ($row = mysql_fetch_array($res))
{
	$parent = $row[0];
}

$res = mysql_query("select catid, catname, catdesc, catpic from cats where parentid='$catid' order by catname asc");

echo "<table width=80% align=center cellspacing=1 cellpadding=3 border=0>
      <tr><td colspan=5 bgcolor=3366cc style=\"padding-top: 0; padding-bottom: 0; padding-left: 6px; color: ffffff\">Subcategories";
if ($parent)
{
	echo " of <b>$parent</b>";
}

echo "</td></tr>";

if (mysql_num_rows($res))
{
	echo "<tr bgcolor=a0a0a0><th>Name</th><th>Description</th><th>Picture</th><th colspan=2 width=1%>Options</th></tr>";
	$tri = true;
		
	while ($row = mysql_fetch_array($res))
	{
		$catid = $row[0];
		$catname = $row[1];
		$catdesc = $row[2];
		$catpic = $row[3];
		if (strlen($catname) > 25)
		{
			$catname = substr($catname, 0, 22) . "...";
		}
		if (strlen($catdesc) > 50)
		{
			$catdesc = substr($catdesc, 0, 47) . "...";
		}
		if ($tri = ! $tri)
		{
			echo "<tr bgcolor=c9c9c9>";
		}
		else
		{
			echo "<tr bgcolor=e9e9e9>";
		}
			
		echo "<td width=1% valign=top nowrap><a href=products.php?catid=$catid title=\"Subcategories and products\">$catname</a></td>
		      <td valign=top>$catdesc</td>
		      <td width=1% valign=top><img src=\"$catpic\" width=50></td>
		      <td width=1% valign=top><form action=modcat.php method=post><input type=hidden name=catid value=$catid><input type=submit value=Modify></td></form>
		      <td width=1% valign=top><form onSubmit=\"return confirmSubmit('cats')\" action=delcat.php method=post><input type=hidden name=catid value=$catid><input type=hidden name=parentid value=$parentid><input type=submit value=Delete></td></form></tr>";
	}
}
else
{
	echo "<tr><td>currently no categories, <a href=addcat.php?parentid=$catid>click here</a> to add one</td></tr>";
}
echo "</table>";

echo "<p>";

echo "<table width=80% align=center cellspacing=1 cellpadding=3 border=0>
      <tr><td colspan=5 bgcolor=3366cc style=\"padding-top: 0; padding-bottom: 0; padding-left: 6px; color: ffffff\">Products";
if ($parent)
{
	echo " of <b>$parent</b>";
}

echo "</td></tr>";

$catid = $_GET["catid"];

$res = mysql_query("select productid, productname, productdesc, productpic from products where catid='$catid' order by productname asc");
if (mysql_num_rows($res))
{
	echo "<tr bgcolor=a0a0a0><th>Name</th><th>Description</th><th>Picture</th><th colspan=2 width=1%>Options</th></tr>";
	$tri = true;
		
	while ($row = mysql_fetch_array($res))
	{
		$productid = $row[0];
		$productname = $row[1];
		$productdesc = $row[2];
		$productpic = $row[3];
		if (strlen($productname) > 25)
		{
			$productname = substr($productname, 0, 22) . "...";
		}
		if (strlen($productdesc) > 50)
		{
			$productdesc = substr($productdesc, 0, 47) . "...";
		}
		if ($tri = ! $tri)
		{
			echo "<tr bgcolor=c9c9c9>";
		}
		else
		{
			echo "<tr bgcolor=e9e9e9>";
		}
			
		echo "<td width=1% valign=top nowrap>$productname</td>
		      <td valign=top>$productdesc</td>
		      <td width=1% valign=top><img src=\"$productpic\" width=50></td>
		      <td width=1% valign=top><form action=modpro.php method=post><input type=hidden name=productid value=$productid><input type=submit value=Modify></td></form>
		      <td width=1% valign=top><form onSubmit=\"return confirmSubmit('products')\" action=delpro.php method=post><input type=hidden name=catid value=$catid><input type=hidden name=productid value=$productid><input type=submit value=Delete></td></form></tr>";
	}
}
else
{
	echo "<tr><td>currently no products, <a href=addpro.php?catid=" . $_GET["catid"] . ">click here</a> to add one</td></tr>";
}

echo "</table>";

include "footer.php";

function cats($catid=0, $parentid=0, $space=1)
{
	echo "<select name=catid><option value=0>Select Parent Category</option>";
	
	$res = mysql_query("select catid, catname from cats where parentid='$parentid' order by catname asc");
	while ($row = mysql_fetch_array($res))
	{
		if ($row[0] == $catid)
		{
			echo "<option value=$row[0] selected>";
		}
		else
		{
			echo "<option value=$row[0]>";
		}
		for ($i = 0; $i < 2*$space; $i++)
		{
			echo "&nbsp;";
		}
		echo "$row[1]</option>";
		
		$query = "select catid from cats where parentid=$row[0]";
		$newresult = mysql_query($query);
		if (mysql_num_rows($newresult) > 0)
		{
			$space++;
			cats($catid, $row[0], $space);
			$space--;
		}
	}
	
	echo "</select>";
}
?>

<script language="javascript">
function confirmSubmit(para)
{
	if (para == "cats")
	{
		var agree=confirm("Are you sure you want to delete this category, its subcategories and products?");
	}
	else
	{
		var agree=confirm("Are you sure you want to delete this product?");
	}
	
	if (agree)
	{
		return true ;
	}
	else
	{
		return false ;
	}
}
</script>