<?php 
if (isset($_GET['act'])) {
  $act = $_GET['act'];
}

if ($act == 'category') {
  $category = mysql_fetch_assoc(mysql_query("SELECT * FROM `treats_category` WHERE `id` = '$ID' LIMIT 1"));
  $name = text($category['name']);
  $return = '?func=category&id=' . $ID;
  $returnPage = returnPage($category['id'], $category['dir']);
} 

elseif ($act == 'goods') {
  $goods = mysql_fetch_assoc(mysql_query("SELECT * FROM `treats_goods` WHERE `id` = '$ID' LIMIT 1"));
  $name = text($goods['name']);
  $return = '?func=treats&id=' . $ID;
  $returnPage = returnPage($goods['id_category'], $goods['dir']);
}

else {
  header('Location: ?func=category');
  exit;
}

if (isset($_POST['fail']) || $level == 0) {
  header('Location: ' . $return);
  exit;
}

if (isset($_POST['success'])) {
  if ($_SESSION['token'] == $_POST['success']) {
    
    if ($act == 'goods') {
      mysql_query("DELETE FROM `treats` WHERE `id_treats` = '$goods[id]' LIMIT 1");
      mysql_query("DELETE FROM `treats_goods` WHERE `id` = '$goods[id]' LIMIT 1");
      @unlink($config['module_dir'] . '/goods/' . $goods['icon'] . '.png');
      $ID = $goods['id_category'];
      $_SESSION['message'] = __('Товар успешно удален');
    }
    
    if ($act == 'category') {
      mysql_query("DELETE FROM `treats_category` WHERE `dir` like '%$category[id]%'");
      mysql_query("DELETE FROM `treats_category` WHERE `id` = '$category[id]'");
      
      $q = mysql_query("SELECT * FROM `treats_goods` WHERE `dir` like '%/$category[id]/%'");
      
      while ($post = mysql_fetch_assoc($q)) {
        mysql_query("DELETE FROM `treats` WHERE `id_treats` = '$post[id]' LIMIT 1");
        mysql_query("DELETE FROM `treats_goods` WHERE `id` = '$post[id]'");
        @unlink($config['module_dir'] . '/goods/' . $post['icon'] . '.png');
      }  
      
      $ID = $category['id_category'];
      $_SESSION['message'] = __('Категория успешно удалена');
    }
    
    header('Location: ?func=category&id=' . $ID);
    exit;
  }
  
  $err[] = __('Ошибка сессии, подтвердите запрос');
}

$_SESSION['token'] = mt_rand(1, 999);
moduleHeader(__('Подтвердите удаление'));
?>
<div class="mess">
  <center>
    <?= __('Вы действительно желаете удалить')?> <b><?= text($name)?></b><br />
    <form action="?func=delete&amp;act=<?= urlencode($act)?>&amp;id=<?= $ID?>" enctype="multipart/form-data" method="POST">
      <button type="submit" name="success" value="<?= $_SESSION['token']?>"><?= __('Удалить')?></button> 
      <button type="submit" name="fail"><?= __('Отмена')?></button> 
    </form>
  </center>
</div>