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

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

$user_id = $_SESSION['SESS_USERID'];

$stmt = $pdo->prepare("SELECT * FROM transactions WHERE log_id = :id ORDER BY id DESC LIMIT 1");
$stmt->execute([':id' => $user_id]);
$txn = $stmt->fetch(PDO::FETCH_ASSOC);

// DEFAULT VALUES
$cur        = $txn['cur'] ?? 'USD';
$amount     = $txn['trans_amount'] ?? 0;
$ben_name   = $txn['ben_name'] ?? 'Beneficiary';
$bank_name  = $txn['bank_name'] ?? 'Receiving Bank';
$ref        = $txn['trans_id'] ?? strtoupper(uniqid('TXN'));
$date       = $txn['date'] ?? date("F j, Y g:i A");
$acct       = $txn['ben_acct_no'] ?? '********';
$note       = $txn['note'] ?? 'Transfer';
$hash       = strtoupper(substr(hash('sha256', $ref . $amount . $user_id), 0, 12));

/* ✅ IMPROVED STATUS LOGIC */
$statusRaw = strtolower(trim($txn['status'] ?? 'pending'));

switch($statusRaw){
    case "successful":
        $statusText = "SUCCESSFUL";
        $statusColor = "green";
        break;

    case "declined":
        $statusText = "DECLINED";
        $statusColor = "black";
        break;

    default:
        $statusText = "PENDING";
        $statusColor = "red";
        break;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Transaction Receipt</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body {
    background: #f7f9fb;
    font-family: Arial, Helvetica, sans-serif;
}

.receipt-box {
    max-width: 600px;
    margin: 40px auto;
    background: #fff;
    padding: 30px;
    border: 1px solid #ddd;
}

.receipt-header {
    display: flex;
    justify-content: space-between;
    border-bottom: 2px solid #000;
    padding-bottom: 10px;
    margin-bottom: 20px;
}

.bank-title {
    font-size: 18px;
    font-weight: bold;
}

.status {
    font-weight: bold;
    font-size: 13px;
}

.amount {
    font-size: 32px;
    font-weight: bold;
    margin: 20px 0;
}

.row-line {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid #eee;
    font-size: 14px;
}

.label { color: #555; }
.value { font-weight: 500; }

.actions {
    margin-top: 25px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.btn-custom {
    flex: 1;
    padding: 10px;
    font-size: 14px;
}

.footer {
    margin-top: 20px;
    font-size: 12px;
    text-align: center;
    color: #777;
}

@media print {
    .actions { display: none; }
    body { background: #fff; }
}
</style>
</head>

<body>

<div class="receipt-box">

    <!-- HEADER -->
    <div class="receipt-header">
        <div class="bank-title">TRANSFER RECEIPT</div>
        <div class="status" style="color: <?= $statusColor ?>;">
            <?= $statusText ?>
        </div>
    </div>

    <!-- AMOUNT -->
    <div class="amount">
        <?= htmlspecialchars($cur) ?> <?= number_format((float)$amount,2) ?>
    </div>

    <!-- DETAILS -->
    <div class="row-line">
        <span class="label">Recipient</span>
        <span class="value"><?= htmlspecialchars($ben_name) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Bank</span>
        <span class="value"><?= htmlspecialchars($bank_name) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Account Number</span>
        <span class="value"><?= htmlspecialchars($acct) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Transaction Date</span>
        <span class="value"><?= htmlspecialchars($date) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Reference</span>
        <span class="value"><?= htmlspecialchars($ref) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Note</span>
        <span class="value"><?= htmlspecialchars($note) ?></span>
    </div>

    <div class="row-line">
        <span class="label">Verification Code</span>
        <span class="value"><?= $hash ?></span>
    </div>

    <!-- BUTTONS -->
    <div class="actions">
        <button onclick="window.print()" class="btn btn-dark btn-custom">
            Print / Save PDF
        </button>

        <a href="dashboard.php" class="btn btn-primary btn-custom">
            Go to Dashboard
        </a>
    </div>

    <!-- FOOTER -->
    <div class="footer">
        This is an electronically generated receipt. No signature required.<br>
        © <?= date("Y") ?> Secure Banking System
    </div>

</div>

</body>
</html>