<!--sections -->

<?php if ($srows>0) { ?>   
<div>
<form name="formasection" method="post">
<input type="hidden" name="id" value="<?php echo $id ?>">
<input type="hidden" name="category_id" value="<?php echo $category_id ?>">

<select name="section_id" title="<?php echo $adminselect ?>" class="forms2">
<option value="0"><?php echo $adminregion ?></option>
<?php
$result1sec = $mysqli->query("SELECT * FROM sections WHERE level=1 AND lang_id=$lang ORDER BY priority");
while($row = $result1sec->fetch_array()) {
$secid1=$row['insert_id'];
$section1=$row['name'];
?>
<option value="<?php echo $secid1 ?>" class="strong color1"<?php if ($secid1==$secid) { echo " selected"; }?>>
<?php echo $section1 ?>
</option>

<?php 
$result2sec = $mysqli->query("SELECT * FROM sections WHERE parent=$secid1 AND lang_id=$lang ORDER BY priority");
while($row = $result2sec->fetch_array()) {
$secid2=$row['insert_id'];
$section2=$row['name'];
?>
<option value="<?php echo $secid2 ?>"<?php if ($secid2==$secid) { echo " selected"; }?>> 
----&raquo; <?php echo $section2 ?>
</option>
<?php } ?>
<?php } ?>
</select>
 
<button type="submit" name="changesection<?php echo $id ?>" title="<?php echo $adminadd ?>" class="button buttonsmall"><span class="icon fa-check"></span></button>
</form>

</div>
<?php if ($groupcat==1 AND $typec==2) { ?><hr><?php } ?> 
<?php } ?> 

