<?php
session_start();
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 4) {
    http_response_code(403);
    echo "Unauthorized";
    exit();
}

include('../../includes/config.php');

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo "<p style='color:red;'>Invalid transaction ID.</p>";
    exit();
}

$id = (int)$_GET['id'];

$sql = "SELECT ft.*, s.store_name, u.first_name, u.last_name
        FROM financial_transactions ft
        LEFT JOIN stores s ON ft.store_id = s.id
        LEFT JOIN users u ON ft.created_by = u.id
        WHERE ft.id = ? LIMIT 1";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo "<p style='color:red;'>Transaction not found or voided.</p>";
    exit();
}

$transaction = $result->fetch_assoc();

function safe($str) {
    return htmlspecialchars($str);
}
?>

<style>
body {
    margin: 0;
    background: url('../../images/profile.jpg') no-repeat center center fixed;
    background-size: cover;
    font-family: 'Segoe UI', sans-serif;
    color: #eee;
}
.content {
    margin: 2rem auto;
    padding: 40px;
    max-width: 1850px;
}
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}
h1.page-title {
    color: #ffa500;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 2rem;
}
h1.page-title img {
    height: 40px;
    width: auto;
}
a.back-link {
    color: #ffa500;
    text-decoration: none;
    font-weight: 600;
    border: 1.5px solid #ffa500;
    padding: 6px 14px;
    border-radius: 8px;
    transition: background-color 0.3s ease, color 0.3s ease;
    white-space: nowrap;
}
a.back-link:hover {
    background-color: #ffa500;
    color: #222;
}
.card {
    background: rgba(255,255,255,0.1);
    border-radius: 15px;
    padding: 30px;
    margin-top: 30px;
}
.detail-table {
    width: 100%;
    border-collapse: collapse;
    color: #eee;
    font-size: 15px;
}
.detail-table th, .detail-table td {
    padding: 10px 12px;
    border-bottom: 1px solid rgba(255,255,255,0.15);
    vertical-align: top;
}
.detail-table th {
    background: rgba(255,165,0,0.3);
    color: #fff;
    text-align: left;
    width: 200px;
}
.detail-table td {
    color: #eee;
}
</style>

<div class="content">
    <div class="page-header">
        <h1 class="page-title">
            <img src="../../images/logo.png" alt="Logo" />
            Transaction Details
        </h1>
        <a href="transactions.php" class="back-link">&laquo; Back to Financial Transactions</a>
    </div>

    <div class="card">
        <table class="detail-table" role="presentation" aria-label="Transaction Details">
            <tr>
                <th>Date</th>
                <td><?= safe($transaction['transaction_date']) ?></td>
            </tr>
            <tr>
                <th>Type</th>
                <td><?= ucwords(str_replace('_', ' ', safe($transaction['type']))) ?></td>
            </tr>
            <tr>
                <th>Amount (MWK)</th>
                <td><?= number_format($transaction['amount'], 2) ?></td>
            </tr>
            <tr>
                <th>Payment Method</th>
                <td><?= ucwords(str_replace('_', ' ', safe($transaction['payment_method']))) ?></td>
            </tr>
            <tr>
                <th>Account</th>
                <td><?= safe($transaction['account']) ?></td>
            </tr>
            <tr>
                <th>Store</th>
                <td><?= safe($transaction['store_name']) ?></td>
            </tr>
            <tr>
                <th>Description</th>
                <td><?= nl2br(safe($transaction['description'])) ?></td>
            </tr>
            <tr>
                <th>Status</th>
                <td><?= safe(ucwords($transaction['status'])) ?></td>
            </tr>
            <tr>
                <th>Reference ID</th>
                <td><?= $transaction['reference_id'] ? safe($transaction['reference_id']) : '<em>None</em>' ?></td>
            </tr>
            <tr>
                <th>Created By</th>
                <td><?= safe($transaction['first_name'] . ' ' . $transaction['last_name']) ?></td>
            </tr>
            <tr>
                <th>Created At</th>
                <td><?= safe($transaction['created_at']) ?></td>
            </tr>
            <tr>
                <th>Updated At</th>
                <td><?= $transaction['updated_at'] ? safe($transaction['updated_at']) : '<em>Never updated</em>' ?></td>
            </tr>
        </table>
    </div>
</div>
