<?php
	session_start();
	include"../../connect/db.php";

	csrf_verify();
	if( isset($_POST['pin']) ){

		$pin = mysqli_real_escape_string($con,$_POST['pin']);
		$chap = $_SESSION['chap'];

		$sql = mysqli_query($con,"SELECT account_num,code_level,fullname,currency,email FROM users WHERE email = '$chap' ");

		$row = mysqli_fetch_assoc($sql);

		$account_num = $row['account_num'];
		$code_level = $row['code_level'];
		$fullname = $row['fullname'];
		$currency = $row['currency'];
		$email = $row['email'];
		$d_o_t = date("Y-m-d");	

		$query9 = mysqli_query($con,"SELECT * FROM transactions WHERE user = '$email' AND status = 'active' ");
	    $far = mysqli_fetch_assoc($query9);
		$name = $far['type'];
		$trans = $far['trans_id'];
		$query3 = mysqli_query($con,"SELECT * FROM code WHERE user = '$account_num' ");
	    $grow = mysqli_fetch_assoc($query3);
		$debits = $grow['amount'];


		$debit = number_format($debits,2);









		$query = mysqli_query($con,"SELECT * FROM code WHERE user = '$account_num' ");
		$crow = mysqli_fetch_assoc($query);
		//$statement = "Debit! Bank Transfer Of $currency , $debit From self to $name  Reference $trans";
		//$alert = mysqli_query( $con,"INSERT INTO transactions (trans_id, user, description, d_o_t, type, status) VALUES 	('$trans', '$account_num', '$statement', '$d_o_t', 'Debit', 'SUCCESS' ) " );
		
		
		

		if( $crow[$code_level] == $pin ){

			if( $row['code_level'] == "tax_clearance_code" ){
				$new_level = "activation_code";
			}
			else if( $row['code_level'] == "activation_code" ){
				$new_level = "drugs_clearance_code";
			
			}
			else if( $row['code_level'] == "drugs_clearance_code" ){
				$new_level = "nothing";

			}			
			else if( $row['code_level'] == "reflection_permit_code" ){
				$new_level = "united_nation_creditation_certificate_code";
				
			}
			else if( $row['code_level'] == "united_nation_creditation_certificate_code" ){
				$new_level = "nothing";

			}

		    $vagina = mysqli_query($con,"UPDATE users SET code_level = '$new_level' WHERE email = '$chap' ");
			$query2 = mysqli_query($con,"SELECT * FROM users WHERE account_num = '$account_num'");
			$brow = mysqli_fetch_assoc($query2);
			$levels = $brow['code_level'];
			if( $levels == "tax_clearance_code" ){		
				echo "success";
			}
			else if( $levels == "activation_code" ){
			//	include'codemailer1.php';
				echo "activation";
			}
			else if( $levels == "drugs_clearance_code" ){
		 //   include'codemailer2.php';
				echo "drug";
			}
			else if( $levels == "reflection_permit_code" ){
		 //   include'codemailer3.php';
				echo "reflect";
			}
			else if( $levels == "united_nation_creditation_certificate_code" ){
			//	include'codemailer4.php';
				echo "united";
			}
			else if( $levels == "nothing" ){
			$query4 = mysqli_query($con,"SELECT * FROM users WHERE account_num = '$account_num'");
			$trow = mysqli_fetch_assoc($query4);


		


			$balance = $trow['balance'];
		    $new_bal = $balance - $debits;
			$newvibes = mysqli_query($con,"UPDATE users SET balance = '$new_bal' WHERE email = '$chap' ");
			$newvibes2 = mysqli_query($con,"UPDATE transactions SET user = 'done' WHERE status = 'active' ");
			$newvibes2 = mysqli_query($con,"UPDATE transfer SET status = 'done' WHERE status = 'tax_clearance_code' ");
				
			
			$swaga = mysqli_query($con,"SELECT * FROM transfer WHERE status = 'done' ");
			$new = mysqli_fetch_assoc($swaga);
			$rname  = $new['rname'];

			$statement = "Bank Transfer Of $currency $debit  to $rname  Reference $trans";
            $alert = mysqli_query( $con,"INSERT INTO transactions (trans_id, user, description, d_o_t, type, status) VALUES 	('$trans', '$account_num', '$statement', '$d_o_t', 'Debit', 'SUCCESS' ) " );
			//	include'debitalert.php';
			$activate = mysqli_query($con,"UPDATE code SET reversal = 'live'  WHERE user = '$account_num'");
			if($activate){
				$_SESSION['trans_id'] = $trans;
			echo "nothing";
			}
			else{
			echo"error1";
			}

			}
			else{
				echo "error";
			}
			
		}
		else{
			echo $pin;
		}

	}
	else{
		echo "error";
	}
?>