<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<?PHP
$_OPTIMIZATION["title"] = $lang['bonus_daily']['title'];
$usid = (int)$_SESSION["user_id"];
$uname = $_SESSION["user"];
$db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
$user_data = $db->FetchArray();
$minsell=2 ;
$db->Query("SELECT * FROM db_payment WHERE user_id = '$usid' ");
if($db->NumRows() > 0){
$minsell=10 ;

}

if($db->NumRows() > 1){
$minsell=50 ;

}

if($db->NumRows() > 2){
$minsell=100 ;

}




# Bonus settings
$unitprice = 0.002;
define('SITE_KEY', '6LdFxb4iAAAAAKZ1giB1id0GRlsGdCS8eA3_oy34');
define('SECRET_KEY', '6LdFxb4iAAAAAMx5bkp8vpafYNffSm8tRtHW22nk');
if($_POST){
    function getCaptcha($SecretKey){
        $Response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".SECRET_KEY."&response={$SecretKey}");
        $Return = json_decode($Response);
        return $Return;
    }
    $Return = getCaptcha($_POST['g-recaptcha-response']);
	
}

?>

<section class="main races">
    <div class="wrapper-constrained">
        <div class="content-wrapper flex">
            <div class="perfpage">
                
                <div class="pinfo">
                    <div class="picon"></div>
                    <div class="pcont">
                        1000 Gold/Silver = 0.002<div class="ic-gold3"></div>BCH.  </div>
                </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 class="container elven">
                    <div class="decor big"></div>
                    <h3><span>Buy and Sell (gold/silver)</span></h3>
                    <div class="container-inner bonus-container">
                        <div class="get-bonus-area">
					<?PHP
                if(isset($_POST["buy"])){
					
					$selling = intval($_POST["selling"]);
					$db->Query("SELECT * FROM db_selling WHERE id = '$selling' LIMIT 1");
					if($db->NumRows() > 0){
                        $sell_data = $db->FetchArray();
					$price=$sell_data["price"];
					$quant=$sell_data["quantity"];
					$metal=$sell_data["metal"];
					$seller=$sell_data["seller"];
					if ($sell_data["bot"]==1){
						
						$seller='Admin' ;
					}
					if ($metal=="silver"){
                      $money_b="money_b";
				      
					}
                    else {
                       $money_b="money_b1";
				       
					}
					$db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
                    $user_data = $db->FetchArray();
					$bch=$user_data["bch_buy"];
					if ($bch >= $price){
						$db->Query("DELETE  FROM db_selling WHERE id = '$selling'");
						
						$db->Query("UPDATE db_users_b SET $money_b = $money_b + '$quant', bch_buy = bch_buy - '$price'  WHERE id = '$usid' ");
						
						$db->Query("UPDATE db_users_b SET bch_withdrawal = bch_withdrawal + '$price' WHERE user = '$seller'");
						$message='Orded completed with success' ;
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',1,6000); }); </script>";
					}
					else {
						$message=' You havent insuffisient BCH to buy ! please deposit Bitcoin Cash.' ;
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',3,6000); }); </script>";
						
					}
					
					
					
					}	
					else {
						$message='Offer not found !' ;
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',3,6000); }); </script>";
					}
				}
                
                
                    if(isset($_POST["sell"]) && $Return->success == true && $Return->score > 0.5){
						
						if(isset($_POST["quantity"])){
							
							$quantity=floatval($_POST["quantity"]);
							
							if (($quantity >=$minsell) && ($quantity <=10000) ){
								$item = intval($_POST["item"]);
								if ($item&1){
                      $money_p="money_p";
				      $metal="silver";
					}
                    else {
                       $money_p="money_p1";
				       $metal="gold";
					}
					$db->Query("SELECT * FROM db_users_b WHERE id = '$usid' LIMIT 1");
                                $user_data = $db->FetchArray();
								
							$available=$user_data[$money_p];
							
							if ($quantity <= $available){
								$price=$quantity*$unitprice/1000 ;
								$avatar =$user_data["avatar"];
								$db->Query("INSERT INTO db_selling (seller, avatar,metal, quantity, price) VALUES ('$uname','$avatar','$metal','$quantity','$price')");
								$db->Query("UPDATE db_users_b SET $money_p = $money_p - $quantity WHERE id = '$usid'");
                                $message = 'Offer added with success!';
								echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',1,6000); }); </script>";
							}
							else {
								$message = 'No enought '.$metal.' to sell' ;
								echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',3,6000); }); </script>";
								
							}
								
							
							
							}							
							else {
								$message='Quantity should between 2 and 10000' ;
								echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$message}',3,6000); }); </script>";
							}
						
                        
                        
                        
						
                     
}					 
                    }	
					
					
                ?>
                            <div class="bonus-button-area">
							<dfn data-info="Silver for withdrawal. You can sell it to other players in crypto.">
                        <span> Balance of metal to sell in crypto </span><br>
                    </dfn>
                                                                    <dfn data-info="Silver for withdrawal. You can sell it to other players in crypto.">
                        <span class="ic-gold1"  ></span><span id="hl_money1">{!BALANCE_P!}</span>
                    </dfn>
                    <dfn data-info="Gold for withdrawal. You can sell it to other players in crypto. ">
                        <span class="ic-gold2"  ></span><span id="hl_money2"  >{!BALANCE_P1!}</span>
                    </dfn>
																	<div class="change_pass">
                                        <small>Sell Gold/Silver:</small>
                    					<script src='https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>'></script>

                                        <form action="" method="post">
										<?php $func->csrf(); ?>
                                            <div class="user-account-data-item pb10" style="margin-top: 6px;">
                             			<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br >

												<input type="radio" id="html" name="item" value="1">
  <label for="html"><span class="ic-gold1"></span></label><br>
  <input type="radio" id="css" name="item" value="2">
  <label for="css"><span class="ic-gold2"></span></label><br>
  
                                                <input type="number" style="width: 300px;" name="quantity" placeholder="quantity to sell : min <?php echo $minsell ; ?> - max 10000" value="">
                                                <p class="tac mt10">
												<button type="submit" name="sell" class="sel_av_btn btn btn-small">Sell</button>
                                                </p>
                                                
                                            </div>

                                        </form>
										<script>
    grecaptcha.ready(function() {
    grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
    .then(function(token) {
        //console.log(token);
        document.getElementById('g-recaptcha-response').value=token;
    });
    });
    </script>
										
										
                                    </div>
									
                                                                </div>
																
																<div class="ent-right1"></div>
																
				

<!--                            <div class="bag-left"></div>-->
                           
                        </div>
						<script type="text/javascript"> 
    $(document).ready(function(){
      refreshTable();
    });

    function refreshTable(){
		const nam = "<?php echo $uname; ?>";
        $('#bonus-content').load('/ajax/place.php?name='+nam, function(){
           setTimeout(refreshTable, 20000);
        });
    }
</script>
                        <h3 class="mt50">Sellers Offers:</h3>
						
                        <div class="bonus-list-area">
                            <div  id="bonus-content">
							              </div>
                        </div>
                    </div>
                    <div class="decor middle"></div>
                    <div class="decor left"></div>
                    <div class="decor right"></div>
                </div>
            </div>
        </div>
    </div>


</section>

<script src="/TableFilter.min.js" defer></script>