<?php
session_start();

include_once '../includes/function.php';
include_once '../includes/autoloader.php'; 
$objvalogin = new bank;


$old = purify($_POST["personalKey"]);
$userId = purify($_POST["userId"]);
$fetch = $objvalogin->fetch_card_id($userId);
$call = $objvalogin->fetch_trans_vals($userId);
$id = $call['id'];
$amount = $call['amount'];
$recipient_acc= $call['recipient_acc'];
$sender_acc = $call['sender_acc'];
$sender_id =  $call['sender_id'];
$date_of_trans =  $call['date_of_trans'];
$recipient_name =  $call['recipient_name'];
$bank =  $call['bank'];
$bank_accbal =  $call['bank_accbal'] - $amount;
$type_of =  $call['type_of'];
$trans_des =  $call['trans_des'];
$from_ =  $call['from_'];
$status =  $call['status'];
$trans_id =  $call['trans_id'];  
$destination_country = $call['destination_country'];
$iban = $call['iban'];
$debit_type = $call['debit_type'];
if ($old == FALSE) {
echo "<div class='alert alert-danger' style='background:white; color: red; '>Anti Terrorism Clearance Code Is Required</div>";
}else {
$pinn = $fetch["AntiTerrorismClearanceCode"];
if ($old == $pinn) {
if ($fetch["num"] == '4') { 

    if ($fetch["allowed"] == 'allowed') { 
        $objvalogin->setbalance($bank_accbal);
        $up = $objvalogin->Trans_funds($userId);
        if ($up) {
            $db = new dbh;
            $dbconn = $db->connect(); 
            $sql = "INSERT INTO `transactions`(amount, recipient_acc, destination_country, sender_acc, sender_id, date_of_trans, recipient_name, bank, bank_accbal, type_of, trans_des, from_, status, trans_id, iban, debit_type)VALUES('$amount', '$recipient_acc', '$destination_country', '$sender_acc', '$sender_id', '$date_of_trans', '$recipient_name', '$bank', '$bank_accbal', '$type_of', '$trans_des', '$from_', '$status', '$trans_id', '$iban', '$debit_type')";
            $conn = $dbconn->prepare($sql);
            $go = $conn->execute();
            if ($go) {
            $db = new dbh;
            $dbconn = $db->connect(); 
            $sql = "DELETE FROM `on` WHERE id = '$id'";
            $conn = $dbconn->prepare($sql);
            if ($conn->execute()) {
            $db = new dbh;
            $dbconn = $db->connect(); 
            $limit = $fetch['lim'];
            $ZIP = (int)$limit + (int)$amount;
            $sql = "UPDATE `users` SET lim = '$ZIP' WHERE id = '$userId'";
            $conn = $dbconn->prepare($sql);
            if ($conn->execute()) {
                $mai = $fetch['email'];
                $currency = $fetch['currency'];
                $to = $mai;
                $subject = 'Welcome to ' . sitename;
                $message = '<!DOCTYPE html>
                <html>
                
                <head>
                  <meta charset="utf-8">
                  <title>Debit Notification</title>
                </head>
                
                <body style="font-family: Arial, sans-serif; background-color: #f2f2f2; text-align: center;">
                  <div style="max-width: 600px; margin: 0 auto; padding: 20px; background-color: #ffffff; border-radius: 4px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);">
                
                    <p style="margin-bottom: 10px; text-align: center;">
                      <a href="#" style="font-family: Arial, \'Helvetica Neue\', Helvetica, sans-serif; font-size: 16px; font-weight: bold; color: #2f3133; text-decoration: none;" rel="noreferrer">
                        <img src="'.sitelink.'assets/img/logo.png" width="120px" height="100px" class="CToWUd">
                      </a>
                    </p>
                    <h1 style="color: #D10404; font-size: 24px; margin-bottom: 20px;">Debit Notification</h1>
                
                    <p style="margin-bottom: 10px;">Hello '.$fetch["fname"].' '.$fetch["middlename"].' '.$fetch["lname"].',</p>
                
                    <p style="margin-bottom: 10px;">We are writing to inform you about a recent debit transaction on your account with ['.sitename.'].</p>
                
                    <p style="margin-bottom: 10px;">Transaction Details:</p>
                    
                    <ul style="list-style: none; padding: 0; margin: 0;">
                      <li>Date: ['.$date_of_trans.']</li>
                      <li>Amount: ['.$currency.''.$amount.']</li>
                      <li>Description: ['.$trans_des.']</li>
                      <li>Transaction ID: ['.$trans_id.']</li>
                    </ul>
                
                    <p style="margin-bottom: 10px;">If you did not authorize this transaction or have any concerns, please contact our customer support immediately for assistance.</p>
                
                    <p style="margin-top: 0; color: #74787e; font-size: 16px; line-height: 1.5em;">
                      For any inquiries or assistance, you can reach us at [Customer Support Email] or [Customer Support Phone].
                    </p>
                
                    <p style="margin-bottom: 10px;">Best regards,</p>
                
                    <p style="margin-bottom: 10px;">The ['.sitename.'] Team</p>
                
                    <div style="margin-top: 40px; text-align: center;">
                      <p style="font-size: 12px;">&copy; ['.date('Y').'] [<span style="color: #D10404;">['.sitename.']</span>]. All rights reserved.</p>
                    </div>
                  </div>
                </body>
                
                </html>
                ';
                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $from =  sitemail;
                $fromName = sitename; 
                // Set content-type header for sending HTML email 
                $headers = "MIME-Version: 1.0" . "\r\n"; 
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
                
                // Additional headers 
                $headers .= 'From: '.$fromName.'<'.$from.'>' . "\r\n"; 
                $headers .= 'Cc:  '.sitemail.'' . "\r\n"; 
                $headers .= 'Bcc:  '.sitemail.'' . "\r\n"; 
                // Mail it
                
                if (mail($to, $subject, $message, $headers)) {
                ?>
                <script>
       var fifthModal = document.getElementById("fifthModal"); 
 fifthModal.style.display = "none";
 Swal.fire({
      position: 'center',
      icon: 'success',
       html: 'Transaction successful',
      showConfirmButton: false,
      timer: 7000
    })
    // Set the timer duration in milliseconds (e.g., 5000ms = 5 seconds)
const timerDuration = 5000;

// Function to reload the page
function reloadPage() {
  location.reload();
}

// Set the timer to reload the page after the specified duration
// setTimeout(reloadPage, timerDuration);
var delay = 3000;
var url = "../dashboard/reciep";
setTimeout(function(){
window.location.href = url; 
}, delay);
                </script>
                <?php
                }else{
                echo 'error in mailing';
                }
            }else{
            echo 'could not update';
            } 
            }else{
            echo 'could not delete';
            }
            
            }else{
            echo "<div class='alert alert-danger' style='background:none; color: red; '>Could not insert</div>";
            }
            } else {
                echo "<div class='alert alert-danger' style='background:white; color: red; '>could not update balance</div>";

            }
    }else{
        echo "<div class='alert alert-danger' style='background:white; color: red; '>!!!Error 420569-code6  transaction failed</div>";
    }

}
}else{
    echo "<div class='alert alert-danger' style='background:white; color: red; '>Anti Terrorism Clearance Code is incorrect</div>";
    }	 
}

