<?
include './system/common.php';
include './system/functions.php';
include './system/user.php';    

if(!$user) {
header('location: /');  
exit;}




$title = 'Сумка';
include './system/h.php';  



$wear = _string(_num($_GET['wear']));

if($wear) {
$query = mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$wear.'\' AND `equip` = \'0\' AND `user` = \''.$user['id'].'\'');
$inv = mysql_fetch_array($query);    

  if($inv) {
$item = mysql_fetch_array(mysql_query('SELECT * FROM `items` WHERE `id` = \''.$inv['item'].'\''));  //Новая вещь
$item2 = mysql_fetch_array(mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$user['w_'.$item['w']].'\''));    //Старая вещь

if($user['level'] < $item['level']){
header('Location: /inv/');
$_SESSION['mes'] = mes('У вас маленький уровень!');
exit; }   

if($user['w_'.$item['w']]){//Если вещь одета

mysql_query('UPDATE `users` SET `str` = `str` - '.$item2['_str'].',        
                                `vit` = `vit` - '.$item2['_vit'].',        
								`def` = `def` - '.$item2['_def'].',
      
                         `w_'.$item['w'].'` = "0" WHERE `id` = "'.$user['id'].'" ');
mysql_query('UPDATE `inv` SET `equip` = "0" WHERE `id` = "'.$item2['id'].'" ');

mysql_query('UPDATE `users` SET `str` = `str` + '.$inv['_str'].',        
                                `vit` = `vit` + '.$inv['_vit'].',        
								`def` = `def` + '.$inv['_def'].',
      
                         `w_'.$item['w'].'` = "'.$inv['id'].'" WHERE `id` = "'.$user['id'].'" ');
mysql_query('UPDATE `inv` SET `equip` = "1" WHERE `id` = "'.$inv['id'].'" ');
header('Location: /inv/');
$_SESSION['mes'] = mes('Вещь успешно одета!');
exit; }
      
   

    
mysql_query('UPDATE `users` SET `str` = `str` + '.$inv['_str'].',        
                                `vit` = `vit` + '.$inv['_vit'].',        
								`def` = `def` + '.$inv['_def'].',
      
                         `w_'.$item['w'].'` = \''.$inv['id'].'\' WHERE `id` = \''.$user['id'].'\'');
mysql_query('UPDATE `inv` SET `equip` = \'1\' WHERE `id` = \''.$inv['id'].'\'');
$_SESSION['mes'] = mes('Вещь успешно одета!');
header('location: /inv/');
exit;
   
  
  }
        
}






$unwear = _string(_num($_GET['unwear']));

if($unwear) {
$query = mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$unwear.'\' AND `user` = \''.$user['id'].'\'');
$inv = mysql_fetch_array($query);

if($inv) {
$query = mysql_query('SELECT * FROM `items` WHERE `id` = \''.$inv['item'].'\'');
$item = mysql_fetch_array($query);

    if($user['w_'.$item['w']] && $user['w_'.$item['w']] == $inv['id']) {
    if(mysql_result(mysql_query('SELECT COUNT(*) FROM `inv` WHERE `user` = \''.$user['id'].'\' AND `equip` = \'0\''),0) + 1 > 20){
$_SESSION['mes'] = mes('Ошибка, сумка заполнена!');
header('location: /inv/');
exit;}
	
mysql_query('UPDATE `users` SET `str` = `str` - '.$inv['_str'].',
                                `vit` = `vit` - '.$inv['_vit'].',
                                `def` = `def` - '.$inv['_def'].',
                                      
                         `w_'.$item['w'].'` = \'0\' WHERE `id` = \''.$user['id'].'\'');
mysql_query('UPDATE `inv` SET `equip` = \'0\' WHERE `id` = \''.$inv['id'].'\'');
$_SESSION['mes'] = mes('Вещь успешно снята!');
header('location: /equip/');
exit;

    
}else{
mysql_query('UPDATE `inv` WHERE `id` = \''.$inv['id'].'\'');
$_SESSION['mes'] = mes('Ошибка!');
header('location: /inv/');
exit;
    }
        
  }       

}





  
$sell = _string(_num($_GET['sell']));  
if($sell) {
$query = mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$sell.'\' AND `equip` = \'0\' AND `user` = \''.$user['id'].'\'');
$inv = mysql_fetch_array($query);
  
  if($inv) {

// Задания
$task_id=14;// Разбери 10 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
/////////////////////////////////////////////////
$task_id=26;// Разбери 10 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
/////////////////////////////////////////////////
$task_id=33;// Разбери 10 необычных вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how'] AND $inv['quality'] == '2'){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
/////////////////////////////////////////////////
$task_id=35;// Разбери 35 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
/////////////////////////////////////////////////
$task_id=43;// Разбери 10 редких вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how'] AND $inv['quality'] == '3'){                                      
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
/////////////////////////////////////////////////


$_sell = array(0, 100, 250, 500, 1000, 50000, 50000);
mysql_query('UPDATE `users` SET `s` = `s` + '.$_sell[$inv['quality']].' WHERE `id` = \''.$user['id'].'\'');
mysql_query('DELETE FROM `inv` WHERE `id` = \''.$inv['id'].'\'');
$_SESSION['mes'] = mes('Вещь разобрана!');
header('location: /inv/');
exit;
  }
}


    $sell_clean = _string(_num($_GET['sell_clean']));
  
