<?php
require_once('system/func.php');
auth(); // Закроем от не авторизованных
$title = 'Сундуки';
require_once('system/header.php');
$id = num(($_GET['id']));

if ($id) {
    $q = mysql_fetch_array(mysql_query("SELECT * FROM `sunduk_user` WHERE `id` = '" . $id . "' AND `status` = 'close' AND `id_user` = '$myID' ORDER BY `quatility` DESC"));

    //Цена
    switch ($q['quatility']) {
        case '1':
            $color = '<font color="green">Обычный';
            $price = 500;
            break;
        case '2':
            $color = '<font color="#1E90FF">Редкий';
            $price = 1000;
            break;
        case '3':
            $color = '<font color="violet">Эпический';
            $price = 2500;
            break;
        case '4':
            $color = '<font color="gold"> Уникальный';
            $price = 5000;
            break;
        case '5':
            $color = '<font color="Darkorange">Легендарный ';
            $price = 10000;
            break;
    }


    if (isset($_GET['k'])) {
        mysql_query("DELETE FROM `sunduk_user`  WHERE `id` ='$q[id]' AND `id_user` = '" . $myID . "'");
        $_SESSION['msg'] = "Вы выбросили $color сундук  </font>";
        header('Location: /sunduk/');
        exit();
    }

    if (isset($_GET['open'])) {


        if ($price > $user['gold']) {
            $_SESSION['msg'] = 'Не хватает еще ' . ico('icons', 'gold.png') . ' ' . ($price - $user['gold']) . '  ';
            header('Location: /sunduk/');
            exit();
        }


        //Награда от сундука 
        $RandReward = rand(1, 8);

        switch ($RandReward) {
            //Награда опыт
            case 1:
                $exp = rand(500, 1000) * rand(2, $q['lvl']);
                $log = 'В сундуке вы нашли ' . ico('icons', 'exp.png') . ' ' . $exp . ' опыта';

                if ($user['clan_rang'] > 0) {
                    mysql_query("UPDATE `users` SET `clan_exp` = '" . ($user['clan_exp'] + ceil($exp * $user[trust] / 100)) . "' WHERE `id` = '" . $myID . "' LIMIT 1");
                    mysql_query("UPDATE `clans` SET `exp` = `exp` + '" . ceil($exp * $user[trust] / 100) . "' WHERE `id` = '" . $user['id_clan'] . "' LIMIT 1");
                    mysql_query("UPDATE `users` SET `ResetClanExp` = `ResetClanExp` + '" . ceil($exp * $user[trust] / 100) . "' WHERE `id` = '" . $myID . "' LIMIT 1");

                }
                mysql_query("UPDATE `users`  SET `exp`  = `exp` + '$exp' WHERE  `id` = '" . $myID . "'");
                break;
            //Награда золото
            case 2:
                $log = 'Сундук пуст';
                break;
            //Награда алмазы
            case 3:
                $almaz = 1 * rand(2, $q['lvl']);
                $log = 'В сундуке вы нашли' . ico('icons', 'almaz.png') . ' ' . $almaz . ' алмазы';
                mysql_query("UPDATE `users`  SET `almaz`  = `almaz` + '$almaz' WHERE  `id` = '" . $myID . "'");
                break;
            //Снаряжение
            //case 8:
                //$level = $q['lvl'];
               // $item = mysql_fetch_assoc(mysql_query("SELECT * FROM `item` WHERE `level` = '$q[lvl]' ORDER BY RAND() "));
               // mysql_query("INSERT INTO `inv` SET `id_user` = '" . ($user['id']) . "', `name` = '$item[name]', `str` = '$item[str]', `def` = '$item[def]', `health` = '$item[health]', `tip` = '$item[tip]', `skill` = '$item[skill]',  `level` = '$item[level]', `odeta` = 'net' , `item` = '$item[item]'") or die(mysql_error());
               // $log = '<font color=grey>Новая вещь в твоей сумке</font><br><font color=white>' . $item['name'] . '</font><br><img src="/images/items/' . $item['tip'] . '/' . $item['item'] . '.png"></div>';
                //break;
            //Сундук пуст
            default :
                $gold = rand(100, 1000) * $q['lvl'];
                $log = 'Награда<br>' . ico('icons', 'gold.png') . ' ' . $gold . ' золота';
                mysql_query("UPDATE `users`  SET `gold`  = `gold` + '$gold' WHERE  `id` = '" . $myID . "'");
                break;
        }

        mysql_query("UPDATE `users`  SET `gold`  = `gold` - '$price' WHERE  `id` = '" . $myID . "'");

        $_SESSION['msg'] = "Вы открыли сундук  $color" . $q['name'] . " </font>  за   " . ico('icons', 'gold.png') . " $price </font>";
        $_SESSION['item'] = ' ' . $log;

        //Кампания
        if ($campaign['id_camp'] == 2) {
            mysql_query("UPDATE `campaign`  SET  `sunduk`='" . ($campaign['sunduk'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
        }

        //Кампания
        if ($campaign['id_camp'] == 4) {
            mysql_query("UPDATE `campaign`  SET  `sunduk`='" . ($campaign['sunduk'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
        }

        mysql_query("DELETE FROM `sunduk_user` WHERE `id` ='$q[id]' AND `id_user` = '" . $myID . "'");
        header('Location: /sunduk/');
        exit();
    }
}

$p = mysql_query("SELECT * FROM `sunduk_user` WHERE `status` = 'close'  AND `id_user` = '$myID' ");

while ($post = mysql_fetch_assoc($p)) {

    echo "<div class='wrapper'>";
    ## Качество ##
    switch ($post['quatility']) {
        case '1':
            $color = '<font color="green">Обычный';
            $price = 500;
            break;
        case '2':
            $color = '<font color="#1E90FF">Редкий';
            $price = 1000;
            break;
        case '3':
            $color = '<font color="violet">Эпический';
            $price = 2500;
            break;
        case '4':
            $color = '<font color="gold"> Уникальный';
            $price = 5000;
            break;
        case '5':
            $color = '<font color="Darkorange">Легендарный ';
            $price = 10000;
            break;
    }


    echo "<center><div class='h'></div>$color</font><br><img src='/images/other/chest.png' width ='98px''></center><br></span><center><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'> <a  href='/sunduk/" . $post['id'] . "/?open'>  Открыть " . ico('icons', 'gold.png') . " $price </div></span></span></center></a> 
<center><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'> <a  href='/sunduk/" . $post['id'] . "/?k'> Выбросить </div></span></span></a></div></center>";


}


$q = mysql_fetch_array(mysql_query("SELECT * FROM `sunduk_user` WHERE `status` = 'close'  AND `id_user` = '$myID' "));

if ($q == 0) {
    echo div('header') . " Пусто</div>";
}


require_once('system/footer.php');
?>