<?php
require_once('system/func.php');
auth(); // Закроем от не авторизованных
# Настройки #
$id = abs(intval($_GET['id']));
if ($id) {
    $item = mysql_fetch_assoc(mysql_query("SELECT * FROM `inv` WHERE `id` = '" . $id . "'"));
}
# Ошибки #
if (!$item) {
    $_SESSION['msg'] == '<div class="block_lose">Такой вещи не существует</div>';
    header('Location: /inv');
    exit();
}
$title = $item['name'];
require_once('system/header.php');
## Тип вещи ##
switch ($item['tip']) {
    case 'golova':
        $tip = 'Голова';
        break;
    case 'plechi':
        $tip = 'Плечи';
        break;
    case 'tors':
        $tip = 'Торс';
        break;
    case 'perchatki':
        $tip = 'Перчатки';
        break;
    case 'lev':
        $tip = 'Левая рука';
        break;
    case 'prav':
        $tip = 'Правая рука';
        break;
    case 'nogi':
        $tip = 'ноги';
        break;
    case 'obuv':
        $tip = 'Обувь';
        break;
}
## Качество ##
switch ($item['skill']) {
    case '0':
        $kachestvo = '<font color="green"></font>';

        break;
    case '100':
        $kachestvo = '<font color="#1E90FF"></font>';

        break;
    case '500':
        $kachestvo = '<font color="violet"></font>';
        break;

    case '2500':
        $kachestvo = '<font color="gold"> </font>';

        break;
    case '5000':
        $kachestvo = '<font color="Darkorange"> </font>';

        break;

}


switch ($item['skill']) {
    case '0':
        $color = '<font color="green">';
        $bonus = 10;

        break;
    case '100':
        $color = '<font color="#1E90FF">';
        $bonus = 20;
        break;
    case '500':
        $color = '<font color="violet">';
        $bonus = 40;
        break;
    case '2500':
        $color = '<font color="gold"> ';
        $bonus = 70;

        break;

    case '5000':
        $color = '<font color="Darkorange"> ';
        $bonus = 100;

        break;
}

if (isset($_GET['equipItem']) and $myID == $item['id_user']) {
    $inv = mysql_result(mysql_query("SELECT COUNT(*) FROM `inv` WHERE `odeta` = 'net' AND `id_user` = '$myID'"), 0);
    if ($inv == 200) {
        $_SESSION['msg'] = " <div class='block_lose'> Вы не смогли поднять вещь, ваша сумка переполнена";
        header("Location: ?$random");
        exit();
    }


    $item_snar = mysql_query("SELECT * FROM `inv` WHERE `id_user` = '" . $myID . "' AND `tip` = '" . $item['tip'] . "' AND `odeta` = 'da'");
    $item_snar = mysql_fetch_array($item_snar);


    if (isset($item_snar)) {
        mysql_query("update `users` set `str` = '" . (($user['str'] - $item_snar['str']) + $item['str']) . "', `def` = '" . (($user['def'] - $item_snar['def']) + $item['def']) . "', `max_health` = '" . (($user['max_health'] - $item_snar['health']) + $item['health']) . "' where (`id` = '" . $myID . "')");
        mysql_query("update `inv` set `odeta` = 'net' where (`id` = '" . $item_snar['id'] . "')");
        mysql_query("update `inv` set `odeta` = 'da' where (`id` = '" . $item['id'] . "')");


    } //Если нету вообще вещей
    else {
        mysql_query("update `inv` set `odeta` = 'da' where (`id` = '" . $item['id'] . "')");
    }


    $_SESSION['msg'] = 'Вещь одета';
    header('Location: /inv');
    exit();
}


