<?php
session_start();
require_once("../config.php");

if(!isset($_SESSION['SESS_ADMINLOGGEDIN']) || $_SESSION['SESS_ADMINLOGGEDIN'] !== true){
    header("Location: " . $config_basedir."access");
    exit();
}

require("header.php");

// VALIDATE ID
function get_valid_id($key){
    return isset($_GET[$key]) && is_numeric($_GET[$key]) ? (int)$_GET[$key] : 0;
}

// DELETE
if(isset($_GET['del'])){
    $id = get_valid_id('del');
    if($id > 0){
        $stmt = $pdo->prepare("DELETE FROM transactions WHERE id = ?");
        $stmt->execute([$id]);
        echo "<div class='alert alert-danger text-center'>Transaction deleted</div>";
    }
}

// APPROVE → successful
if(isset($_GET['approve'])){
    $id = get_valid_id('approve');
    if($id > 0){
        $pdo->prepare("UPDATE transactions SET status = 'successful' WHERE id=?")->execute([$id]);
    }
    header("Location: transactions.php");
    exit;
}

// DECLINE → declined
if(isset($_GET['decline'])){
    $id = get_valid_id('decline');
    if($id > 0){
        $pdo->prepare("UPDATE transactions SET status = 'declined' WHERE id=?")->execute([$id]);
    }
    header("Location: transactions.php");
    exit;
}

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

<div class='page-wrapper'>

<div class='page-breadcrumb'>
<h4 class='page-title'>Customers Transactions</h4>
</div>

<div class='container-fluid'>
<div class='card'>
<div class='card-body'>

<input type='text' id='myInput' onkeyup='searchTable()' placeholder='Search...' class='form-control mb-3'>

<div class='table-responsive'>

<table id='myTable' class='table table-bordered text-center'>

<tr style='background:#0f2e5f;color:white;'>
<th>Depositor</th>
<th>Receiver</th>
<th>Depositor A/C</th>
<th>Receiver A/C</th>
<th>Amount</th>
<th>Routing</th>
<th>Bank</th>
<th>Swift</th>
<th>Note</th>
<th>Trans ID</th>
<th>Date</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>

<?php foreach($transactions as $row): 

$status = strtolower(trim($row['status']));

// STATUS BADGE
if($status == "successful"){
    $badge = "<span class='badge bg-success'>SUCCESSFUL</span>";
}
elseif($status == "declined"){
    $badge = "<span class='badge bg-dark'>DECLINED</span>";
}
else{
    $badge = "<span class='badge bg-danger'>PENDING</span>";
}

?>

<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><?= htmlspecialchars($row['trans_amount']) ?></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><?= $badge ?></td>



<td>
<a href="edit_transaction.php?id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">Edit</a>
</td>

<td>
<a href="transactions.php?del=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Delete this transaction?')">Delete</a>
</td>

</tr>

<?php endforeach; ?>

</table>

</div>
</div>
</div>
</div>

<?php require("footer.php"); ?>

<script>
function searchTable(){
    let input = document.getElementById("myInput");
    let filter = input.value.toLowerCase();
    let rows = document.querySelectorAll("#myTable tr");

    rows.forEach((row, index)=>{
        if(index === 0) return;
        let text = row.innerText.toLowerCase();
        row.style.display = text.includes(filter) ? "" : "none";
    });
}
</script>