<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('../includes/functions.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}

$allReports = getAllSafetyReports();
$unreadCount = count(array_filter($allReports, function($r) { return $r['status'] == 'unread'; }));
?>
<?php include('../includes/header.php'); ?>
<style>
    .report-row {
        border-bottom: 1px solid #eee;
        transition: background 0.2s;
    }
    .report-row:hover {
        background: #f8f9fa;
    }
    .report-unread {
        background: #fff3cd;
    }
    .status-badge {
        display: inline-block;
        padding: 3px 10px;
        border-radius: 15px;
        font-size: 11px;
        font-weight: bold;
    }
    .status-unread {
        background: #e74c3c;
        color: white;
    }
    .status-read {
        background: #27ae60;
        color: white;
    }
    .reply-count {
        background: #667eea;
        color: white;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 11px;
    }
    .btn-view {
        background: #3498db;
        color: white;
        padding: 5px 12px;
        border-radius: 5px;
        text-decoration: none;
        font-size: 12px;
    }
    .btn-view:hover {
        background: #2980b9;
    }
</style>
<div class="sidebar">
    <h3>📋 Admin Menu</h3>
    <a href="dashboard.php" class="btn" style="display: block;">📊 Dashboard</a>
    <a href="add_user.php" class="btn" style="display: block;">👤 Add Employee</a>
    <a href="manage_users.php" class="btn" style="display: block;">📋 Manage Users</a>
    <a href="edit_user.php" class="btn" style="display: block;">✏️ Edit User</a>
    <a href="manage_endorsements.php" class="btn" style="display: block;">✈️ Endorsements</a>
    <a href="add_memo.php" class="btn" style="display: block;">📝 Add Memo</a>
    <a href="add_quiz.php" class="btn" style="display: block;">📚 Add Quiz</a>
    <a href="inbox.php" class="btn" style="display: block; background: #667eea;">📥 Safety Reports</a>
    <a href="upload_manual.php" class="btn" style="display: block;">📘 Upload Manual</a>
</div>
<div class="content">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 10px;">
        <h2>📥 Safety Reports Inbox</h2>
        <?php if($unreadCount > 0): ?>
            <span style="background: #e74c3c; color: white; padding: 5px 15px; border-radius: 20px;">
                <?php echo $unreadCount; ?> Unread
            </span>
        <?php endif; ?>
    </div>
    
    <?php if(empty($allReports)): ?>
        <div class="card" style="text-align: center; padding: 40px;">
            <p>No safety reports have been submitted yet.</p>
        </div>
    <?php else: ?>
        <div class="card">
            <div class="table-responsive">
                <table style="width: 100%;">
                    <thead>
                        <tr style="background: #f8f9fa;">
                            <th>ID</th>
                            <th>Employee</th>
                            <th>Title</th>
                            <th>Replies</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($allReports as $report): ?>
                            <tr class="report-row <?php echo $report['status'] == 'unread' ? 'report-unread' : ''; ?>">
                                <td>#<?php echo $report['id']; ?></td>
                                <td>
                                    <strong><?php echo htmlspecialchars($report['user_name']); ?></strong><br>
                                    <small><?php echo htmlspecialchars($report['user_email']); ?></small>
                                </td>
                                <td><?php echo htmlspecialchars(substr($report['title'], 0, 40)) . (strlen($report['title']) > 40 ? '...' : ''); ?></td>
                                <td>
                                    <span class="reply-count">💬 <?php echo $report['reply_count']; ?> replies</span>
                                </td>
                                <td><small><?php echo date('M j, Y', strtotime($report['created_at'])); ?></small></td>
                                <td>
                                    <span class="status-badge <?php echo $report['status'] == 'unread' ? 'status-unread' : 'status-read'; ?>">
                                        <?php echo $report['status'] == 'unread' ? 'Unread' : 'Read'; ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="view_report.php?id=<?php echo $report['id']; ?>" class="btn-view">View & Reply</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php endif; ?>
</div>
<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>