<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;


if( $_REQUEST['redemption'] ){

	$get_country     	   = getCurrentCountry() ;	
	$country_code 		   = $get_country['geoplugin_countryCode'] ;
	
	$download_link 		   = '' ;
	$query 				   = $mysqli->query( "SELECT * FROM redemption 	
									 		  WHERE countrycode = '".$country_code."' AND status = '0' ORDER BY redemption_id ASC LIMIT 1" ) ;
	if( $query->num_rows   > 0 ){
		$result            = $query->fetch_assoc() ;
		$id                = $result['redemption_id'] ;
		$download_link     = $result['download_link'] ;

		//update download_link status
		$update            = $mysqli->query( "UPDATE redemption SET status = '1' WHERE redemption_id = '".$id."'" ) ;
		if( !$update ){
			$download_link = '' ;
		}
	}
	echo $download_link ;
	exit;
}


// body onload script
$show_map_library = true ;
$show_map_script = true ;
$body_onload = true ;


// start header here
include 'requires/page_header.php' ;

?>
<div class="container">
	<div class="row" style="margin:50px 0;">
        <div class="col-lg-4 col-lg-offset-4">
        	<h3 class="text-center login_logo">
        		<img src="<?= PATH ?>images/logo_full.png" style="width:150px;" />
        	</h3>
			<!--
				<h3 class="text-center" style="font-size:20px; margin-bottom:40px; color:#000;"><b><?= COMPANY ?></b></h3>
			-->
            <hr class="clean">
            <p id="email_error"><?= $lang['please_get_the_verification_code_from_the_owner']?></a></p>
            <p id="block_error"><?= $lang['sorry_your_account_was_in_our_block_list']?></p>
            <form method="post" role="form" id="redemption_form" novalidate>
                <!-- last login coordinates use -->
                <input type="hidden" name="latitude" id="current_lat_input" />
                <input type="hidden" name="longtitude" id="current_lot_input" />
                <!-- end last login coordinates use -->
            	<input type="hidden" name="user_hide" value="1" />
                <input type="hidden" name="user_hide_status" value="<?= ($boolean_verifcation ? 'verification' : 'login') ?>" />
                <input type="hidden" name="user_token" value="<?= $_SESSION['system_token'] ?>" />
                <button type="button" class="btn btn-purple btn-block" style="padding:10px;" id="redemption_btn" name="redemption_btn"><?= $lang['Redemption']?></button>
            </form>

            <div class="language index_language">
            	<?php
            	if ( arrayCheck($app_download_link) ){
            		$new_download = [] ;
	            	foreach ( $app_download_link as $k => $v ){
	            		$new_download[] = '<a href="https://ips.com.my/apk/apk/'.$k.'.apk" download>'.$v.'</a>' ;
	            	}
	            	echo implode( ' / ', $new_download ) ;
            	}
            	?>
            </div>

            <hr />
            
            <div class="language index_language">
            	<a href="language.php?lang=en&link=//<?= $_SERVER['HTTP_HOST'].urlencode($_SERVER['REQUEST_URI']) ?>" class="<?= $get_lang == 'en' ? 'active' :'' ?>">ENG</a>
            	<a href="language.php?lang=cn&link=//<?= $_SERVER['HTTP_HOST'].urlencode($_SERVER['REQUEST_URI']) ?>" class="<?= $get_lang == 'cn' ? 'active' :'' ?>">中文</a>
            </div>

            <hr />

			<p class="text-center text-gray"><?= $lang['developed_by_eng']?><a href="http://ips.com.my" target="_blank">IPS Software Sdn. Bhd.</a><?= $lang['developed_by_cn']?></p>
        </div>
    </div>
</div>

<script>
$("#redemption_btn").click(function(){
	if(confirm("<?= $lang['Are you sure to get the redemption code?'] ?>")){
		var data='100';
			$.ajax({
			type: 'POST',
			url: 'redemption.php',
			data: {redemption:1},
			success: function (result) {
				var d = result.toString();
				if(d==''){
					alert("<?= $lang['Sorry, no code cannot be redempted.'] ?>");
				}else{
					window.location.href = d;
				}
			}
		});
	}
});
</script>

</body>
</html>