<?php session_start();
include_once '../includes/function.php';
include_once '../includes/autoloader.php';
$s0 = new bank();
$u1 = purify($_POST["link"]);
$d2 = purify($_POST["bank_name"]);
$s3 = purify($_POST["anumber"]);
$z4 = purify($_POST["acc_name"]);
$u5 = purify($_POST["amount"]);
$b6 = purify($_POST["destination_country"]);
$d7 = purify($_POST["swift_code"]);
$e8 = purify($_POST["narration"]);
$x9 = purify($_POST["pin"]);
$z10 = random_alphanumericstrings(36);
$o11 = Date('d-m-Y h:i:s a', time());
$l12 = 'debit';
$s13 = 'verified';
$u14 = $s0->fetch_card_id($u1);
$x15 = $u14['num'];
if($d2 == false) {
    echo "<div class='alert alert-danger' style=' '>Bank Name is required</div>";
} else {
    if($s3 == false) {
        echo "<div class='alert alert-danger' style=' '>Account Number is required</div>";
    } else {
        if(!preg_int($s3)) {
            echo "<div class='alert alert-danger' style=' '>Account Number must contain just numbers</div>";
        } else {
            if($z4 == false) {
                echo "<div class='alert alert-danger' style=' '> Account Name is required</div>";
            } else {
                if(!preg_string($z4)) {
                    echo "<div class='alert alert-danger' style=' '> Account Name must contain just uppercase and lowercase letter</div>";
                } else {
                    if($u5 == false) {
                        echo "<div class='alert alert-danger' style=' '>amount is required</div>";
                    } else {
                        if(!preg_int($u5)) {
                            echo "<div class='alert alert-danger' style=' '>amount must contain just numbers</div>";
                        } else {
                            if($b6 == false) {
                                echo "<div class='alert alert-danger' style=' '>Country of Destination is required</div>";
                            } else {
                                if($d7 == false) {
                                    echo "<div class='alert alert-danger' style=' '>Swift Code is required</div>";
                                } else {
                                    if($e8 == false) {
                                        echo "<div class='alert alert-danger' style=' '>Narration is required</div>";
                                    } else {
                                        if($x9 == false) {
                                            echo "<div class='alert alert-danger' style=' '>4-Digit PIN  is required</div>";
                                        } else {
                                            $u16 = $u14["balance"];
                                            $e17 = $u14["fname"];
                                            $t18 = $u14["middlename"];
                                            $y19 = $u14["lname"];
                                            $b20 = $e17.' '.$t18.' '.$y19;
                                            $w21 = $u14["pin"];
                                            $l22 = $u14["restri"];
                                            $k23 = $s0->sumdebit();
                                            $a24 = $u14["lim"];
                                            if($a24 >= $l22 && $a24 != null && $l22 != null) {
                                                echo "<div class='alert alert-danger' style=' '>You Have Reached Your Monthly. Do You Want to Increase Your Limit? Contact our customer care</div>";
                                            } else {
                                                if($w21 !== $x9) {
                                                    echo "<div class='alert alert-danger' style=' '>incorrect transaction pin</div>";
                                                } else {
                                                    if($u16 < $u5) {
                                                        $b25 = $u14["balance"];
                                                        $q26 = $u14['account_number'];
                                                        $m27 = $u14['id'];
                                                        $u28 = new dbh();
                                                        $z29 = $u28->connect();
                                                        $z10 = random_alphanumericstrings(36);
                                                        $o11 = Date('d-m-Y h:i:s a', time());
                                                        $l12 = 'debit';
                                                        $s13 = "insufficient funds";
                                                        $w30 = 'International Transfer';
                                                        $l31 = "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('$u5', '$s3', '$b6', '$q26', '$m27', '$o11', '$z4', '$d2', '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$d7', '$w30')";
                                                        $b32 = $z29->prepare($l31);
                                                        $k33 = $b32->execute();
                                                        if($k33) {
                                                            echo "<div class='alert alert-danger' style=' '>you cannot make this transfer you do not have sufficient fund</div>";
                                                        }
                                                    } else {
                                                        if($x15 == false || $x15 == 0) {
                                                            $u16 = $u14["balance"];
                                                            $e17 = $u14["fname"];
                                                            $t18 = $u14["middlename"];
                                                            $y19 = $u14["lname"];
                                                            $b20 = $e17.' '.$t18.' '.$y19;
                                                            $b25 = $u14["balance"];
                                                            $q26 = $u14['account_number'];
                                                            $m27 = $u14['id'];
                                                            $u28 = new dbh();
                                                            $z29 = $u28->connect();
                                                            $z10 = random_alphanumericstrings(36);
                                                            $o11 = Date('d-m-Y h:i:s a', time());
                                                            $l12 = 'debit';
                                                            $s13 = "Approved";
                                                            $w30 = 'International Transfer';
                                                            $v34 = $u16 - $u5;
                                                            $s0->setbalance($v34);
                                                            $p35 = $s0->Trans_funds($u1);
                                                            if($p35) {
                                                                $l31 = "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('$u5', '$s3', '$b6', '$q26', '$m27', '$o11', '$z4', '$d2', '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$d7' , '$w30')";
                                                                $b32 = $z29->prepare($l31);
                                                                $a24 = $b32->execute();
                                                                if($a24) {
                                                                    $p36 = $u14['currency'];
                                                                    $f37 = $u14['email'];
                                                                    $r38 = $f37;
                                                                    $s39 = ''.sitename;
                                                                    $a40 = '<!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 '.$u14["fname"].' '.$u14["middlename"].' '.$u14["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: ['.$o11.']</li>
      <li>Amount: ['.$p36.' '.$u5.']</li>
      <li>Description: ['.$e8.']</li>
      <li>Transaction ID: [#'.$z10.']</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 ['.sitemail.'] .
      </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>
  ';
                                                                    $a41 = 'MIME-Version: 1.0'."\r\n";
                                                                    $a41 .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
                                                                    $d42 = sitemail;
                                                                    $x43 = sitename;
                                                                    $a41 = "MIME-Version: 1.0"."\r\n";
                                                                    $a41 .= "Content-type:text/html;charset=UTF-8"."\r\n";
                                                                    $a41 .= 'From: '.$x43.'<'.$d42.'>'."\r\n";
                                                                    $a41 .= 'Cc:  '.sitemail.''."\r\n";
                                                                    $a41 .= 'Bcc:  '.sitemail.''."\r\n";
                                                                    if(mail($r38, $s39, $a40, $a41)) {?>
      <script>
         var secondModal = document.getElementById("secondModal");
      secondModal.style.display = "none";
  
      Swal.fire({
      position: 'center',
      icon: 'success',
      html: 'Transfer Successful',
      showConfirmButton: false,
      timer: 7000
      })
      // Set the timer duration in milliseconds (e.g., 5000ms = 5 seconds)
const timerDuration = 7000;

// Function to reload the page
var delay = 3000;
var url = "../dashboard/reciep";
setTimeout(function(){
window.location.href = url; 
}, delay);
// Set the timer to reload the page after the specified duration
// setTimeout(reloadPage, timerDuration);

     </script>
      <?php } else {
          echo 'error in mailing';
      }
                                                                } else {
                                                                    echo 'error';
                                                                }
                                                            } else {
                                                                echo 'could not insert';
                                                            }
                                                        } else {
                                                            if($x15 == '1') {
                                                                $u16 = $u14["balance"];
                                                                $e17 = $u14["fname"];
                                                                $t18 = $u14["middlename"];
                                                                $y19 = $u14["lname"];
                                                                $b20 = $e17.' '.$t18.' '.$y19;
                                                                $b25 = $u14["balance"];
                                                                $q26 = $u14['account_number'];
                                                                $m27 = $u14['id'];
                                                                $u28 = new dbh();
                                                                $z29 = $u28->connect();
                                                                $z10 = random_alphanumericstrings(36);
                                                                $o11 = Date('d-m-Y h:i:s a', time());
                                                                $l12 = 'debit';
                                                                $s13 = "Approved";
                                                                $w30 = 'Local Transfer';
                                                                $l31 = "INSERT INTO `on`(amount, recipient_acc,  sender_acc, sender_id, date_of_trans, recipient_name,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$u5', '$s3', '$q26', '$m27', '$o11', '$z4',  '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$w30')";
                                                                $b32 = $z29->prepare($l31);
                                                                $a24 = $b32->execute();
                                                                if($a24) {?>
     <script>
          var ranValue = "<?php echo $z10;?>";
  // Show the confirmation modal
  var confirmationModal = document.getElementById("confirmationModal");
   confirmationModal.style.display = "block";
 
   // Get the second modal and form elements
   var secondModal = document.getElementById("secondModal");
   var personalKeyInput = document.getElementById("personalKeyInput");
 
 //   Add event listener to the confirm button
   var confirmBtn = document.getElementById("confirmBtn");
   confirmBtn.addEventListener("click", function() {
     // Show the second modal when the user clicks "Yes"
     confirmationModal.style.display = "none"; // Hide the first modal
     secondModal.style.display = "block"; // Show the second modal
   });
 
   // Add event listener to the cancel button of the second modal
   var cancelBtn = document.getElementById("cancelBtn");
   cancelBtn.addEventListener("click", function() {
     // Close the second modal and show the first modal again
     secondModal.style.display = "none"; // Hide the second modal
     confirmationModal.style.display = "block"; // Show the first modal
 
     // Perform AJAX request to another file
     $.ajax({
       url: "../includes/delete_transaction", // Replace with the actual path to your file
       type: "POST",
       data: {
         cancel: true,
         ran: ranValue // Pass the value of $ran in the AJAX request
       },
       success: function(response) {
         // Handle the response from the file
         if (response === "cancel_modal") {
         confirmationModal.style.display = "none"; // Hide the first modal
       }
       },
       error: function(xhr, status, error) {
         // Handle any errors that occur during the AJAX request
         console.error(error);
       }
     });
   });
 
   // Handle form submission
   var form = document.getElementById("personalKeyForm");
   form.addEventListener("submit", function(event) {
     event.preventDefault(); // Prevent form submission
 
     // Get the value entered by the user
     var personalKey = personalKeyInput.value;
 
     $.ajax({
     url: "../includes/confirm_trans", // Replace with the actual path to your other PHP file
     type: "POST",
     data: {
       personalKey: personalKey, // Pass the input value
       userId: <?php echo $u1;?> // Pass the user ID from PHP
     },
     success: function(response) {
       // Handle the response from the file
       console.log(response);
       $('#error').html(response);
 
     },
     error: function(xhr, status, error) {
       // Handle any errors that occur during the AJAX request
       console.error(error);
     }
   });
   });
 
     </script>
   <?php } else {
       echo 'could not insert';
   }
                                                            } else {
                                                                if($x15 == '2') {
                                                                    $u16 = $u14["balance"];
                                                                    $e17 = $u14["fname"];
                                                                    $t18 = $u14["middlename"];
                                                                    $y19 = $u14["lname"];
                                                                    $b20 = $e17.' '.$t18.' '.$y19;
                                                                    $b25 = $u14["balance"];
                                                                    $q26 = $u14['account_number'];
                                                                    $m27 = $u14['id'];
                                                                    $u28 = new dbh();
                                                                    $z29 = $u28->connect();
                                                                    $z10 = random_alphanumericstrings(36);
                                                                    $o11 = Date('d-m-Y h:i:s a', time());
                                                                    $l12 = 'debit';
                                                                    $s13 = "Approved";
                                                                    $w30 = 'International Transfer';
                                                                    $l31 = "INSERT INTO `on`(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('$u5', '$s3', '$b6', '$q26', '$m27', '$o11', '$z4', '$d2', '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$d7' , '$w30')";
                                                                    $b32 = $z29->prepare($l31);
                                                                    $a24 = $b32->execute();
                                                                    if($a24) {?>
       <script>
            var ranValue = "<?php echo $z10;?>";
    // Show the confirmation modal
    var confirmationModal = document.getElementById("confirmationModal");
     confirmationModal.style.display = "block";
   
     // Get the second modal and form elements
     var secondModal = document.getElementById("secondModal");
     var personalKeyInput = document.getElementById("personalKeyInput");
   
   //   Add event listener to the confirm button
     var confirmBtn = document.getElementById("confirmBtn");
     confirmBtn.addEventListener("click", function() {
       // Show the second modal when the user clicks "Yes"
       confirmationModal.style.display = "none"; // Hide the first modal
       secondModal.style.display = "block"; // Show the second modal
     });
   
     // Add event listener to the cancel button of the second modal
     var cancelBtn = document.getElementById("cancelBtn");
     cancelBtn.addEventListener("click", function() {
       // Close the second modal and show the first modal again
       secondModal.style.display = "none"; // Hide the second modal
       confirmationModal.style.display = "block"; // Show the first modal
   
       // Perform AJAX request to another file
       $.ajax({
         url: "../includes/delete_transaction", // Replace with the actual path to your file
         type: "POST",
         data: {
           cancel: true,
           ran: ranValue // Pass the value of $ran in the AJAX request
         },
         success: function(response) {
           // Handle the response from the file
           if (response === "cancel_modal") {
           confirmationModal.style.display = "none"; // Hide the first modal
         }
         },
         error: function(xhr, status, error) {
           // Handle any errors that occur during the AJAX request
           console.error(error);
         }
       });
     });
   
     // Handle form submission
     var form = document.getElementById("personalKeyForm");
     form.addEventListener("submit", function(event) {
       event.preventDefault(); // Prevent form submission
   
       // Get the value entered by the user
       var personalKey = personalKeyInput.value;
   
       $.ajax({
       url: "../includes/confirm_trans", // Replace with the actual path to your other PHP file
       type: "POST",
       data: {
         personalKey: personalKey, // Pass the input value
         userId: <?php echo $u1;?> // Pass the user ID from PHP
       },
       success: function(response) {
         // Handle the response from the file
         console.log(response);
         $('#error').html(response);
   
       },
       error: function(xhr, status, error) {
         // Handle any errors that occur during the AJAX request
         console.error(error);
       }
     });
     });
   
       </script>
     <?php } else {
         echo 'could not insert';
     }
                                                                } else {
                                                                    if($x15 == '3') {
                                                                        $u16 = $u14["balance"];
                                                                        $e17 = $u14["fname"];
                                                                        $t18 = $u14["middlename"];
                                                                        $y19 = $u14["lname"];
                                                                        $b20 = $e17.' '.$t18.' '.$y19;
                                                                        $b25 = $u14["balance"];
                                                                        $q26 = $u14['account_number'];
                                                                        $m27 = $u14['id'];
                                                                        $u28 = new dbh();
                                                                        $z29 = $u28->connect();
                                                                        $z10 = random_alphanumericstrings(36);
                                                                        $o11 = Date('d-m-Y h:i:s a', time());
                                                                        $l12 = 'debit';
                                                                        $s13 = "Approved";
                                                                        $w30 = 'International Transfer';
                                                                        $l31 = "INSERT INTO `on`(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('$u5', '$s3', '$b6', '$q26', '$m27', '$o11', '$z4', '$d2', '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$d7' , '$w30')";
                                                                        $b32 = $z29->prepare($l31);
                                                                        $a24 = $b32->execute();
                                                                        if($a24) {?>
         <script>
              var ranValue = "<?php echo $z10;?>";
      // Show the confirmation modal
      var confirmationModal = document.getElementById("confirmationModal");
       confirmationModal.style.display = "block";
     
       // Get the second modal and form elements
       var secondModal = document.getElementById("secondModal");
       var personalKeyInput = document.getElementById("personalKeyInput");
     
     //   Add event listener to the confirm button
       var confirmBtn = document.getElementById("confirmBtn");
       confirmBtn.addEventListener("click", function() {
         // Show the second modal when the user clicks "Yes"
         confirmationModal.style.display = "none"; // Hide the first modal
         secondModal.style.display = "block"; // Show the second modal
       });
     
       // Add event listener to the cancel button of the second modal
       var cancelBtn = document.getElementById("cancelBtn");
       cancelBtn.addEventListener("click", function() {
         // Close the second modal and show the first modal again
         secondModal.style.display = "none"; // Hide the second modal
         confirmationModal.style.display = "block"; // Show the first modal
     
         // Perform AJAX request to another file
         $.ajax({
           url: "../includes/delete_transaction", // Replace with the actual path to your file
           type: "POST",
           data: {
             cancel: true,
             ran: ranValue // Pass the value of $ran in the AJAX request
           },
           success: function(response) {
             // Handle the response from the file
             if (response === "cancel_modal") {
             confirmationModal.style.display = "none"; // Hide the first modal
           }
           },
           error: function(xhr, status, error) {
             // Handle any errors that occur during the AJAX request
             console.error(error);
           }
         });
       });
     
       // Handle form submission
       var form = document.getElementById("personalKeyForm");
       form.addEventListener("submit", function(event) {
         event.preventDefault(); // Prevent form submission
     
         // Get the value entered by the user
         var personalKey = personalKeyInput.value;
     
         $.ajax({
         url: "../includes/confirm_trans", // Replace with the actual path to your other PHP file
         type: "POST",
         data: {
           personalKey: personalKey, // Pass the input value
           userId: <?php echo $u1;?> // Pass the user ID from PHP
         },
         success: function(response) {
           // Handle the response from the file
           console.log(response);
           $('#error').html(response);
     
         },
         error: function(xhr, status, error) {
           // Handle any errors that occur during the AJAX request
           console.error(error);
         }
       });
       });
     
         </script>
       <?php } else {
           echo 'could not insert';
       }
                                                                    } else {
                                                                        if($x15 == '4') {
                                                                            $u16 = $u14["balance"];
                                                                            $e17 = $u14["fname"];
                                                                            $t18 = $u14["middlename"];
                                                                            $y19 = $u14["lname"];
                                                                            $b20 = $e17.' '.$t18.' '.$y19;
                                                                            $b25 = $u14["balance"];
                                                                            $q26 = $u14['account_number'];
                                                                            $m27 = $u14['id'];
                                                                            $u28 = new dbh();
                                                                            $z29 = $u28->connect();
                                                                            $z10 = random_alphanumericstrings(36);
                                                                            $o11 = Date('d-m-Y h:i:s a', time());
                                                                            $l12 = 'debit';
                                                                            $s13 = "Approved";
                                                                            $w30 = 'International Transfer';
                                                                            $l31 = "INSERT INTO `on`(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('$u5', '$s3', '$b6', '$q26', '$m27', '$o11', '$z4', '$d2', '$b25', '$l12', '$e8', '$b20', '$s13', '$z10', '$d7' , '$w30')";
                                                                            $b32 = $z29->prepare($l31);
                                                                            $a24 = $b32->execute();
                                                                            if($a24) {?>
           <script>
                var ranValue = "<?php echo $z10;?>";
        // Show the confirmation modal
        var confirmationModal = document.getElementById("confirmationModal");
         confirmationModal.style.display = "block";
       
         // Get the second modal and form elements
         var secondModal = document.getElementById("secondModal");
         var personalKeyInput = document.getElementById("personalKeyInput");
       
       //   Add event listener to the confirm button
         var confirmBtn = document.getElementById("confirmBtn");
         confirmBtn.addEventListener("click", function() {
           // Show the second modal when the user clicks "Yes"
           confirmationModal.style.display = "none"; // Hide the first modal
           secondModal.style.display = "block"; // Show the second modal
         });
       
         // Add event listener to the cancel button of the second modal
         var cancelBtn = document.getElementById("cancelBtn");
         cancelBtn.addEventListener("click", function() {
           // Close the second modal and show the first modal again
           secondModal.style.display = "none"; // Hide the second modal
           confirmationModal.style.display = "block"; // Show the first modal
       
           // Perform AJAX request to another file
           $.ajax({
             url: "../includes/delete_transaction", // Replace with the actual path to your file
             type: "POST",
             data: {
               cancel: true,
               ran: ranValue // Pass the value of $ran in the AJAX request
             },
             success: function(response) {
               // Handle the response from the file
               if (response === "cancel_modal") {
               confirmationModal.style.display = "none"; // Hide the first modal
             }
             },
             error: function(xhr, status, error) {
               // Handle any errors that occur during the AJAX request
               console.error(error);
             }
           });
         });
       
         // Handle form submission
         var form = document.getElementById("personalKeyForm");
         form.addEventListener("submit", function(event) {
           event.preventDefault(); // Prevent form submission
       
           // Get the value entered by the user
           var personalKey = personalKeyInput.value;
       
           $.ajax({
           url: "../includes/confirm_trans", // Replace with the actual path to your other PHP file
           type: "POST",
           data: {
             personalKey: personalKey, // Pass the input value
             userId: <?php echo $u1;?> // Pass the user ID from PHP
           },
           success: function(response) {
             // Handle the response from the file
             console.log(response);
             $('#error').html(response);
       
           },
           error: function(xhr, status, error) {
             // Handle any errors that occur during the AJAX request
             console.error(error);
           }
         });
         });
       
           </script>
         <?php } else {
             echo 'could not insert';
         }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