if($sell_clean) {
  $s=0;  $kol=0;
  $s1=0; $kol1=0;
  $s2=0; $kol2=0;
  $s3=0; $kol3=0;
$__inv = mysql_query('SELECT * FROM `inv` WHERE `user`= \''.$user['id'].'\' AND `quality`<= \''.$sell_clean.'\' AND `equip`="0" ORDER BY `id`');
while($__item = mysql_fetch_array($__inv))
{
$item = mysql_fetch_array(mysql_query('SELECT * FROM `items` WHERE `id` = \''.$__item['item'].'\''));
  if($item['quality'] == 1 OR $item['quality'] == 2 OR $item['quality'] == 3) {
if($item['quality'] == '1'){
$kol1 = mysql_num_rows( mysql_query('SELECT * FROM `inv` WHERE `user`= "'.$user['id'].'" AND `quality`= "1" AND `equip`="0"') );
$s1+=100*$kol1;
}  
if($item['quality'] == '2'){
$kol2 = mysql_num_rows( mysql_query('SELECT * FROM `inv` WHERE `user`= "'.$user['id'].'" AND `quality`= "2" AND `equip`="0"') );
$s2+=250*$kol2;
}  
if($item['quality'] == '3'){
$kol3 = mysql_num_rows( mysql_query('SELECT * FROM `inv` WHERE `user`= "'.$user['id'].'" AND `quality`= "3" AND `equip`="0"') );
$s3+=500*$kol3;
}  
$s+=$s1+$s2+$s3;
$kol+=$kol1+$kol2+$kol3;


// Задания
$task_id=14;// Разбери 10 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+"'.$kol.'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}else{
mysql_query ('UPDATE `task_user` SET `how`="'.$task['how'].'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');	
	}}}
/////////////////////////////////////////////////
// Задания
$task_id=26;// Разбери 30 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+"'.$kol.'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}else{
mysql_query ('UPDATE `task_user` SET `how`="'.$task['how'].'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');	
	}}}
/////////////////////////////////////////////////
$task_id=33;// Разбери 10 необычных вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how'] AND $item['quality'] == '2'){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+"'.$kol2.'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}else{
mysql_query ('UPDATE `task_user` SET `how`="'.$task['how'].'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');	
	}}}
/////////////////////////////////////////////////
$task_id=35;// Разбери 45 любых вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+"'.$kol.'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}else{
mysql_query ('UPDATE `task_user` SET `how`="'.$task['how'].'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');	
	}}}
/////////////////////////////////////////////////
$task_id=43;// Разбери 10 редких вещей
$req = mysql_query('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
    if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
if ($t['how'] < $task['how']){                            
mysql_query ('UPDATE `task_user` SET `how`=`how`+"'.$kol3.'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}else{
mysql_query ('UPDATE `task_user` SET `how`="'.$task['how'].'" WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');	
	}}}
/////////////////////////////////////////////////

    mysql_query('UPDATE `users` SET `s` = `s` + '.$s.' WHERE `id` = "'.$user['id'].'"');
    mysql_query('DELETE FROM `inv` WHERE `user`= "'.$user['id'].'" AND `quality`<= "'.$sell_clean.'" AND `equip`="0"');   
  


   $_SESSION['mes'] = mes('Вы продали '.$kol.' вещей за <img src="/images/ico/png/silver.png" alt="*" width="18"/>'.$s.' серебра');
header('location: /inv/');
 exit;   
}
}
$_SESSION['mes'] = mes('У вас нет вещей для разбора!');
header('location: /inv/');
exit; 
 }












echo '<div class="title">'.$title.'</div>';
     echo' '.$_SESSION['mes'].'  ';
    $_SESSION['mes']=NULL; //Удаляем сесию
	

