<?php 
only_reg();

if (isset($_GET['id']) && $user['level'] == 4) {
  $id = (int) $_GET['id'];
}

if (isset($id)) {
  $ank = user::get_user($id);
} else {
  $ank = $user;
}

$page = (isset($_GET['page']) ? (int) $_GET['page'] : 1);

if (isset($_GET['act']) && isset($_GET['pays_id'])) {
  $pays_id = (int) $_GET['pays_id'];
  $pays = mysql_fetch_assoc(mysql_query("SELECT * FROM `payment` WHERE `id` = '$pays_id' LIMIT 1"));
  
  if (!isset($pays['id'])) {
    $err[] = __('Счет не найден');
  }
    
  if ($_GET['act'] == 'delete') {

    if ($pays['paid'] == '1') {
      $err[] = __('Нельзя удалить оплаченный счет');
    }
    
    if (!isset($err)) {
      mysql_query("DELETE FROM `payment` WHERE `id` = '$pays_id' LIMIT 1");
      $_SESSION['message'] = __('Счет успешно удален');    
      header('Location: ?func=' . $func . '&id=' . $ank['id'] . '&sort=' . $sort . '&page=' . $page);  
    }
  }
}

$sort = 'unpaid';
$where = ' AND `paid` = 0';

if (isset($_GET['sort']) && preg_match('/^(paid|unpaid|all)$/', $_GET['sort'])) {
  $sort = $_GET['sort'];
  if ($sort == 'paid') {
    $where = ' AND `paid` = 1';
  } elseif ($sort == 'unpaid') {
    $where = ' AND `paid` = 0';
  } else {
    $where = '';
  }
}

$set['title'] = __('Счета') . ($user['id'] != $ank['id'] ? ' ' . $ank['nick'] : ''); 
include_once H.'sys/inc/thead.php';
title();
aut();
err();
?>
<ul class="nav nav-pills">
  <li class="<?= ($sort == 'unpaid' ? 'disabled' : '')?>"><a href="?func=<?= $func?>&amp;sort=unpaid&amp;id=<?= $ank['id']?>"><?= __('Счета')?></a></li>
  <li class="<?= ($sort == 'paid' ? 'disabled' : '')?>"><a href="?func=<?= $func?>&amp;sort=paid&amp;id=<?= $ank['id']?>"><?= __('Оплачено')?></a></li>
  <li class="<?= ($sort == 'all' ? 'disabled' : '')?>"><a href="?func=<?= $func?>&amp;sort=all&amp;id=<?= $ank['id']?>"><?= __('Все')?></a></li>
</ul>
<?

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `payment` WHERE `client_id` = '$ank[id]' $where"), 0);
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];

if ($k_post == 0) {
  ?><div class="alert alert-info"><?= __('Список счетов пуст')?></div><?
}

$q = mysql_query("SELECT * FROM `payment` WHERE `client_id` = '$ank[id]' $where ORDER BY id DESC LIMIT $start, $set[p_str]");

while($post = mysql_fetch_assoc($q)) {
  ?>
  <div class="pays pays-<?= ($post['paid'] == 1 ? 'paid' : 'unpaid')?>">
    <?= user::avatar($post['client_id'], 2)?> <?= user::nick($post['client_id'])?> 
    <span class="pays-time"><?= vremja($post['time'])?></span>
    <span class="pays-coins pull-right"><?= round($post['wmr'], 1)?> руб.</span><br />
    <span class="pays-name">#<?= $post['id']?> <?= $post['paytype']?></span><br />
    <span class="pays-name"><?= $post['name']?></span><br />
    <span class="pays-panel">
      <? if ($post['paid'] == 0) { ?>
        <a href="/payment/?service=<?= $post['service']?>&amp;id=<?= $post['id']?>">
          <img src="/<?= $config['patch_icons']?>/money-arrow.png" /> <?= __('Оплатить')?>
        </a>
        
        <a href="?func=<?= $func?>&amp;sort=<?= $sort?>&amp;id=<?= $ank['id']?>&amp;pays_id=<?= $post['id']?>&amp;act=delete">
          <img src="/<?= $config['patch_icons']?>/money-minus.png" /> <?= __('Удалить')?>
        </a>
      <? } else { ?>
        <img src="/<?= $config['patch_icons']?>/money-coin.png" /> <span class="pays-paid"><?= __('Оплачено')?></span>
      <? } ?>
    </span>
  </div>
  <?
}

if ($k_page > 1) { 
  str('?func=' . $func . '&amp;sort=' . $sort . '&amp;id=' . $ank['id'] . '&amp;', $k_page, $page);
}
?>

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