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


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

?>
    


      

  
      
       <?php
if (isset($_POST['name']) && isset($_GET['id'])) {
    $id = $_GET['id'];

    $name = $_POST['name'];
    $acc_no = $_POST['acc_no'];
    $trans_amount = $_POST['trans_amount'];
    $tran_type = $_POST['tran_type'];
    $note = $_POST['note'];
    $trans_id = $_POST['trans_id'];
    $date = $_POST['date'];
    $ben_acct_no = $_POST['ben_acct_no'];
    $ben_name = $_POST['ben_name'];
    $bank_name = $_POST['bank_name'];
    $bank_swift = $_POST['bank_swift'];
    $routin = $_POST['routin'];
    $status = $_POST['status'];

    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' => $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,
            ':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'>Error: " . $e->getMessage() . "</div>";
    }
}
?>

<?php
if (isset($_GET['id'])) {
    $id = $_GET['id'];

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

        if ($ordrow) {
?>
            <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 col-sm-12">
                        <div class="card shadow-lg">
                            <div class="card-header bg-primary text-white">
                                <h5 class="mb-0">Edit Transaction Details</h5>
                            </div>
                            <div class="card-body">
                                <form method="post">
                                    <!-- Your input fields -->
                                    <div class="form-group">
                                        <label>Receiver Name</label>
                                        <input type="text" class="form-control" name="name" value="<?= htmlspecialchars($ordrow['name']) ?>">
                                    </div>

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

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

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

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

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

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

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

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

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

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

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

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

                                    <div id="statusLabel" class="mb-3 font-weight-bold">
                                        <?php if ($ordrow['status'] === 'pending') : ?>
                                            <span style="color:red;">Pending</span>
                                        <?php elseif ($ordrow['status'] === 'successful') : ?>
                                            <span style="color:green;">Successful</span>
                                        <?php endif; ?>
                                    </div>

                                    <button type="submit" class="btn btn-success btn-block">Update Transaction</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <script>
            function updateStatusColor() {
                const select = document.getElementById('statusSelect');
                const label = document.getElementById('statusLabel');

                if (select.value === "pending") {
                    label.innerHTML = "<span style='color:red;'>Pending</span>";
                } else if (select.value === "successful") {
                    label.innerHTML = "<span style='color:green;'>Successful</span>";
                } else {
                    label.innerHTML = "";
                }
            }
            </script>
<?php
        } else {
            echo "<div class='alert alert-danger text-center mt-3'>Transaction not found.</div>";
        }
    } catch (PDOException $e) {
        echo "<div class='alert alert-danger text-center mt-3'>Error fetching transaction: " . $e->getMessage() . "</div>";
    }
}
?>



           
           <?php require ("footer.php")?>
           
<script>
function myFunction() {
  window.print(); }
</script>