<?php include "includes/conexao.php";?>

<?php 
if(isset($_POST['submit'])){
	
	$id = $_POST['id'];
	
	
	
	$selectThis = "SELECT * FROM creditos WHERE id='$id'";
	$queryThis = mysqli_query($con,$selectThis);
	$row = mysqli_fetch_array($queryThis);
	
	$cliente = $row['cliente'];
	$valor = $row['valor'];
	$conta = $row['conta'];
	


		$buscar_conta2 = "SELECT * FROM contas WHERE conta='$conta'";
		$dados_ligar2 = mysqli_query($con,$buscar_conta2);
		$nice_query2 = mysqli_fetch_assoc($dados_ligar2);
		
		$saldo_actual_conta = $nice_query2['saldo'];
		
		$novo_taco_actual = $saldo_actual_conta + $valor;
		
		
	$actualiza_conta2 = "UPDATE contas SET saldo='$novo_taco_actual' WHERE conta='$conta'";
	$yes_we_do2 = mysqli_query($con,$actualiza_conta2);

	if($yes_we_do2){
	    
	   $del = "DELETE FROM creditos WHERE id='$id'";
	   $query = mysqli_query($con,$del);
	
		 echo "<script>alert('Credito anulado com sucesso, retornado o valor de $valor MT para a conta $conta')</script>";
		 echo "<script>window.open('mensais.php','_self')</script>";
		
	}else{
		echo "<script>alert('FALHA')</script>";
		echo "<script>window.open('mensais.php','_self')</script>";
	}
	
}

?>