<?
require_once '../core/system.php';
require_once 'sys.php';
echo only_reg();
if($user['clan_id'] == 0) {
    header("Location: /clan/");
    exit();
}
$header = 'Казна клана';
require_once '../core/head.php';

$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '".$user['clan_id']."' LIMIT 1"));
$g = mysql_fetch_assoc(mysql_query("SELECT SUM(`gold`) AS `sum` FROM `jr_kazna` WHERE `clan_id` = '".$user['clan_id']."' AND `date` >= CURDATE()"));
$limit = 999999 - $g['sum'];
$error = NULL;
if(isset($_POST['submit'])) {
    $kamni = (int) $_POST['kamni'];
    $gold = (int) $_POST['gold'];
    if ($gold <0 )$error='Не верное значение';
    if ($kamni <0 )$error='Не верное значение';
    if($kamni > $user['kamni']) $error = 'Недостаточно камня для пополнения';
    if($gold > $user['gold']) $error = 'Недостаточно золота для пополнения';
    if($gold > $limit) $error = 'Привышение лимита';
    if(!$error) {
        mysql_query("UPDATE `clans` SET `kamni` = `kamni` + $kamni, `gold` = `gold` + $gold WHERE `id` = '".$user['clan_id']."'");
        mysql_query("UPDATE `user` SET `kamni` = `kamni` - $kamni, `gold` = `gold` - $gold WHERE `id` = '".$user['id']."'");
        mysql_query("INSERT INTO `jr_kazna` SET `clan_id` = '".$user['clan_id']."', `user_id` = '".$user['id']."', `gold` = '$gold', `kamni` = '$kamni'");
        header("Location: kazna.php");
        exit();
    }
}
if($error) {
    echo '<div class="player" style="color: red">
    '.$error.'
    </div>';
}
echo '<div class="block_zero">
Казна клана: 
<img src="/images/icon/kamni.png" /> '.$clan['kamni'].' 
<img src="/images/icon/gold.png" /> '.$clan['gold'].'<br/>
У вас на счету:
<img src="/images/icon/kamni.png" /> '.$user['kamni'].' камней 
<img src="/images/icon/gold.png" /> '.$user['gold'].' золота
</div>
<div class="mini-line"></div>
<div class="menuList">
<li>
<a href="kazna_log.php"><img src="/images/icon/arrow.png" alt=""/>Журнал казны</a>
</li>
</div>
<div class="mini-line"></div>
<div class="block_zero">
<form action="" method="post">
<div>
<img src="/images/icon/kamni.png" alt="kamni"/> 
<input type="text" class="text" name="kamni" value="0"/><br/>
<img src="/images/icon/gold.png" alt="gold"/> 
<input type="text" class="text" name="gold" value="0"/><br/>
<span class="btn">
<span class="end"><input class="label" type="submit" name="submit" value="Пополнить">Пополнить</span>
</span>
</div>
</form>
</div>
<div class="mini-line"></div>
<div class="block_zero">Доступно пополнение казны сегодня:<br/>
<span class="medium">Золото: <img src="/images/icon/gold.png" alt=""/> '.$limit.'<br/>
Камень: <img src="/images/icon/kamni.png" alt=""/> неограниченно</span>
</div>
';
include '../core/foot.php';