<?php
session_start();
include('../../includes/config.php');
include('../../includes/header.php');

if (!in_array($_SESSION['role_id'], [1, 4])) {
    die("Access Denied.");
}

// Filters
$date_from = $_GET['date_from'] ?? date('Y-m-01');
$date_to = $_GET['date_to'] ?? date('Y-m-d');

$stmt = $conn->prepare("
    SELECT al.*, bt.reference AS deposit_reference, ft.description AS ft_description, u.username 
    FROM reconciliation_audit_logs al
    LEFT JOIN bank_transactions bt ON al.bank_transaction_id = bt.id
    LEFT JOIN financial_transactions ft ON al.financial_transaction_id = ft.id
    LEFT JOIN users u ON al.user_id = u.id
    WHERE DATE(al.created_at) BETWEEN ? AND ?
    ORDER BY al.created_at DESC
");
$stmt->bind_param("ss", $date_from, $date_to);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><img src="../../images/logo.png" alt="Logo" style="height:24px;vertical-align:middle;"> Reconciliation Audit History</title>
    <link rel="icon" href="../../images/logo.png" type="image/png">
    <style>
        body {
            margin: 0;
            padding-top: 40px; /* space for header */
            background: url('../../images/profile.jpg') no-repeat center center fixed;
            background-size: cover;
            font-family: 'Segoe UI', sans-serif;
            color: #eee;
        }

        .content {
            margin: 0 auto;
            padding: 40px;
            max-width: 1850px;
        }

        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-header h2 {
            color: #ffa500;
            font-size: 28px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .page-header h2 img {
            height: 30px;
        }

        .back-link {
            background-color: #ffa500;
            color: #000;
            text-decoration: none;
            padding: 10px 16px;
            border-radius: 8px;
            font-weight: bold;
        }

        .filter-form {
            display: flex;
            gap: 15px;
            margin-bottom: 25px;
            flex-wrap: wrap;
        }

        .filter-form input,
        .filter-form select,
        .filter-form button,
        .filter-form a {
            padding: 10px 14px;
            border-radius: 8px;
            border: none;
            font-weight: 500;
            font-size: 14px;
        }

        .filter-form button {
            background-color: #ffa500;
            color: #000;
            cursor: pointer;
        }

        .filter-form a {
            background-color: #ffa500;
            color: #000;
            text-decoration: none;
        }

        .card {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            padding: 25px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
            color: #fff;
        }

        table th, table td {
            padding: 12px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            text-align: left;
        }

        table th {
            background-color: rgba(255, 165, 0, 0.3);
            color: #fff;
        }

        .no-data {
            text-align: center;
            font-style: italic;
            color: #ccc;
        }
    </style>
</head>
<body>

<div class="content">
    <div class="page-header">
        <h2><img src="../../images/logo.png" alt="Logo"> Reconciliation Audit History</h2>
        <a href="bank_reconciliation.php" class="back-link">← Back to Reconciliation</a>
    </div>

    <form method="GET" class="filter-form">
        <input type="date" name="date_from" value="<?= htmlspecialchars($date_from) ?>">
        <input type="date" name="date_to" value="<?= htmlspecialchars($date_to) ?>">
        <button type="submit">Filter</button>
        <a href="?<?= http_build_query(array_merge($_GET, ['export' => 'csv'])) ?>">Export CSV</a>
        <a href="?<?= http_build_query(array_merge($_GET, ['export' => 'pdf'])) ?>">Export PDF</a>
    </form>

    <div class="card">
        <table>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Event</th>
                    <th>Reference</th>
                    <th>FT Description</th>
                    <th>Amount</th>
                    <th>User</th>
                    <th>Note</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($result->num_rows === 0): ?>
                    <tr><td colspan="7" class="no-data">No audit logs found for selected date range.</td></tr>
                <?php else: ?>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['created_at']) ?></td>
                            <td><?= htmlspecialchars(ucfirst($row['event_type'])) ?></td>
                            <td><?= htmlspecialchars($row['deposit_reference'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($row['ft_description'] ?? '-') ?></td>
                            <td>MWK <?= number_format($row['amount'], 2) ?></td>
                            <td><?= htmlspecialchars($row['username']) ?></td>
                            <td><?= htmlspecialchars($row['description'] ?? '-') ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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