<?php require ("header.php")?>


      
      
    
      
      
      
      
      
      
      
      
      
       <?php
session_start();
if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) {
    header("Location: " . $config_basedir."access");
}
require_once("../config.php");

?>
    






<?php

// Validate integer from GET
function get_valid_id($key) {
    return isset($_GET[$key]) && is_numeric($_GET[$key]) ? (int)$_GET[$key] : 0;
}

// DELETE transaction
if (isset($_GET['del'])) {
    $id = get_valid_id('del');
    if ($id > 0) {
        $stmt = $pdo->prepare("DELETE FROM transactions WHERE id = :id");
        $stmt->execute(['id' => $id]);
        $delete = 1;
    }
}

// APPROVE transaction (status = 1)
if (isset($_GET['funcAPPROV']) && $_GET['funcAPPROV'] === "conf") {
    $id = get_valid_id('id');
    if ($id > 0) {
        $stmt = $pdo->prepare("UPDATE transactions SET status = 1 WHERE id = :id");
        $stmt->execute(['id' => $id]);
    }
    header("Location: " . $config_basedir . "transactions.php");
    exit;
}

// DECLINE transaction (status = 2)
if (isset($_GET['funcDECLN']) && $_GET['funcDECLN'] === "conf") {
    $id = get_valid_id('id');
    if ($id > 0) {
        $stmt = $pdo->prepare("UPDATE transactions SET status = 2 WHERE id = :id");
        $stmt->execute(['id' => $id]);
    }
    header("Location: " . $config_basedir . "transactions.php");
    exit;
}

// END transaction (status = 3)
if (isset($_GET['funcEND']) && $_GET['funcEND'] === "conf") {
    $id = get_valid_id('id');
    if ($id > 0) {
        $stmt = $pdo->prepare("UPDATE transactions SET status = 3 WHERE id = :id");
        $stmt->execute(['id' => $id]);
    }
    header("Location: " . $config_basedir . "transactions.php");
    exit;
}
?>
  
      
      
      
      
      <div class='page-wrapper'>
           
            <div class='page-breadcrumb'>
                <div class='row'>
                    <div class='col-12 d-flex no-block align-items-center'>
                        <h4 class='page-title'>Customers transactons</h4>
                        <div class='ml-auto text-right'>
                            <nav aria-label='breadcrumb'>
                                <ol class='breadcrumb'>
                                    <li class='breadcrumb-item'><a href='index.php'>Home</a></li>
                                    <li class='breadcrumb-item active' aria-current='page'>Customers transactons</li>
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
           
            <div class='container-fluid'>
               
                <div class='row'>
                    <div class='col-12'>
                        <div class='card'>
                           
                 
               
                 
                 
            <?php

// Fetch all transactions
$stmt = $pdo->query("SELECT * FROM transactions ORDER BY id DESC");
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Show success message if deletion occurred
if (isset($delete)) {
    echo "<span style='color:red'>Successfully deleted transaction</span>";
}

echo "
<input type='text' id='myInput' onkeyup='myFunction()' placeholder='Search for names..' title='Type in a name' class='form-control mb-3'>
<div class='card'>
    <div class='card-body'>
        <div class='table-responsive'>
            <table id='myTable' class='table table-striped table-bordered text-center'>
                <tr style='background-color:#0f2e5f; height:30px; color:white;'>
                    
                    <th>DEPOSITOR NAME</th>
                    <th>RECEIVER NAME</th>
                    
                    <th>DEPOSITOR A/C</th>
                    <th>RECEIVER A/C</th>
                    
                    <th>AMOUNT</th>
                    <th>ROUTIN NO.</th>
                    <th>BANK NAME</th>
                    <th>SWIFT CODE</th>
                    <th>DESCRIPTION</th>
                    <th>TRANS ID</th>
                    <th>DATE</th>
                    <th>STATUS</th>
                    <th>UPDATE</th>
                    <th>DEL</th>
                </tr>
";

foreach ($transactions as $row) {
    $amountColor = ($row['tran_type'] == 1 || $row['tran_type2'] == 1) ? '#ff0000' : '#33cc33';
    $amountText = htmlspecialchars($row['trans_amount']);

    // Normalize status
    $statusRaw = strtolower(trim($row['status']));
    if ($statusRaw === 'successful') {
        $statusBadge = "<span class='badge bg-success text-white'>Successful</span>";
    } elseif ($statusRaw === 'pending' || $statusRaw === '0' || $row['status'] === 0) {
        $statusBadge = "<span class='badge bg-danger text-white'>Pending</span>";
    } else {
        $statusBadge = "<span class='badge bg-secondary text-white'>Unknown</span>";
    }

    echo "<tr>
        <td>" . htmlspecialchars($row['name']) . "</td>
        <td>" . htmlspecialchars($row['ben_name']) . "</td>
        <td>" . htmlspecialchars($row['acc_no']) . "</td>
        <td>" . htmlspecialchars($row['ben_acct_no']) . "</td>
        <td style='color:$amountColor'>" . $amountText . "</td>
        <td>" . htmlspecialchars($row['routin']) . "</td>
        <td>" . htmlspecialchars($row['bank_name']) . "</td>
        <td>" . htmlspecialchars($row['bank_swift']) . "</td>
        <td>" . htmlspecialchars($row['note']) . "</td>
        <td>" . htmlspecialchars($row['trans_id']) . "</td>
        <td>" . htmlspecialchars($row['date']) . "</td>
        <td>$statusBadge</td>
        <td><a class='btn btn-success' href='edit_transaction.php?id=" . $row['id'] . "' style='color:white'>UPDATE</a></td>
        <td><a class='btn btn-danger' href='transactions.php?del=" . $row['id'] . "' style='color:white;'>DELETE</a></td>
    </tr>";
}

echo "</table></div></div></div>";
?>

                        
                        
                    </div>
                </div>
               
            </div>
           
           <?php require ("footer.php")?>