if(mysql_result(mysql_query('SELECT COUNT(*) FROM `inv` WHERE `user` = \''.$user['id'].'\' AND `equip` = \'0\' '),0) > 0) {
$i = 0;
  
$q = mysql_query('SELECT * FROM `inv` WHERE `user` = \''.$user['id'].'\' AND `equip` = \'0\' ORDER BY `id` DESC');
while($row = mysql_fetch_array($q)) {  
$i++;
$item = mysql_fetch_array(mysql_query('SELECT * FROM `items` WHERE `id` = \''.$row['item'].'\''));

  $quality = [	'1'=>'Обычный',
				'2'=>'Необычный',
				'3'=>'Редкий',
				'4'=>'Эпический',
				'5'=>'Легендарный',
				'6'=>'Мифический'
		];
$quality_color=['1'=>'#999999',
				'2'=>'#B1D689',
				'3'=>'#6BA0E7',
				'4'=>'#C780DB',
				'5'=>'#FF8E94',
				'6'=>'#FE7E01'
		];
    
    if($row['new'] == 0) {
mysql_query('UPDATE `inv` SET `new` = \'1\' WHERE `id` = \''.$row['id'].'\'');
}
      
    echo '<div class="empty_block">
  <table cellpadding="0" cellspacing="0"><tr>
  <td width="15%"><img src="/images/items/'.$row['item'].'.png" alt="*"/></td>
  <td><img src="/images/ico/quality/'.$item['quality'].'.png" alt="*"/> 
  <a href="/item/'.$row['id'].'/">'.$item['name'].'</a> '.($row['smith'] > 0 ? '<font color="#90b0c0"><small>+'.$row['smith'].'</small></font>':'').'<br/>
  <small><font color="#'.(($user['level'] < $item['level']) ? 'c06060':'ffffff').'"><img src="/images/ico/png/up.png" alt="*" width="12"/>'.$item['level'].' ур, </font>
  <font color="'.$quality_color[$item['quality']].'">'.$quality[$item['quality']].' </font> ';
  
    if($user['w_'.$item['w']]) {
$equipitem = mysql_fetch_array(mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$user['w_'.$item['w']].'\''));
if( ( $row['_str'] + $row['_vit'] + $row['_def'] ) - ( $equipitem['_str'] + $equipitem['_vit'] + $equipitem['_def'] ) > 0) echo '<font color=\'#30c030\'>+'.(( $row['_str'] + $row['_vit'] + $row['_def'] ) - ( $equipitem['_str'] + $equipitem['_vit'] + $equipitem['_def'] )).'</font>';
}else{
echo '<font color="#30c030">+'.( $row['_str'] + $row['_vit'] + $row['_def'] ).'</font>';
}




    if($row['rune']) {
      
switch($row['rune']) { 
case 1:
$rune_stats = 15; 
$rune_color = '#999999';
break;
case 2:
$rune_stats = 30; 
$rune_color = '#B1D689';
break;
case 3:
$rune_stats = 60;
$rune_color = '#6BA0E7'; 
break;
case 4:
$rune_stats = 200; 
$rune_color = '#C780DB';
break;
case 5:
$rune_stats = 500; 
$rune_color = '#FF8E94';
break;
case 6:
$rune_stats = 1000; 
$rune_color = '#FE7E01';
break;
}

    
echo '<br/>
<img src="/images/ico/rune/small_'.$row['rune'].'.png" alt="*"/> <font color="'.$rune_color.'">+'.$rune_stats.' к параметрам </font><br/>';

}

    echo '</small></td>
</tr></table>
<div align="center">';

//if(!$user['w_'.$item['w']]) 
echo ' 
'.(($user['level'] < $item['level']) ? '<div class="link_center_h"><a> с '.$item['level'].' уровня </a></div>':'
<div class="link_center"><a href="/inv/wear/'.$row['id'].'/">Надеть</a></div>').' ';
echo '</div></div>';
if($i < mysql_result(mysql_query('SELECT COUNT(*) FROM `inv` WHERE `user` = \''.$user['id'].'\' AND `equip` = \'0\''),0))echo '<div class="line"></div>';

  }

}else{
echo '<div class="empty_block item_center"> Сумка пуста </div>';
}
echo'
<div class="line"></div>
<div class="empty_block">
Продать все: <a href="/inv/sell_clean/3/"><img src="/images/ico/quality/3.png" alt=""/>  Редкие</a> / 
<a href="/inv/sell_clean/2/"><img src="/images/ico/quality/2.png" alt=""/>  Необычные</a> / 
<a href="/inv/sell_clean/1/"><img src="/images/ico/quality/1.png" alt=""/>  Обычные</a>
<br />Выберете тип вещей которые хотите продать
</div>
<div class="line"></div>';
include './system/f.php';
?>