<?
	include("head.php");
	include("../include/db.inc.php");
	include("../include/pager.php");		
	require_once("../include/bit_functions.inc.php");
	$db1=new db;
    $db1->open();mysql_query("SET NAMES 'utf8'");
if($_SESSION['uname']=='' || $_SESSION['pwd']=='')
		{
			header("location:login.php");
		}
		
if(isset($_POST['submit']))
{
		$cname=$_POST['cat_name'];
		$pcat=$_POST['category'];
		$subcat=$_POST['subcategory'];
		$subcat2=$_POST['subcategory2'];
		if($subcat!=0 && $subcat2!='')
		$top=$subcat2;
		if($subcat2!=0)
		$top=$subcat;
		else if($pcat!='0')
		$top=$pcat;
				else
			$top=0;
	 	 $sql_chk="select * from add_category where name='$cname' and top='$top'";
		$res_chk=mysql_query($sql_chk);
		
				
				if(mysql_num_rows($res_chk)>0)
				{
					$err_msg="Sorry! Another Category with same name already exists at this level! Please try another name.";
				}
				else
				{
					
						 $sql="insert into add_category VALUES ('', '$cname', '$top','')";
					  
					
					$x=@mysql_query($sql);
					
					if($x)
					{
					?>
						<script language="javascript">
							//alert("Category added successfully!");
							window.location.href="addcategory.php?msg=Category added successfully!";
						</script>
					<?
					}
					else
					{
					    $errmsg="Category could not be added!\nPlease try again!";
					?>
						 <script language="javascript">
							//alert("Category could not be added!\nPlease try again!");
							//window.location.href="add_tutocategory.php");
						</script>
					<?
					}
				}}

if(!empty($_GET['dele']))
{
$id = $_GET['dele'];
mysql_query("delete from add_category where id='$id'") or die(mysql_error());
header("location:addcategory.php");
//exit();
}
if(!empty($_GET['edit']))
{
$id = $_GET['edit'];
$ids = $_GET['edit1'];
$query = ("select id, name,image from add_category where id='$id'") or die(mysql_error());
if (!($res=@mysql_query($query))) {error(mysql_error(),__LINE__,__FILE__);}
list($id,$name,$image) = mysql_fetch_array($res);
$edit = 1;
$text = "Edit";
}
else
{
$edit = 0;
$text = "Add";
}
if(!isset($_GET['edit'])) {
$id = $_GET['edit'];

}

	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<? $table='<table width="99%" border="1" cellspacing="0" cellpadding="10">
  <tr  style="color:#FFFFFF; background-color:#333333">
    <td width="24%">CATEGORIES</td>
    <td width="15%">SUB CATEGORIE 1 </td>
    <td width="16%">SUB CATEGORIE 2 </td>
    <td width="15%">SUB CATEGORIE 3 </td>
    <td width="15%">SUB CATEGORIE 4 </td>
    <td width="15%">SUB CATEGORIE 5 </td>
  </tr>';
  
  $table.="<tr><td><table>";
  $qry="select * from add_category where top=0";
		  	$rst=mysql_query($qry);
 	 while($mycat=mysql_fetch_array( $rst)){
		  		$table.='<tr height="25"><td>:-&nbsp;&nbsp;<a href="add_product.php?subcat='.$mycat[id].'" class="link2"><strong>'.$mycat[name].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="addcategory.php?edit='.$mycat[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addcategory.php?del='.$mycat[id].'">Delete</a>)</td></tr>';		  	  
	//Level 1 ----------------------------------				
				$qry="select * from add_category where top=".$mycat[id];
				$rst1=mysql_query($qry);
				$table.="<td><table>";	
					while($mycat1=mysql_fetch_array($rst1)){
					$table.='<tr height="25"><td style="padding-left:15px"><li><a href="addcategory.php?supplier='.$sid.'&subcat='.$mycat1[id].'"><strong>'.$mycat1[name].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href=addcategory.php?edit1='.$mycat1[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addcategory.php?del1='.$mycat1[id].'">Delete</a>)</td></tr>';
		//Level 2 ----------------------------------
			$table.="<td><table>";	
			$qry="select * from add_category where top=".$mycat1[id];
					$rst2=mysql_query($qry);				
					while($mycat2=mysql_fetch_array($rst2)){
						$table.='<tr height="25"><td style="padding-left:30px"><li><a href="addcategory.php?supplier='.$sid.'&subcat='.$mycat2[id].'"><strong>'.$mycat2[name].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="addcategory.php?edit2='.$mycat2[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addategory.php?&del='.$mycat2[id].'">Delete</a>)
						</td></tr>';
		//Level 3 -------------------------------
			$table.="<td><table>";	
			$qry="select * from add_category where top=".$mycat2[id];
						$rst3=mysql_query($qry);				
						while($mycat3=mysql_fetch_array($rst3)){
							$table.='<tr height="25"><td style="padding-left:45px"><li><a href="addcategory.php?supplier='.$sid.'&subcat='.$mycat3[id].'"><strong>'.$mycat3[category].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="addcategory.php?edit3='.$mycat3[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addcategory.php?del='.$mycat3[id].'">Delete</a>)</td></tr>';			
					//Level 4 ---------------------------------	
						$table.="<td><table>";	
					$qry="select * from add_category where top=".$mycat3[id];
								$rst4=mysql_query();				
								while($mycat4=mysql_fetch_array($rst4)){
									$table.='<tr height="25"><td style="padding-left:55px"><li><a href="addcategory.php?supplier='.$sid.'&subcat='.$mycat4[id].'"><strong>'.$mycat4[category].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="addcategory.php?edit='.$mycat4[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addcategory.php?del='.$mycat4[id].'">Delete</a>)</td></tr>';
							//Level 5 ---------------------------------	
							$table.="<td><table>";			
										$qry="select * from add_category where top=".$mycat4[id];
										$rst5=mysql_query();				
										while($mycat5=mysql_fetch_array($rst5)){
											$table.='<tr height="25"><td style="padding-left:65px"><li><a href="addcategory.php?supplier='.$sid.'&subcat='.$mycat5[id].'"><strong>'.$mycat5[category].'</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="addcategory.php?edit='.$mycat5[id].'" class="">Edit</a>&nbsp;&nbsp;<a href="addcategory.php?del='.$mycat5[id].'">Delete</a>)</td></tr>';
																
												
										}//Level 5	
										$table.="</table></td>";						
												
							}//Level 4	
							$table.="</table></td>";							
							
						}//Level 3	
					$table.="</table></td>";							
									
					 }//Level 2	
					$table.="</table></td>";							
				 }//Level 1	
				$table.="</table></td>";								
		  }//Level 0
		$table.="</table></td>";
			$table.="<tr></table>";
			echo $table;
	
?>
  
</table>
</body>
</html>
