<?php
require_once('system/func.php');
auth(); // Закроем от гостей


if (isset($_GET['ShopItem'])) {
    $getSale = mysql_fetch_assoc(mysql_query("SELECT * FROM `avenax_admin` WHERE `id` = '1'"));
    $sale = false;
    if (!empty($getSale) && $getSale['sale_item_time'] > time()) {
        $sale = true;
    }

    $id = abs(intval($_GET['ShopItem']));
    if (isset($id)) {
        $komplekt = mysql_fetch_assoc(mysql_query("SELECT * FROM `komplekt` WHERE `id` = '" . $id . "'"));
    }
    if (!$komplekt) {
        header('Location: /shop?act=shop');
        exit();
    }
	if($user['level']<$komplekt['level']){
		$_SESSION['msg']="Не доступно по уровню"; 
		header('Location: /shop?act=shop');
		exit();
	}
    $title = $komplekt['name'];
    require_once('system/header.php');

		
    $cena = 0;
    for ($item = 1; $item < 9; $item ++) {
        $q = mysql_fetch_array(mysql_query("SELECT * FROM `item` WHERE `id_komplekt` = '$id' ORDER BY `id` ASC"));
        $cena += $q['cena'];
    }
    if (isset($_GET['pay'])) {
        if ($user['almaz'] < $cena) {


            $_SESSION['msg'] = "Не хватает " . ico('icons', 'almaz.png') . " " . ($cena - $user['almaz']) . " золота";
            header("Location: ?ShopItem=$id");
            exit();
        }
        if (mysql_result(mysql_query('SELECT COUNT(*) FROM `inv` WHERE `odeta` = "0" AND `id_user` = "' . $myID . '"'), 0) + 8 > 20) {
            $_SESSION['msg'] = 'В сумке не хватает места';
            header("Location: ?ShopItem=$id");
            exit();
        }

        for ($items = 1; $items < 9; $items ++) {
            if ($items == 1) {
                $tip = 'golova';
            }
            if ($items == 2) {
                $tip = 'plechi';
            }
            if ($items == 3) {
                $tip = 'tors';
            }
            if ($items == 4) {
                $tip = 'perchatki';
            }
            if ($items == 5) {
                $tip = 'lev';
            }
            if ($items == 6) {
                $tip = 'nogi';
            }
            if ($items == 7) {
                $tip = 'obuv';
            }
            $item = mysql_fetch_array(mysql_query("SELECT * FROM `item` WHERE `id_komplekt` = '$komplekt[id]' AND `tip` = '$tip'"));
            mysql_query("INSERT INTO `inv` SET 
`id_user` = '$myID', 
`name` = '$item[name]', 
`str` = '$item[str]', 
`def` = '$item[def]', 
`health` = '$item[health]', 
`tip` = '$item[tip]', 
`skill` = '$item[skill]', 
`odeta` = 'net' , 
`item` = '$item[item]'");
        }
        mysql_query("update `users` set `almaz` = '" . ($user['almaz'] - $cena) . "' where (`id` = '" . $myID . "')");
        $_SESSION['msg'] = "$komplekt[name] успешно куплен";
        header('Location: ?');
        exit();
    }
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `item` WHERE `id_komplekt` = '$id'"), 0);
    $q = mysql_query("SELECT * FROM `item` WHERE `id_komplekt` = '$id' ORDER BY `id` ASC");

    $s = mysql_query("SELECT * FROM `item` WHERE `tip` = '$tip' ORDER BY `id` ASC");

    if ($k_post == 0) {
        echo "<div class='block'>В комплекте нет вещей</div>";
    }
    
     
    
    
    
    
    echo "<div class='header'> ";

   // echo "  <br> <div  class='cbtn blue center mt2'>   <span class='cb_end'><span class='cb_ttl'> <a href='?ShopItem=$id&pay'>
    //Купить комплект за ".ico('icons', 'almaz.png')."  $cena</a></div></div></span></span><br>";
    
    if ($sale == true) {
        echo '<div class="block_c">Сейчас на снаряжение <span style="color: orange">' . $getSale['sale_item'] . '%</span>, осталось ' . tl($getSale['sale_item_time'] - time()) . '</div>';
    }
    
    while ($post = mysql_fetch_assoc($q)) {

        if ($sale == true) {
            $value = floor($post['cena']/100*$getSale['sale_item']);
            $post['cena'] = $post['cena'] - $value;
        }

        $r = rand(33123, 312312);

        echo "<div class='text_p1'>";
        $item_snars = mysql_query("SELECT * FROM `inv` WHERE `id_user` = '" . $myID . "' AND `tip` = '" . $post['tip'] . "' AND `odeta` = 'da'");
        $item_snars = mysql_fetch_array($item_snars);


        $param_1 = $item_snars['str'] + $item_snars['def'] + $item_snars['health'];
        $param_2 = $post['str'] + $post['def'] + $post['health'];
        if ($param_1 >= $param_2) {
            $status = "<small><font color='red'>-" . ($param_1 - $param_2) . "</font> </small>";
        } else if ($param_2 > $param_1) {
            $status = " <small><font color='lime'>+" . ($param_2 - $param_1) . "</font></small> ";
        }
        echo "<font color='red'>  $post[name] $status</font><br><br>";
        //echo "<li>  <img src='/images/items/$post[tip]/$post[item].png' class='bord'>  " . ico('icons', 'str.png') . "$post[str]</br>" . ico('icons', 'def.png') . "$post[def]</br>" . ico('icons', 'health.png') . "$post[health]</a></div></br>";
 
// echo "<div class='header'>";
//echo $item['name'] . " , $kachestvo</div>";
echo "<div class='oh'><img src='/images/items/$post[tip]/$post[item].png'></div>";
//echo "<div class='wrapper'>";
echo ico('icons', 'str.png') . " Сила: $post[str]<br>";
echo ico('icons', 'def.png') . " Защита: $post[def]<br>";
echo ico('icons', 'health.png') . " Здоровье: $post[health]<br><br></div>";






        $id = abs(intval($_GET['ShopItem']));
        if ($id) {
            $k = mysql_fetch_assoc(mysql_query("SELECT * FROM `komplekt` WHERE `id` = '" . $id . "'"));
        }
		if($user['level']<$komplekt['level']){
			$_SESSION['msg']="Не доступно по уровню"; 
			header('Location: /shop?act=shop');
			exit();
		}
        //echo "</div>";

        //echo div('header');
        echo "  <a  class='btn_grey' href ='/itemshop/$post[id]?buy'> Купить " . ico('icons', 'almaz.png') . " " . $post['cena'] . "</span></span></a>";


    }

    echo "<div>";

    echo "<a href='?act=skill_$komplekt[skill]' class='main_link'><div class='ssilki_dop p_0'><img src='/images/icons/back.png' width ='28px'> Вернуться назад</div></a>";

    //echo "<div class='ssilki_dop p_0'><a href='?act=skill_$komplekt[skill]'  Вернуться назад</div></a>";
    require_once('system/footer.php');
    exit();
}

switch (isset($_GET['act'])) {


    case 'items':
        $title = 'Магазин снаряжения';
        require_once('system/header.php');

echo div('header');


echo "<div class='main_link'>";
        echo "<li>  <div class='ssilki_dop p_0'><a href='/shop?ShopItem=2?'> <img src='/images/snop/7.png' width ='20px'> " . span('red') . " <font  size='4px'>Эксклюзивные вещи </span></div></div>";
        //echo "</div></div></div>";


//echo div('header');
        if ($user['level'] >= 60) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=147625843?'> <img src='/images/snop/48.png' width ='20px'> " . span('white') . " <font  size='4px'>Сверх- божественные вещи</span></div></div>";
           // echo "</div></div></div>";
        }
        
        
        
       // echo div('header');

        if ($user['level'] >= 50) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=913758321?'> <img src='/images/snop/222.png' width ='20px'> " . span('white') . " <font  size='4px'>Божественные вещи</span></div></div>";
            //echo "</div></div></div>";
        }
        
        
        
        
        
        //echo div('header');

        if ($user['level'] >= 40) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=4?'> <img src='/images/snop/111.png' width ='20px'> " . span('white') . " <font  size='4px'>Легендарные вещи</span></div></div>";
           // echo "</div></div></div>";
        
        }
        
        
        
        
        
        
