<?PHP

$goods = mysql_fetch_assoc(mysql_query("SELECT * FROM `treats_goods` WHERE `id` = '$ID' LIMIT 1"));

if (!isset($goods['id'])) {
  header('Location: ?');
  exit;
}

$returnPage = returnPage($goods['id_category'], $goods['dir']);

if (isset($_POST['success'])) {
  
  if ($user[$config['table_coins']] < $goods['coins']) {
    $err[] = __('У вас не достаточно ') . $config['coins_name'][2];
  }
  
  if (!isset($config['return'][$from])) {
    $err[] = __('Место назначение не найдено');
  }
  
  if (!isset($err)) {
    $msg = prepareMsg();
    
    mysql_query("UPDATE `user` SET `" . $config['table_coins'] . "` = `" . $config['table_coins'] . "` - '" . $goods['coins'] . "' WHERE `id` = '$user[id]' LIMIT 1");
    mysql_query("UPDATE `treats_goods` SET `rating` = `rating` + '0.01' WHERE `id` = '$goods[id]' LIMIT 1");
    
    if (is_file('act/' . $from . '.php')) {
      require 'act/' . $from . '.php';
    }
    
    mysql_query("INSERT INTO `treats` (id_user, id_send, time, msg, type, id_object, id_treats) 
    values('$user[id]', '$anketa[id]', '$time', '" . my_esc($msg) . "', '$from', '$object', '$goods[id]')");

    $msg .= "\r\n";
    $msg .= ':) [url=' . $config['return'][$from] . ']' . $config['return_name'][$from] . '[/url]';
    mysql_query("INSERT INTO `mail` (id_user, id_kont, time, msg) values('$config[id_send_user]', '$anketa[id]', '$time', '" . my_esc($msg) . "')");
    
    $_SESSION['message'] = __('Отправка прошла успешно');
    header('Location: ' . $config['return'][$from]);
    exit;
  }
}

$_SESSION['token'] = mt_rand(1, 999);
moduleHeader($config['module_name']);

?>
<div class="foot">
  <?= $returnPage?>
</div>

<div class="nav2">
  <b><?= text($goods['name'])?></b><br /> 
  <img src="/<?= $config['goods_patch']?>/<?= $goods['icon']?>.png" style="max-width: 70px; max-height: 70px;" /><br /> 
  <?= __('Рейтинг товара')?>: <span class="on"><?= $goods['rating']?>%</span><br />
  <?= __('Стоимость')?>: <span class="on"><?= sclon2num($goods['coins'], $config['coins_name'])?></span><br />
  <?= __('В наличии')?>: <span class="<?= ($user[$config['table_coins']] > $goods['coins'] ? 'on' : 'off')?>"><?= sclon2num($user[$config['table_coins']], $config['coins_name'])?></span><br />
  <?= __('Получатель')?>: <?= $anketa['icon'] . $anketa['link'] . $anketa['online']?><br />
</div>

<? if ($user[$config['table_coins']] > $goods['coins']) { ?>
<form class="mess" action="?func=treats&amp;id=<?= $ID?>&amp;success=<?= $_SESSION['token']?>" enctype="multipart/form-data" method="POST">
  <? if ($goods['coins'] > 0) { ?>
    <?= __('У вас будет списано')?> 
    <span class="off">-<?= sclon2num($goods['coins'], $config['coins_name'])?></span>, 
    <?= __('вы действительно хотите отправить покупку пользователю?')?><br />
  <? } ?>
  
  <div class="cit"><?= user::avatar($config['id_send_user']) . user::nick($config['id_send_user'])?><br /> <?= output_text(prepareMsg())?></div>
  
  <button type="submit" class="btn btn-success" name="success" value="<?= $_SESSION['token']?>"><?= __('Отправить')?></button> 
</form>
<?
}

if ($level == 1) {
  ?>
  <div class="foot">
    <a href="?func=edit&amp;act=goods&amp;id=<?= $goods['id']?>&amp;elid=edit">
      <img src="/<?= $config['icons_patch']?>/folder--pencil.png" /> <?= __('Параметры товара')?>
    </a><br />
    
    <a href="?func=delete&amp;act=goods&amp;id=<?= $goods['id']?>">
      <img src="/<?= $config['icons_patch']?>/folder--minus.png" /> <?= __('Удалить товар')?>
    </a><br />
  </div>
  <?
}
?>
<div class="foot">
  <?= $returnPage?>
</div>