if (isset($_GET['inv']) and $myID == $item['id_user']) {
    mysql_query("update `inv` set `odeta` = 'net' where (`id` = '" . $item['id'] . "')");
    mysql_query("update `users` set 
`str` = '" . ($user['str'] - $item['str']) . "', 
`def` = '" . ($user['def'] - $item['def']) . "', 
`max_health` = '" . ($user['max_health'] - $item['health']) . "' 
where (`id` = '" . $myID . "')");
    $_SESSION['msg'] = ' <div class="block_win">Вещь успешно снята</div>';
    header('Location: /equipment/');
    exit();
}

if (isset($_GET['sellItem']) and $myID == $item['id_user'] and $item['odeta'] == 'net') {
    switch ($item['skill']) {
        case '0':
            $sell = 100;
            break;
        case '100':
            $sell = 1000;
            break;
        case '500':
            $sell = 10000;
            break;
        case '2500':
            $sell = 25000;
            break;
        case '5000':
            $sell = 75000;
            break;

    }
    $item_snar = mysql_query("SELECT * FROM `inv` WHERE `id_user` = '" . $myID . "' AND `tip` = '" . $item['tip'] . "' AND `odeta` = 'da'");
    $item_snar = mysql_fetch_array($item_snar);

    mysql_query("DELETE FROM `inv` where (`id` = '" . $item['id'] . "')");
    //Задание
    $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '13'"));
    $q = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest_user` WHERE `id_quest` = '$quest[id]' AND `id_user` = '$myID'"));
    if ($q['last'] < time()) {
        mysql_query("update `quest_user` set `koll` = `koll`+'1' where (`id` = '" . $q['id'] . "')");
    }


    $log = $item['name'] . ' продано за ' . $sell . ' золота ,  заточка ' . $item['smitch'] . ', бонус  ' . $item['bonus'] . ' , сила  ' . $item['str'] . ' , здоровье  ' . $item['health'] . ', защита  ' . $item['def'] . ' ';
    mysql_query('INSERT INTO `sell_item_log` ( `user`, `text` , `login`) VALUES ("' . $user['id'] . '", "' . $log . '" , "' . $user['login'] . '")');

    mysql_query("update `users` set `gold` = '" . ($user['gold'] + $sell) . "' where (`id` = '" . $myID . "')");
    $_SESSION['msg'] = '<div class="block_win"> Вещь успешно продана</div>';
    header('Location: /inv');
    exit();
}

echo "<div class='block_c'>";
echo $item['name'] . " , $kachestvo</div>";
echo "<div class='oh'><img src='/images/items/" . $item['tip'] . "/" . $item['item'] . ".png'></div>";
echo "<div class='wrapper'>";
echo ico('icons', 'str.png') . " Сила: $item[str]<br>";
echo ico('icons', 'def.png') . " Защита: $item[def]<br>";
echo ico('icons', 'health.png') . " Здоровье: $item[health]<br><br></div>";

echo "<div class='wrapper'>";

## Камни ##
$jewel = null;
switch ($item['jewel']) {


    case '25':
        $jewel = main('jewel/ico', '1.png');
        $color = 'green';
        $name = 'Обычный';
        break;
    case '75':
        $jewel = main('jewel/ico', '2.png');
        $color = '#1E90FF';
        $name = 'Редкий';

        break;
    case '150':
        $jewel = main('jewel/ico', '3.png');
        $color = 'violet';
        $name = 'Эпический';

        break;
    case '275':
        $jewel = main('jewel/ico', '4.png');
        $color = 'yellow';
        $name = 'Уникальный';
        break;
    case '350':
        $jewel = main('jewel/ico', '5.png');
        $color = 'orange';
        $name = 'Легендарный';
        break;
    default:
        $jewel = null;
        $color = 'grey';
        $name = 'Не установлен';
        break;


}


echo color('white') . " $jewel Камень:  </font> <font color='$color'> $name </font> </a><br> " . color('white') . " " . main('icons', 'smitch.png') . " Заточка: + </font>  $item[smitch]     </a><br>
" . color('white') . " " . main('icons', 'level.png') . " Уровень вещи:</font>  $item[bonus] </a></div>";
echo "<div style='clear:both;'></div>";

$item_snars = mysql_query("SELECT * FROM `inv` WHERE `id_user` = '" . $myID . "' AND `tip` = '" . $item['tip'] . "' AND `odeta` = 'da'");
$item_snars = mysql_fetch_array($item_snars);
$param_1 = $item_snars['str'] + $item_snars['def'] + $item_snars['health'];
$param_2 = $item['str'] + $item['def'] + $item['health'];
if ($param_1 > $param_2) {
    echo "<div class='wrapper'><font color='red'>-" . ($param_1 - $param_2) . "</font> к параметрам</div>";
} elseif ($param_1 < $param_2) {
    echo "<div class='wrapper'><font color='lime'>+" . ($param_2 - $param_1) . "</font> к параметрам";
}
switch ($item['skill']) {
    case '0':
        $sell = 100;
        break;
    case '100':
        $sell = 1000;
        break;
    case '500':
        $sell = 10000;
        break;
    case '2500':
        $sell = 25000;
        break;
    case '5000':
        $sell = 75000;
        break;

}
echo div('header');

if ($myID == $item['id_user']) {
    if ($item['odeta'] == 'net' and $user['level'] >= $item['level']) {

        echo "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='/item/$post[id]?equipItem' >Надеть</div></span></span></a><br>";
        echo "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?sellItem' > Продать " . ico('icons', 'gold.png') . " $sell </div></span></span></a>";
    } else {

        echo "<div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a href='?inv'> Снять</div></span></span></a>";

    }

}
echo "</div>";
require_once('system/footer.php');
?>