//echo div('header');

        if ($user['level'] >= 30) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=6?'> <img src='/images/snop/100.png' width ='20px'> " . span('white') . " <font  size='4px'>Уникальные вещи</span></div></div>";
           // echo "</div></div></div>";
        }
//echo div('header');

        if ($user['level'] >= 20) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=546655459?'>  <img src='/images/snop/26.png' width ='20px'> " . span('white') . " <font  size='4px'>Эпические вещи</span></div></div>";
            //echo "</div></div></div>";
        }
//echo div('header');

        if ($user['level'] >= 10) {
            echo "<div class='main_link'>";
            echo "<li><div class='ssilki_dop p_0'><a href='/shop?ShopItem=654654654?'> <img src='/images/snop/10.png' width ='20px'> " . span('white') . " <font  size='4px'>Редкие вещи </span></div></div>";
            //echo "</div></div></div>";
        }
//echo div('header');

        echo "<div class='main_link'>";
        echo "<li>  <div class='ssilki_dop p_0'><a href='/shop?ShopItem=1?'> <img src='/images/snop/1.png' width ='20px'> " . span('white') . " <font  size='4px'>Обычные вещи </span></div></div>";
        echo "</div>";

        break;

    default:
        $title = 'Магазины ';
        require_once('system/header.php');
        echo "<div class='header'> <img src='/images/other/shop.jpeg'> </span>";


        $key = rand(3131, 424234);
        echo '<div class="cntr white m5"><div class="tptrn ib"><div class="p_end">Рыночная площадь</div></div></div></div>';
        echo div('header');

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


        echo "<a href='?act=items'><div class='ssilki_dop p_0'><img src='/images/icons/ItemShop1.png' width ='28px'> " . span('white') . " Cнаряжение  </span></div>";
        echo "</div></div>";
        
        
        echo div('header');

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


        echo "<a href='/buy_rod.php'><div class='ssilki_dop p_0'><img src='/images/icons/ohota.png' width ='28px'> " . span('white') . " Охотничья лавка  </span></div></div>";
        echo "</div></div>";
        
        
        echo div('header');

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


        echo "<a href='/pay.php'><div class='ssilki_dop p_0'><img src='/images/icons/g.png' width ='28px'> " . span('white') . " Обменник  </span></div></div>";
        echo "</div></div>";
        
        
        
        
            

        
        
        
        echo div('header');

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

        echo "<a href='/ability'><div class='ssilki_dop p_0'><img src='/images/icons/ability.png' width ='28px'> " . span('white') . " Умения  </span></div>";
        echo "</div></div>";



echo "<div class='header'><div class='main_link'>";
            

echo "<a href='/smitch/?key=$key'><div class='ssilki_dop p_0'><img src='/images/icons/smitch.png' width ='28px'> ".span('white')." Кузнец  " . color('orange') . " </span></span>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='header'><div class='main_link'>";
echo "<a href='/j/?key=$key'><div class='ssilki_dop p_0'><img src='/images/icons/j.png' width ='28px'> ".span('white')." Ювелир  </span>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='header'><div class='main_link'>";
echo "<a href='/ItemLvl/?key=$key'><div class='ssilki_dop p_0'><img src='/images/icons/ItemLvl.png' width ='28px'> ".span('white')." Улучшение вещей  </span>";
echo "</div>";
echo "</div>";











}

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