<?php include 'sets.php';?>
<?php
$superuser="0";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Webadmin <?php echo $domain ?></title>
<?php include 'codes.php';?>
</head>
<body>

<?php include 'navbar.php';?>
    
<div class="container">
<div class="frame">
<div class="frametitles fdel">
<?php
$id = $mysqli->real_escape_string(stripslashes($_REQUEST['id']));

$result = $mysqli->query("SELECT insert_id, name FROM categories WHERE insert_id='{$_REQUEST['id']}' AND lang_id=$lang");
$row = $result->fetch_array(); 
$catid=$row['insert_id'];
$category=$row['name'];
?>
<?php echo $admindelete ?> <?php echo $category ?>
</div>
<div class="framein">
<?php if ($id>1) { ?>

<?php
if  (isset($_POST['deletecat'.$catid]) && $catid) {
$catid = $mysqli->real_escape_string(stripslashes($_REQUEST['catid']));

$mysqli->query("DELETE FROM cart WHERE (item_id=ANY (SELECT insert_id FROM items WHERE category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid)))");

$mysqli->query("DELETE FROM groups WHERE category_id=$catid");
$mysqli->query("ALTER TABLE groups AUTO_INCREMENT = 1");
$mysqli->query("DELETE FROM extras WHERE type=1 AND (item_id=ANY (SELECT insert_id FROM items WHERE category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid)))");
$mysqli->query("ALTER TABLE extras AUTO_INCREMENT = 1");
$mysqli->query("DELETE FROM prices WHERE (item_id=ANY (SELECT insert_id FROM items WHERE category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid)))");
$mysqli->query("ALTER TABLE prices AUTO_INCREMENT = 1");
$result10 = $mysqli->query("SELECT * FROM extras WHERE type=0 AND category_id=$catid OR (item_id=ANY (SELECT insert_id FROM items WHERE category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid)))");
while($row = $result10->fetch_array()) {
$pid=$row['id'];

if (file_exists("./".$filepath."photos/".$pid.".jpg")) {
   unlink("./".$filepath."photos/".$pid.".jpg");
   }
}
$mysqli->query("DELETE FROM extras WHERE type=0 AND category_id=$catid OR (item_id=ANY (SELECT insert_id FROM items WHERE category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid)))");
$mysqli->query("ALTER TABLE extras AUTO_INCREMENT = 1");

$result11 = $mysqli->query("SELECT * FROM items WHERE (category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid))");
while($row = $result11->fetch_array()) {
$id=$row['insert_id'];
$file=$row['file'];

if (file_exists("./".$filepath."items/".$id.".jpg")) {
	unlink("./".$filepath."items/".$id.".jpg");
	}
	if (file_exists("./".$filepath."items/files/".$id.".".$file)) {
	unlink("./".$filepath."items/files/".$id.".".$file);
	}
}
$mysqli->query("DELETE FROM items WHERE (category_id=$catid OR category_id=ANY (SELECT insert_id FROM categories WHERE parent=$catid))");
$mysqli->query("ALTER TABLE items AUTO_INCREMENT = 1");

$result12 = $mysqli->query("SELECT * FROM categories WHERE parent=$catid");
while($row = $result12->fetch_array()) {
$catid2=$row['insert_id'];

if (file_exists("./".$filepath."categories/".$catid2.".jpg")) {
unlink("./".$filepath."categories/".$catid2.".jpg");
	}
}
$mysqli->query("DELETE FROM categories WHERE parent=$catid");

if (file_exists("./".$filepath."categories/".$catid.".jpg")) {
	unlink("./".$filepath."categories/".$catid.".jpg");
	}
$mysqli->query("DELETE FROM categories WHERE insert_id=$catid");
$mysqli->query("ALTER TABLE categories AUTO_INCREMENT = 1");
?>
<meta http-equiv="refresh" content="0">  

<?php } else {?>

<?php
$result1 = $mysqli->query("SELECT id FROM items WHERE category_id='$catid' AND lang_id=$lang");
$nrrows1 = mysqli_num_rows($result1);

$result2 = $mysqli->query("SELECT id FROM categories WHERE parent='$catid' AND lang_id=$lang");
$nrrows2 = mysqli_num_rows($result2);
			
$allrows=$nrrows1 + $nrrows2;
?>

<?php if ($catid) { ?>
<form name="forma" method="post" enctype="multipart/form-data">
<input type="hidden" name="catid" value="<?php echo $catid ?>">

     <?php if ($allrows>0) { ?>
       <b> <?php echo $allrows ?> </b>
   <?php echo $admindeltext ?> 
  <?php } ?>

     <hr>
  <div class="divallbuttons">
<div class="divlist"><a href="list-items.php?catid=<?php echo $catid ?>" title="<?php echo $adminlist ?>" class="icstyle1 color1"><span class="icon fa-tasks"></span></a></div>
<div class="divbuttons">
<button type="submit" name="deletecat<?php echo $catid ?>" class="linkoption linkdelete" title="<?php echo $admindelete ?>"><span class="icon fa-trash-o"></span> <?php echo $admindelete ?></button>
</div>
   </div>
   
</form>
<?php } else {?>
<?php echo $admindeleted ?>
<?php } ?>

<?php } ?>

<?php } else {?>
<span class="icon fa-lock big color3"></span>
<?php } ?>  
</div>
</div>
</div>

<?php include 'menudown.php';?>

</body>
</html>