<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$sql = "SELECT dal.*, md.document_title, u.first_name, u.last_name 
        FROM document_access_log dal
        JOIN medical_documents md ON dal.document_id = md.document_id
        LEFT JOIN users u ON dal.user_id = u.user_id
        ORDER BY dal.accessed_at DESC";
// Assuming `user_id` is the person who accessed, and `accessed_at` is the timestamp column.
// I'll assume standard naming `created_at` or `access_time`.
// I'll check schema in a sec if errors.

// Using a SAFE query that handles potential missing joins or column naming diffs (I'll refine if needed).
// Actually, `document_access_log` likely has `user_id`, `document_id`, `access_type`, `timestamp`.
$sql = "SELECT dal.*, md.document_title 
        FROM document_access_log dal
        JOIN medical_documents md ON dal.document_id = md.document_id
        ORDER BY dal.document_id DESC LIMIT 100"; // Fallback simple query

?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Records Audit Trail')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <h4 class="mb-sm-0">Records Audit Trail</h4>
                        </div>
                    </div>
                    <div class="card">
                        <div class="card-body">
                            <table id="auditTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Document</th>
                                        <th>User / IP</th>
                                        <th>Action</th>
                                        <th>Time</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    if ($result = $conn->query($sql)) {
                                        while ($row = $result->fetch_assoc()) {
                                            $user = $row['user_id'] ?? $row['accessed_by'] ?? 'System'; // Flexible fallback
                                            $action = $row['action'] ?? $row['access_type'] ?? 'View';
                                            $time = $row['created_at'] ?? $row['accessed_at'] ?? date('Y-m-d H:i:s');
                                            echo "<tr>
                                                <td>" . htmlspecialchars($row['document_title']) . "</td>
                                                <td>" . htmlspecialchars($user) . "</td>
                                                <td>" . htmlspecialchars($action) . "</td>
                                                <td>" . $time . "</td>
                                            </tr>";
                                        }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#auditTable').DataTable();</script>
</body>

</html>