<?php
session_start();
include_once '../includes/function.php';
include_once '../includes/autoloader.php'; 
$objvalogin = new bank;

$anumber = purify($_POST["bitcoin_address"]);
$amount = purify($_POST["bitcoin_amount"]);
$narration = purify($_POST["note"]);
$pin = purify($_POST["pin"]);
$ran = random_alphanumericstrings(36);
$da = Date('d-m-Y h:i:s a', time());
$type = 'debit';
$status = 'verified';
$link = purify($_POST["link"]);

$fetch = $objvalogin->fetch_card_id($link);

if ($anumber == FALSE) {
echo "<div class='alert alert-danger' style=' '>Usdt Adress is required</div>";
} else {
 if ($amount == FALSE) {
echo "<div class='alert alert-danger' style=' '>amount is required</div>";
} else {
if (!preg_int($amount)) {
echo "<div class='alert alert-danger' style=' '>amount must contain just numbers</div>";
} else {
if ($pin == FALSE) {
echo "<div class='alert alert-danger' style=' '>4-Digit PIN  is required</div>";
} else {

$pswd = $fetch["balance"];
$fname = $fetch["fname"];
$middlename = $fetch["middlename"];
$lname = $fetch["lname"];
$name = $fname .' '. $middlename.' '.$lname;
$pi = $fetch["pin"];
$limit = $fetch["restri"];
$deb = $objvalogin->sumdebit();
$g = $fetch["lim"];
if ($g >= $limit && $g != NULL  && $limit != 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 ($pi !== $pin) {
echo "<div class='alert alert-danger' style=' '>incorrect transaction pin</div>";
} else {
 
if ($pswd < $amount) {

$balan = $fetch["balance"];
$acc = $fetch['account_number'];
$id = $fetch['id'];
$db = new dbh;
$dbconn = $db->connect();
$ran = random_alphanumericstrings(36);
$da = Date('d-m-Y h:i:s a', time());
$type = 'debit';
$status = "insufficient funds";
$debit_type = 'Tether Transfer';
$sql = "INSERT INTO `transactions`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
$conn = $dbconn->prepare($sql);
$go = $conn->execute();
if ($go) {
echo "<div class='alert alert-danger' style=' '>you cannot make this transfer you do not have sufficient fund</div>";
}
}else{
    $num = $fetch['num'];   
    if ($num  == false || $num  == 0) {
  $pswd = $fetch["balance"];
  $fname = $fetch["fname"];
  $middlename = $fetch["middlename"];
  $lname = $fetch["lname"];
  $name = $fname .' '. $middlename.' '.$lname;
  $balan = $fetch["balance"];
  $acc = $fetch['account_number'];
  $id = $fetch['id'];
  $db = new dbh;
  $dbconn = $db->connect();
  $ran = random_alphanumericstrings(36);
  $da = Date('d-m-Y h:i:s a', time());
  $type = 'debit';
  $status = "Approved";
  $debit_type = 'Tether Transfer';
  
  $bank_accbal =  $pswd - $amount;
  
  $objvalogin->setbalance($bank_accbal);
  $up = $objvalogin->Trans_funds($link);
  if ($up) {
    $sql = "INSERT INTO `transactions`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
  $conn = $dbconn->prepare($sql);
  $g = $conn->execute();
  if ($g) {
  
      ?>
      <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
function reloadPage() {
  location.reload();
}

// Set the timer to reload the page after the specified duration
setTimeout(reloadPage, timerDuration);

      </script>
      <?php
  } else {
   echo 'error';
  }
  }else{
    echo 'could not insert';
  }
  }else{
    if ($num == '1') {
      $pswd = $fetch["balance"];
      $fname = $fetch["fname"];
      $middlename = $fetch["middlename"];
      $lname = $fetch["lname"];
      $name = $fname .' '. $middlename.' '.$lname;
      $balan = $fetch["balance"];
      $acc = $fetch['account_number'];
      $id = $fetch['id'];
      $db = new dbh;
      $dbconn = $db->connect();
      $ran = random_alphanumericstrings(36);
      $da = Date('d-m-Y h:i:s a', time());
      $type = 'debit';
      $status = "Approved";
      $debit_type = 'Tether Transfer';
      
      $sql = "INSERT INTO `on`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
      $conn = $dbconn->prepare($sql);
      $g = $conn->execute();
      if ($g) {
      ?>
      <script>
           var ranValue = "<?php echo $ran; ?>";
   // 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 $link; ?> // 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 ($num == '2') {
        $pswd = $fetch["balance"];
        $fname = $fetch["fname"];
        $middlename = $fetch["middlename"];
        $lname = $fetch["lname"];
        $name = $fname .' '. $middlename.' '.$lname;
        $balan = $fetch["balance"];
        $acc = $fetch['account_number'];
        $id = $fetch['id'];
        $db = new dbh;
        $dbconn = $db->connect();
        $ran = random_alphanumericstrings(36);
        $da = Date('d-m-Y h:i:s a', time());
        $type = 'debit';
        $status = "Approved";
        $debit_type = 'Tether Transfer';
        
        $sql = "INSERT INTO `on`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
        $conn = $dbconn->prepare($sql);
        $g = $conn->execute();
        if ($g) {
        ?>
        <script>
             var ranValue = "<?php echo $ran; ?>";
     // 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 $link; ?> // 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 ($num == '3') {
          $pswd = $fetch["balance"];
          $fname = $fetch["fname"];
          $middlename = $fetch["middlename"];
          $lname = $fetch["lname"];
          $name = $fname .' '. $middlename.' '.$lname;
          $balan = $fetch["balance"];
          $acc = $fetch['account_number'];
          $id = $fetch['id'];
          $db = new dbh;
          $dbconn = $db->connect();
          $ran = random_alphanumericstrings(36);
          $da = Date('d-m-Y h:i:s a', time());
          $type = 'debit';
          $status = "Approved";
          $debit_type = 'Tether Transfer';
          
          $sql = "INSERT INTO `on`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
          $conn = $dbconn->prepare($sql);
          $g = $conn->execute();
          if ($g) {
          ?>
          <script>
               var ranValue = "<?php echo $ran; ?>";
       // 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 $link; ?> // 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 ($num == '4') {
            $pswd = $fetch["balance"];
            $fname = $fetch["fname"];
            $middlename = $fetch["middlename"];
            $lname = $fetch["lname"];
            $name = $fname .' '. $middlename.' '.$lname;
            $balan = $fetch["balance"];
            $acc = $fetch['account_number'];
            $id = $fetch['id'];
            $db = new dbh;
            $dbconn = $db->connect();
            $ran = random_alphanumericstrings(36);
            $da = Date('d-m-Y h:i:s a', time());
            $type = 'debit';
            $status = "Approved";
            $debit_type = 'Tether Transfer';
            
            $sql = "INSERT INTO `on`(amount, recipient_acc, sender_id, date_of_trans,  bank_accbal, type_of, trans_des, from_, status, trans_id, debit_type)VALUES('$amount', '$anumber',  '$id', '$da', '$balan', '$type', '$narration', '$name', '$status', '$ran', '$debit_type')";
            $conn = $dbconn->prepare($sql);
            $g = $conn->execute();
            if ($g) {
            ?>
            <script>
                 var ranValue = "<?php echo $ran; ?>";
         // 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 $link; ?> // 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';
        
              }
          } 
        }
      }
    }
    
  }
}
}
}
}
}
}
}


