<?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");
?>

<?php
// UPDATE TRANSACTION
if (isset($_POST['name']) && isset($_GET['id'])) {

    $id = $_GET['id'];

    try {
        $stmt = $pdo->prepare("UPDATE transactions SET 
            name = :name, 
            ben_name = :ben_name, 
            bank_name = :bank_name, 
            bank_swift = :bank_swift, 
            routin = :routin, 
            acc_no = :acc_no, 
            ben_acct_no = :ben_acct_no, 
            trans_amount = :trans_amount, 
            tran_type = :tran_type, 
            note = :note, 
            trans_id = :trans_id, 
            date = :date,
            status = :status
            WHERE id = :id");

        $stmt->execute([
            ':name' => $_POST['name'],
            ':ben_name' => $_POST['ben_name'],
            ':bank_name' => $_POST['bank_name'],
            ':bank_swift' => $_POST['bank_swift'],
            ':routin' => $_POST['routin'],
            ':acc_no' => $_POST['acc_no'],
            ':ben_acct_no' => $_POST['ben_acct_no'],
            ':trans_amount' => $_POST['trans_amount'],
            ':tran_type' => $_POST['tran_type'],
            ':note' => $_POST['note'],
            ':trans_id' => $_POST['trans_id'],
            ':date' => $_POST['date'],
            ':status' => $_POST['status'],
            ':id' => $id
        ]);

        echo "<div class='alert alert-success text-center mt-3'>
        Transaction updated successfully!
        <meta http-equiv='refresh' content='2; url=transactions.php'>
        </div>";

    } catch (PDOException $e) {
        echo "<div class='alert alert-danger text-center mt-3'>".$e->getMessage()."</div>";
    }
}
?>

<?php
// FETCH TRANSACTION
if (isset($_GET['id'])) {

    $stmt = $pdo->prepare("SELECT * FROM transactions WHERE id = ?");
    $stmt->execute([$_GET['id']]);
    $ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($ordrow) {

        $status = strtolower($ordrow['status']);

        $color = "red";
        if($status == "successful") $color = "green";
        elseif($status == "declined") $color = "black";
?>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">

<div class="card shadow-lg">

<div class="card-header bg-primary text-white">
<h5>Edit Transaction</h5>
</div>

<div class="card-body">

<form method="post">

<div class="form-group">
<label>Receiver Name</label>
<input type="text" name="name" class="form-control" value="<?= htmlspecialchars($ordrow['name']) ?>">
</div>

<div class="form-group">
<label>Beneficiary Name</label>
<input type="text" name="ben_name" class="form-control" value="<?= htmlspecialchars($ordrow['ben_name']) ?>">
</div>

<div class="form-group">
<label>Bank Name</label>
<input type="text" name="bank_name" class="form-control" value="<?= htmlspecialchars($ordrow['bank_name']) ?>">
</div>

<div class="form-group">
<label>Bank Swift</label>
<input type="text" name="bank_swift" class="form-control" value="<?= htmlspecialchars($ordrow['bank_swift']) ?>">
</div>

<div class="form-group">
<label>Routing No</label>
<input type="text" name="routin" class="form-control" value="<?= htmlspecialchars($ordrow['routin']) ?>">
</div>

<div class="form-group">
<label>Receiver Acc No</label>
<input type="text" name="acc_no" class="form-control" value="<?= htmlspecialchars($ordrow['acc_no']) ?>">
</div>

<div class="form-group">
<label>Beneficiary Acc No</label>
<input type="text" name="ben_acct_no" class="form-control" value="<?= htmlspecialchars($ordrow['ben_acct_no']) ?>">
</div>

<div class="form-group">
<label>Amount</label>
<input type="text" name="trans_amount" class="form-control" value="<?= htmlspecialchars($ordrow['trans_amount']) ?>">
</div>

<div class="form-group">
<label>Transaction ID</label>
<input type="text" name="trans_id" class="form-control" value="<?= htmlspecialchars($ordrow['trans_id']) ?>">
</div>

<input type="hidden" name="tran_type" value="<?= htmlspecialchars($ordrow['tran_type']) ?>">

<div class="form-group">
<label>Date</label>
<input type="text" name="date" class="form-control" value="<?= htmlspecialchars($ordrow['date']) ?>">
</div>

<div class="form-group">
<label>Note</label>
<input type="text" name="note" class="form-control" value="<?= htmlspecialchars($ordrow['note']) ?>">
</div>

<!-- ✅ STATUS -->
<div class="form-group">
<label>Status</label>
<select name="status" id="statusSelect" class="form-control" onchange="updateStatusColor()">
<option value="">Select</option>
<option value="pending" <?= $status=='pending'?'selected':'' ?>>Pending</option>
<option value="successful" <?= $status=='successful'?'selected':'' ?>>Successful</option>
<option value="declined" <?= $status=='declined'?'selected':'' ?>>Declined</option>
</select>
</div>

<!-- STATUS DISPLAY -->
<div id="statusLabel" class="mb-3 font-weight-bold">
<span style="color: <?= $color ?>;">
<?= strtoupper($status) ?>
</span>
</div>

<button class="btn btn-success btn-block">Update Transaction</button>

</form>

</div>
</div>

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

<script>
function updateStatusColor(){

    let val = document.getElementById('statusSelect').value;
    let label = document.getElementById('statusLabel');

    if(val === "pending"){
        label.innerHTML = "<span style='color:red;'>PENDING</span>";
    }
    else if(val === "successful"){
        label.innerHTML = "<span style='color:green;'>SUCCESSFUL</span>";
    }
    else if(val === "declined"){
        label.innerHTML = "<span style='color:black;'>DECLINED</span>";
    }
    else{
        label.innerHTML = "";
    }
}
</script>

<?php
    } else {
        echo "<div class='alert alert-danger text-center mt-3'>Transaction not found</div>";
    }
}
?>

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