<?php 
$tariff_id = (isset($_GET['tariff']) ? (int) $_GET['tariff'] : 1);

if (isset($mark_tariff['list'][$tariff_id])) {
  $tariff = $mark_tariff['list'][$tariff_id];
} else {
  $tariff = $mark_tariff['list'][1];
}

$mark = mysql_fetch_assoc(mysql_query("SELECT * FROM `user_set` WHERE `id_user` = '$user[id]' LIMIT 1"));
$coins = ($tariff['days'] * $mark_tariff['coins']);
$rub = ($coins * $coins_tariffs['coins_course']);

if (isset($_GET['bay'])) {
  if ($_GET['bay'] == 'coins') {
    if ($user[$config['coins_table']] < $coins) {
      $err[] = __('У вас не достаточно средств');
    }
    
    if (!isset($err)) {
      mysql_query("UPDATE `user_set` SET `ocenka` = '" . ($time + 60 * 60 * 24 * $tariff['days']) . "' WHERE `id_user` = '$user[id]'");
      mysql_query("UPDATE `user` SET `" . $config['coins_table'] . "` = `" . $config['coins_table'] . "` - '$coins' WHERE `id` = '$user[id]' LIMIT 1");
      $_SESSION['massage'] = __('Услуга успешно подключена');
      $redirect = '?';
    }
  }
  
  if ($_GET['bay'] == 'rub') {
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `payment` WHERE `client_id` = '$user[id]' AND `service` = 'mark' AND `paid` = '0' AND `rub` = '$rub' AND `count` = '$tariff[days]'"), 0) == 0) 
    {
      mysql_query("INSERT INTO `payment` (`client_id`, `time`, `name`, `service`, `wmr`, `count`, `discount`) 
      VALUES ('$user[id]', '$time', 'Услуга 5+ на " . des2num($tariff['days'], $config['days_name']) . "', 'mark', '$rub', '$tariff[days]', '$tariff[discount]')");
    }  
    
    $pays = mysql_fetch_assoc(mysql_query("SELECT * FROM `payment` WHERE `client_id` = '$user[id]' AND `service` = 'mark' AND `paid` = '0' AND `wmr` = '$rub' AND `count` = '$tariff[days]' LIMIT 1"));
    
    $redirect = '/payment/?service=mark&id=' . $pays['id'];
  }
  
  if (!isset($err)) {
    header('Location: ' . $redirect);
    exit;    
  }
}

$set['title'] = __('Покупка услуги "5+"'); 
include_once H.'sys/inc/thead.php';
title();
aut();
err();

$user['money'] = 20;
?>
<div class="panel panel-success margin">
  <div class="panel-heading"><?= __('Покупка услуги "5+"')?></div>
  <div class="panel-body center">
    <img src="/<?= $config['patch_icons']?>/service_1.png" /><br />
    <? if ($mark['ocenka'] < $time) { ?>
    Возможность ставить на фотографиях пользователей оценку 5+. &laquo;5+&raquo; на <?= des2num($tariff['days'], $config['days_name'])?> дней стоит <?= des2num($coins, $config['coins_name'])?>. 
    <a onclick="this.style.display='none';document.getElementById('markForm').style.display='inline'; document.getElementById('markForm').focus(); return false;" href="javascript:;">выбрать другой период</a>
    <form method="GET" style="display: none;" id="markForm">
      <input type="hidden" name="func" value="<?= $func?>">
      <select name="tariff" id="markFormSelect">
        <? foreach($mark_tariff['list'] AS $key => $post) { ?>
          <option value="<?= $key?>" <?= ($key == $tariff_id ? 'selected' : '')?>><?= des2num($post['days'], $config['days_name'])?></option>
        <? } ?>
      </select>
    </form><br />
    <? } ?>
    
    <? if ($mark['ocenka'] >= $time) { ?>
      <div class="alert alert-success">Срок действия услуги &laquo;5+&raquo; истекает <?= vremja($mark['ocenka'])?></div>
      <a href="?" class="btn btn-success">Биллинг</a>
    <? } elseif ($user[$config['coins_table']] >= $coins) { ?>
      
      <hr>
      <h3>Цена <?= des2num($coins, $config['coins_name'])?></h3>
      на счете <?= des2num($user[$config['coins_table']], $config['coins_name'])?><br />
      <hr>
      
      <a href="?func=<?= $func?>&amp;tariff=<?= $tariff_id?>&amp;bay=coins" class="btn btn-success">Заказать</a>
    <? } else { ?>
    
      <hr>
      Заказываем <?= des2num($coins, $config['coins_name'])?><br />
      <h3>за <?= des2num($rub, $config['rub_name'])?></h3>
      <hr>
      
      <a href="?func=<?= $func?>&amp;tariff=<?= $tariff_id?>&amp;bay=rub" class="btn btn-warning">Перейти к оплате</a><br />
      <span class="text-small">Стоимость услуги &laquo;5+&raquo; на <?= des2num($tariff['days'], $config['days_name'])?> дней стоит <?= des2num($coins, $config['coins_name'])?>.</span>
    <? } ?> 
  </div>
</div>

<ol class="breadcrumb">
  <li><img src="/<?= $config['patch_icons']?>/arrow-180.png" /> <a href="?func=menu"><?= __('Биллинг')?></a></li>
  <li class="active"><?= __('Счета')?></li>
</ol>

<script>
  document.getElementById('markFormSelect').addEventListener('change', function(){ this.form.submit(); })
</script>