<?php 	
require_once '../../Server/connection.php';
$valid['success'] = array('success' => false, 'messages' => array(), 'currency_id' => '');
if($_POST){	

  
  $currency_id = $_POST['currency_id'];
  $Amount = $_POST['Amount'];
  
    $date = date("Y-m-d");
	$month1 = date('m', strtotime($date));
	$year = date('Y', strtotime($date));
	
	if($month1==1){
	 $month = 'JAN';
	  }else if($month1==2){
	 $month = 'FEB';	  
	  }else if($month1==3){
	$month = 'MARCH';	  
	  }else if($month1==4){
	$month = 'APR';		  
	  }else if($month1==5){
	$month = 'MAY';	  
	  }else if($month1==6){
	$month = 'JUNE';	  
	  }else if($month1==7){
	$month = 'JULY';	  
	  }else if($month1==8){
	$month = 'AUG';	  
	  }else if($month1==9){
	$month = 'SEPT';	  
	  }else if($month1==10){
	$month = 'OCT';	  
	  }else if($month1==11){
	$month = 'NOV';	  
	  }else{
	$month = 'DEC';	  
	  }
  
  $sql8122 = "SELECT * FROM currency where currency_id='$currency_id'";
				$result8122 = $conn->query($sql8122);
				while($data8122 = mysqli_fetch_array($result8122))
							{
							$bankAmount = $data8122['amount'];
							}
  
                        if($Amount>$bankAmount){
							
					$valid['success'] = true;
			        $valid['messages'] = "Insufficient Amount";
							
						}else{
							
					$amountRemain = $bankAmount - $Amount;
					
					$sql = "UPDATE currency SET amount = '$amountRemain' WHERE currency_id  = '$currency_id'";
					
					$result = $conn->query($sql);
					
					$sql1 = "INSERT INTO bank_withdrawals (amount,currency_id,date,month,year) 
				    VALUES ('$Amount','$currency_id', '$date', '$month','$year')";
				
				
				    $result1 = $conn->query($sql1);
					
					if($result&&$result1){
					
					
				$valid['success'] = true;
			    $valid['messages'] = "Amount is Successfully Withdrawn";
					
				
				         }
							
						}
  
    
	$conn->close();

	echo json_encode($valid);
 
} // /if $_POST