<?php if ($typec==2) { ?>
<!--groups cat-->

<?php
$rcategories = $mysqli->query("SELECT id FROM categories WHERE insert_id!=$catid AND type<3 AND insert_id>1 AND lang_id=$lang");
$rcategories = mysqli_num_rows($rcategories);

$gcategories = $mysqli->query("SELECT id FROM categories WHERE insert_id!=$catid AND type<3 AND insert_id>1 AND insert_id IN (SELECT category_id FROM groups WHERE item_id=$id) AND lang_id=$lang");
$gcategories = mysqli_num_rows($gcategories);
?>
<?php if ($groupcat==1) { ?>

<div>
<?php if ($rcategories>$gcategories) { ?>
<form name="formacat" method="post">
<input type="hidden" name="id" value="<?php echo $id ?>">

<select name="gcat_id" title="<?php echo $adminselect ?>" class="forms2">
<option value="0"><?php echo $adminaddcat ?></option>
<?php
$result1g = $mysqli->query("SELECT insert_id, name, type FROM categories WHERE level=1 AND type<3 AND insert_id>1 AND insert_id NOT IN (SELECT category_id FROM groups WHERE item_id=$id) AND lang_id=$lang ORDER BY priority");
while($row = $result1g->fetch_array()) {
$catid1=$row['insert_id'];
$category1=$row['name'];
$typec1=$row['type'];
$category1 = mb_substr(strip_tags($category1), 0, 32, 'UTF-8');	
?>
<?php if ($catid1 != $catid ) { ?>
<option value="<?php echo $catid1 ?>" class="strong<?php if ($typec1==2) { echo ' color1'; }?>" > 
<?php echo $category1 ?>
</option>
<?php } ?>

<?php 
$result2g = $mysqli->query("SELECT insert_id, name, type FROM categories WHERE parent=$catid1 AND insert_id>1 AND insert_id NOT IN (SELECT category_id FROM groups WHERE item_id=$id) AND lang_id=$lang ORDER BY priority");
while($row = $result2g->fetch_array()) {
$catid2=$row['insert_id'];
$category2=$row['name'];
$typec2=$row['type'];
$category2 = mb_substr(strip_tags($category2), 0, 30, 'UTF-8');	
?>
<?php if ($catid2 != $catid ) { ?>
<option value="<?php echo $catid2 ?>"<?php if ($typec2==2) { echo ' class="color1"'; }?>> 
----&raquo; <?php echo $category2 ?>
</option>
<?php } ?>

<?php } ?>
<?php } ?>
</select>
 
<button type="submit" name="addcat<?php echo $id ?>" title="<?php echo $adminadd ?>" class="button buttonsmall bgrcolor2"><span class="icon fa-plus"></span></button>
</form>
<?php } ?>

<div>
<?php 
$result3g = $mysqli->query("SELECT insert_id, name, type FROM categories WHERE type<3 AND insert_id IN (SELECT category_id FROM groups WHERE item_id=$id) AND lang_id=$lang ORDER BY priority");
while($row = $result3g->fetch_array()) {
$catida=$row['insert_id'];
$categorya=$row['name'];
$typeca=$row['type'];
?>
<?php
if (isset($_POST['deletecat'.$id.$catida])) {
$mysqli->query("DELETE FROM groups WHERE category_id=$catida AND item_id=$id");
$mysqli->query("ALTER TABLE groups AUTO_INCREMENT = 1");
?>
<meta http-equiv="refresh" content="0">
<?php } ?>
<form name="formagc" method="post">
<input type="hidden" name="deletecat<?php echo $id ?><?php echo $catida ?>" value="submit">
<?php echo $categorya ?>
 <button type="submit" name="deletecat<?php echo $catida ?>" title="<?php echo $admindelete ?>" class="buttonicon color1"><span class="icon fa-trash-o"></span></button>
&nbsp; 
</form>
<?php } ?>
</div>

</div>
<?php } ?>

<!--groups items-->

<?php
$ritems = $mysqli->query("SELECT id FROM items WHERE insert_id!=$id AND category_id IN (SELECT insert_id FROM categories WHERE type=2) AND lang_id=$lang");
$ritems = mysqli_num_rows($ritems);

$gitems = $mysqli->query("SELECT id FROM items WHERE insert_id!=$id AND category_id IN (SELECT insert_id FROM categories WHERE type=2) AND (insert_id IN (SELECT item_id2 FROM groups WHERE item_id=$id) 
OR insert_id IN (SELECT item_id FROM groups WHERE item_id2=$id)) AND lang_id=$lang");
$gitems = mysqli_num_rows($gitems);
?>
<?php if ($groupitem==1) { ?>

<div>
<hr>
<?php if ($ritems>$gitems) { ?>
<form name="formaitem" method="post">
<input type="hidden" name="id" value="<?php echo $id ?>">

<select name="gitem_id" title="<?php echo $adminselect ?>" class="forms2">
<option value="0"><?php echo $adminadditem1 ?></option>
<?php
$result1gi = $mysqli->query("SELECT insert_id, name FROM items WHERE insert_id!=$id AND category_id IN (SELECT insert_id FROM categories WHERE type=2) AND insert_id NOT IN (SELECT item_id2 FROM groups WHERE item_id=$id) 
AND insert_id NOT IN (SELECT item_id FROM groups WHERE item_id2=$id) AND lang_id=$lang ORDER BY category_id, priority");
while($row = $result1gi->fetch_array()) {
$itid1=$row['insert_id'];
$item1=$row['name'];
$item1 = mb_substr(strip_tags($item1), 0, 32, 'UTF-8');	
?>
<option value="<?php echo $itid1 ?>"> <?php echo $item1 ?>
</option>
<?php } ?>
</select>
 
<button type="submit" name="additem<?php echo $id ?>" title="<?php echo $adminadd ?>" class="button buttonsmall bgrcolor2"><span class="icon fa-plus"></span></button>
</form>
<?php } ?>

<div>
<?php 
$result2gi = $mysqli->query("SELECT insert_id, name FROM items WHERE insert_id IN (SELECT item_id2 FROM groups WHERE item_id=$id) AND lang_id=$lang ORDER BY priority");
while($row = $result2gi->fetch_array()) {
$itida=$row['insert_id'];
$itema=$row['name'];
?>
<?php
if (isset($_POST['deleteitemg'.$id.$itida])) {
$mysqli->query("DELETE FROM groups WHERE item_id=$id AND item_id2=$itida");
$mysqli->query("ALTER TABLE groups AUTO_INCREMENT = 1");
?>
<meta http-equiv="refresh" content="0">
<?php } ?>
<form name="formagc" method="post">
<input type="hidden" name="deleteitemg<?php echo $id ?><?php echo $itida ?>" value="submit">
<?php echo $itema ?> 
 <button type="submit" name="deleteitemg<?php echo $itida ?>" title="<?php echo $admindelete ?>" class="buttonicon color1"><span class="icon fa-trash-o"></span></button>
&nbsp; 
</form>
<?php } ?>
</div>

</div>
<?php } ?>

<?php } ?>