<?php
session_start();
if (!isset($_SESSION['role_id']) || !in_array($_SESSION['role_id'], [1, 4])) {
    header("Location: ../../index.php");
    exit();
}

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

// Fetch logs
$logsResult = $conn->query("
    SELECT l.*, 
           u.first_name, u.last_name, 
           e.reference AS expense_reference,
           a.account_name
    FROM expense_approval_logs l
    LEFT JOIN users u ON l.performed_by = u.id
    LEFT JOIN expenses e ON l.expense_id = e.id
    LEFT JOIN accounts a ON l.account_id = a.id
    ORDER BY l.created_at DESC
");
?>

<!-- Styles -->
<style>
body {
    font-family: 'Segoe UI', sans-serif;
    background: url('../../images/profile.jpg') no-repeat center center fixed;
    background-size: cover;
    color: #eee;
}
.container {
    margin: 50px auto;
    max-width: 1100px;
    background: rgba(255, 255, 255, 0.08);
    padding: 25px;
    border-radius: 10px;
    backdrop-filter: blur(10px);
}
h2 {
    text-align: center;
    color: #ffa500;
}
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}
th, td {
    padding: 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}
th {
    color: #ffa500;
    text-align: left;
}
tr:hover {
    background: rgba(255, 255, 255, 0.05);
}
.back-link {
    color: #00c0ff;
    display: inline-block;
    margin-bottom: 15px;
    text-decoration: none;
}
.print-btn {
    float: right;
    padding: 6px 12px;
    background-color: #ffa500;
    border: none;
    color: white;
    font-weight: bold;
    border-radius: 5px;
    cursor: pointer;
}
</style>

<!-- HTML -->
<div class="container">
    <a href="approve_expenses.php" class="back-link">← Back to Approvals</a>
    <button class="print-btn" onclick="window.print()">🖨️ Print</button>

    <h2>Expense Approval History</h2>

    <table>
        <thead>
            <tr>
                <th>Expense Ref</th>
                <th>Action</th>
                <th>By</th>
                <th>Payment Account</th>
                <th>Action Time</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($logsResult->num_rows > 0): ?>
                <?php while ($log = $logsResult->fetch_assoc()): ?>
                    <tr>
                        <td><?= htmlspecialchars($log['expense_reference'] ?? 'N/A') ?></td>
                        <td><?= ucfirst(htmlspecialchars($log['action'])) ?></td>
                        <td><?= htmlspecialchars($log['first_name'] . ' ' . $log['last_name']) ?></td>
                        <td><?= htmlspecialchars($log['account_name'] ?? 'N/A') ?></td>
                        <td><?= htmlspecialchars($log['created_at']) ?></td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="5" style="text-align:center;">No approval history found.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php include('../../includes/footer.php'); ?>
