<?PHP
$_OPTIMIZATION["title"] = $lang['market']['title'];
$usid = $_SESSION["user_id"];
$usname = $_SESSION["user"];
$db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
$user_data = $db->FetchArray();
$db->Query("SELECT * FROM db_config WHERE id = '1' LIMIT 1");
$sonfig_site = $db->FetchArray();
$p_convert = 100-$sonfig_site["percent_sell"];
$b_convert = $sonfig_site["percent_sell"];
?>
   <?PHP
            $_OPTIMIZATION["title"] = $lang['store']['title'];
            $usid = $_SESSION["user_id"];
            $db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
            $user_data = $db->FetchArray();
            $db->Query("SELECT * FROM db_config WHERE id = '1' LIMIT 1");
            $sonfig_site = $db->FetchArray();
                if(isset($_POST["sbor"])){
                    if($user_data["last_sbor"] < (time() - 600) ){
                        $_item1 = $func->SumCalc($sonfig_site["a_in_h"], $user_data["a_t"], $user_data["last_sbor"]);
                        $_item2 = $func->SumCalc($sonfig_site["b_in_h"], $user_data["b_t"], $user_data["last_sbor"]);
                        $_item3 = $func->SumCalc($sonfig_site["c_in_h"], $user_data["c_t"], $user_data["last_sbor"]);
                        $_item4 = $func->SumCalc($sonfig_site["d_in_h"], $user_data["d_t"], $user_data["last_sbor"]);
                        $_item5 = $func->SumCalc($sonfig_site["e_in_h"], $user_data["e_t"], $user_data["last_sbor"]);
                        $_item6 = $func->SumCalc($sonfig_site["f_in_h"], $user_data["f_t"], $user_data["last_sbor"]);
                        $db->Query("UPDATE db_users_b SET 
                        a_b = a_b + '$_item1', 
                        b_b = b_b + '$_item2', 
                        c_b = c_b + '$_item3', 
                        d_b = d_b + '$_item4', 
                        e_b = e_b + '$_item5', 
                        f_b = f_b + '$_item6', 
                        all_time_a = all_time_a + '$_item1',
                        all_time_b = all_time_b + '$_item2',
                        all_time_c = all_time_c + '$_item3',
                        all_time_d = all_time_d + '$_item4',
                        all_time_e = all_time_e + '$_item5',
                        all_time_f = all_time_f + '$_item6',
                        last_sbor = '".time()."' 
                        WHERE id = '$usid' LIMIT 1");
                        $message = sprintf($lang['store']['success'],$config->settings['product']);
                        
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',1,6000); }); </script>";
                        $db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
                        $user_data = $db->FetchArray();
                    }else echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$lang['store']['error']}',3,6000); }); </script>";
                }
                $veloc1 = ($sonfig_site["a_in_h"] /3600) * $user_data["a_t"];
                $item1 = $func->SumCalc($sonfig_site["a_in_h"], $user_data["a_t"], $user_data["last_sbor"]);

                $veloc2 = ($sonfig_site["b_in_h"] /3600) * $user_data["b_t"];
                $item2 = $func->SumCalc($sonfig_site["b_in_h"], $user_data["b_t"], $user_data["last_sbor"]);

                $veloc3 = ($sonfig_site["c_in_h"] /3600) * $user_data["c_t"];
                $item3 = $func->SumCalc($sonfig_site["c_in_h"], $user_data["c_t"], $user_data["last_sbor"]);

                $veloc4 = ($sonfig_site["d_in_h"] /3600) * $user_data["d_t"];
                $item4 = $func->SumCalc($sonfig_site["d_in_h"], $user_data["d_t"], $user_data["last_sbor"]);

                $veloc5 = ($sonfig_site["e_in_h"] /3600) * $user_data["e_t"];
                $item5 = $func->SumCalc($sonfig_site["e_in_h"], $user_data["e_t"], $user_data["last_sbor"]);

                $veloc6 = ($sonfig_site["f_in_h"] /3600) * $user_data["f_t"];
                $item6 = $func->SumCalc($sonfig_site["f_in_h"], $user_data["f_t"], $user_data["last_sbor"]);
            ?>
  
 <?PHP
                # Продажа
                if(isset($_POST["sell"])){
                    $all_items = $user_data["a_b"] + $user_data["c_b"] +  $user_data["e_b"] ;
					$all_items1 = $user_data["b_b"] + $user_data["d_b"] +  $user_data["f_b"];
                    if (($all_items > 0 ) || ($all_items1 > 0 )) {
                        $money_add = $func->SellItems($all_items, $sonfig_site["items_per_coin"]);
						$money_add1 = $func->SellItems($all_items1, $sonfig_site["items_per_coin"]);
                        $item1_b = $user_data["a_b"];
                        $item2_b = $user_data["b_b"];
                        $item3_b = $user_data["c_b"];
                        $item4_b = $user_data["d_b"];
                        $item5_b = $user_data["e_b"];
                        $item6_b = $user_data["f_b"];
                        $money_b = ( (100 - $sonfig_site["percent_sell"]) / 100) * $money_add;
						$money_b1 = ( (100 - $sonfig_site["percent_sell"]) / 100) * $money_add1;
                        $money_p = (  ($sonfig_site["percent_sell"]) / 100) * $money_add1;
						$money_p1 = (  ($sonfig_site["percent_sell"]) / 100) * $money_add;
                        # Обновляем юзверя
                        $db->Query("UPDATE db_users_b SET money_b = money_b + '$money_b', money_p1 = money_p1 + '$money_p1', money_b1 = money_b1 + '$money_b1', money_p = money_p + '$money_p', a_b = 0, b_b = 0, c_b = 0, d_b = 0, e_b = 0 , f_b=0
                        WHERE id = '$usid'");
                        $da = time();
                        $dd = $da + 60*60*24*15;
                        # Вставляем запись в статистику
                        $db->Query("INSERT INTO db_sell_items (user, user_id, a_s, b_s, c_s, d_s, e_s, f_s,amount, all_sell, date_add, date_del) VALUES 
                        ('$usname','$usid','$item1_b','$item2_b','$item3_b','$item4_b','$item5_b','$item6_b','$money_add','$all_items','$da','$dd')");
                        $message = sprintf($lang['market']['sold'],$all_items + $all_items1,$config->settings['product'],$money_add+$money_add1,$config->settings['coins']);
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',1,6000); }); </script>";
                        
                        $db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
                        $user_data = $db->FetchArray();
                    }else echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$lang['market']['error']}',3,6000); }); </script>";
                }
                ?>  

<section class="main">

            <div class="gamearea">
               
				              
                <div class="leftnav elven" id="left_nav">
				  <div class="object elven animated slideExpandUp">
                         <div class="sborarea">
						<div class="mt5">
                            
							<form action="" method="post" class="text-center">
                <input id="get_coins" class="btn btn-small mt5" type="submit" name="sbor" class="btn btn-primary" value="Collect Crystals" />
            </form>
						</div>
						<br /><br />
						<div class="mt10">
                            Convert Crystal to Gold/Silver:<br /><br />
							
							<form action="" method="post" class="text-center">
                    <button type="submit" name="sell"  id="sell_coins" class="btn btn-small mt5"><?php echo $lang['btn']['sellAll'];?></button>
                </form>
							
						</div>
						
					</div>	
					</div>	
				   </div>   
				 <div class="field" id="game_field">
                    <br><br><br><br><br><br><br><br><br><br><br>
					
                      <div class="buy_objects1">
					  <div class="object elven animated slideExpandUp">

            </div>
			<div class="object elven animated slideExpandUp">
                <div class="image"><img src="/images/objects/us1.png" class="img-fluid rounded-circle"></div>
				<br>
                <div class="text">
                    <h3 class="h5"><div class="ic-emerald"></div> <span id="counter1"><?php echo $item1;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["a_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["a_b"]; ?></small>
					
                </div>
            </div>

            <div class="object elven animated slideExpandUp">
                <div class="image"><img src="/images/objects/hiz1.png" class="img-fluid rounded-circle"></div>
                <div class="text">
                    <h3 class="h5"><div class="ic-ruby"></div> <span id="counter2"><?php echo $item2;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["b_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["b_b"]; ?></small>
                </div>
            </div>
            <div class="object elven animated slideExpandUp">
                <div class="image"><img src="/images/objects/rez1.png" class="img-fluid rounded-circle"></div>
                <div class="text">
                    <h3 class="h5"><div class="ic-emerald"></div> <span id="counter3"><?php echo $item3;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["c_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["c_b"]; ?></small>
                </div>
            </div>
            
              </div>      
                 
					   <div class="buy_objects1">
					  <div class="object elven animated slideExpandUp">

            </div>
			
            <div class="object elven animated slideExpandUp">
			
                <div class="image"><img src="/images/objects/erez1.png" class="img-fluid rounded-circle"></div>
                <div class="text">
                    <h3 class="h5"><div class="ic-ruby"></div> <span id="counter4"><?php echo $item4;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["d_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["d_b"]; ?></small>
                </div>
            </div>
            <div class="object elven animated slideExpandUp">
                <div class="image"><img src="/images/objects/academy1.png" class="img-fluid rounded-circle"></div>
                <div class="text">
                    <h3 class="h5"><div class="ic-emerald"></div> <span id="counter5"><?php echo $item5;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["e_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["e_b"]; ?></small>
                </div>
            </div>
			
			<div class="object elven animated slideExpandUp">
                <div class="image"><img src="/images/objects/zeml1.png" class="img-fluid rounded-circle"></div>
                <div class="text">
                    <h3 class="h5"><div class="ic-ruby"></div> <span id="counter5"><?php echo $item6;?></span></h3>
                    <small><b><?= $lang['store']['quantity'] ?>:</b> <?=$user_data["f_t"]; ?> builiding</small><br>
					<small><b>In the warehouse:</b> <?=$user_data["f_b"]; ?></small>
                </div>
            </div>
          
              </div>      
                 
						

                                                                 
                    <br><br><br><br><br><br><br><br><br><br><br><br>
                    <div id="nav_panel" class="nav_panel">
					
                        <div class="panel_content">
						
                            <p>The Gold and Silver gained from the sale is distributed between two balances (Selling Balance and Purchase Balance) in the proportion of 50% to the Purchase Balance (Gold or Silver) and 50% to the Balance for selling in Crypto (Silver or Gold) inversely.</p>
                              <p>Conversion Rate:  100 <span class="ic-emerald"></span> = 1 <span class="ic-gold1"></span> = 100 <span class="ic-ruby"></span> = 1 <span class="ic-gold2"></span></p>

                            </div>
                            <div class="pc-message elven" id="msgw1">								
	        <div class="msg-type sm-success"></div>
	        <div class="content">Simple Text Message</div> 
	        <div class="decor middle"></div>
            <div class="decor left"></div>
            <div class="decor right"></div>
	    </div>                   
						
                        
                    </div>
				 <div class="devider bottom"></div>
                        </div>

                    

                   
                    
        </section>