<?php
include '../config/index.php';
include '../module/index.php';

$data = new Database;

$con = $data->connect();

$user = new User($con);

$post = filter_var_array($_POST,FILTER_SANITIZE_FULL_SPECIAL_CHARS);

$user_id = $_SESSION['id'];

$check_transaction = $user->check_temp('transaction');

$trans_reference = $check_transaction['0']->reference;

$datas          = $user->check_tempsss($trans_reference);

$bal            = $post['balance'];
$amount         = $_SESSION['inter_amount'];  

$county        = $post['country'];
$fullname      = $post['fullname'];
$city          = $post['city'];
$sort          = $post['sort'] ?? 0;
$address       = $post['address'];
$swiftcode     = $post['swiftcode'];
$accountnumber = $post['accountnumber'];
$bank          = $post['bank'];

$transfer = [
   'country'=>$county,
   'city'=>$city,
   'sort'=>$sort,
   'name'=>$fullname,
   'address'=>$address,
   // 'zipcode'=>$zipcode,
   // 'email'=>$email,
   // 'phone'=>$phone,
   // 'iban'=>$iban,
   'swiftcode'=>$swiftcode,
   'bank'=>$bank,
   'accountnumber'=>$accountnumber,
];

$_SESSION['int']   = $transfer;

if($user->getUserNameById($_SESSION['id'])->transfer == false){

   $_SESSION['t_error_c'] =  "Your account has been locked pls contact bank suppport for more information";
   echo $data->redirect('../account/../index?wire2=wire');
   exit();
}

                        

if($amount > $bal){
   $_SESSION['t_error_c']  = 'insufficient balance';
   echo $data->redirect('../account/../index?wire2=wire');
   exit();
}
   
   $bal =   $_SESSION['inter_amount'];   

   $user_name = $_SESSION['first_name'];
   $type = 'Debit';
   $d = 'DT';
   $user_id = $_SESSION['id'];
   $amount = $_SESSION['inter_amount'];

   $reference = uniqid();
   $date      = new DateTime();
   $dates     = $date->format('d M y, h:i a'); // 21 Dec 22, 08:39 pm
   $status    = 'pending';

   echo $user->fundTransfer($amount);

   echo $user->createTransaction($amount,$user_id,$reference,'nill',$dates,$status,$type,$user_name.'/'.$d.'_'.$reference.'>'.$amount);

   echo $user->temp($user_id,$amount,$reference,$status,$dates);

   echo $user->InterTransfer($reference,$_SESSION['int']['name'],$_SESSION['int']['sort'],$_SESSION['int']['accountnumber'],$_SESSION['inter_amount'],$_SESSION['int']['bank'],$_SESSION['int']['country'],$_SESSION['int']['city'],$_SESSION['description']);
   
   $email = 'ibanking@vaultmogrebk.com';

   echo $user->transferReceipt($email,$_SESSION['int']['country'],$_SESSION['int']['name'],$_SESSION['int']['bank'],$_SESSION['int']['accountnumber'],$amount);

   echo $data->redirect('../account/../index?cot=cot');

?>