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

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

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

$userId = $_SESSION['user_id'];
$userReports = getUserSafetyReports($userId);
?>
<?php include('../includes/header.php'); ?>
<style>
    .report-card {
        background: white;
        border-radius: 20px;
        margin-bottom: 20px;
        overflow: hidden;
        border: 1px solid #e2cfb3;
        transition: transform 0.1s ease, box-shadow 0.2s;
    }
    .report-card:hover {
        box-shadow: 0 6px 14px rgba(0,0,0,0.05);
    }
    .report-header {
        background: #faf3e8;
        padding: 14px 18px;
        border-bottom: 1px solid #e2cfb3;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 10px;
    }
    .report-title {
        font-size: 1.1rem;
        font-weight: 700;
        color: #6b1d2f;
        font-family: 'Georgia', serif;
    }
    .attachment-icon {
        color: #c9a87c;
        margin-left: 6px;
        font-size: 0.9rem;
    }
    .reply-count {
        background: #e2cfb3;
        color: #3e2c1f;
        padding: 2px 10px;
        border-radius: 30px;
        font-size: 0.7rem;
        font-weight: bold;
        margin-left: 12px;
    }
    .report-status {
        padding: 4px 12px;
        border-radius: 40px;
        font-size: 0.7rem;
        font-weight: 600;
    }
    .status-unread {
        background: #a03e4b;
        color: white;
    }
    .status-read {
        background: #8b6b42;
        color: #faf3e8;
    }
    .btn-view {
        background: #6b1d2f;
        color: #f9e4c2;
        padding: 6px 14px;
        border-radius: 40px;
        text-decoration: none;
        font-size: 0.75rem;
        font-weight: 500;
        transition: 0.2s;
        display: inline-block;
    }
    .btn-view:hover {
        background: #8b2635;
    }
    .report-body {
        padding: 16px 18px;
    }
    .report-meta {
        font-size: 0.7rem;
        color: #8b7355;
        margin-bottom: 10px;
    }
    .report-preview {
        color: #3e2c1f;
        font-size: 0.85rem;
        line-height: 1.4;
    }
    .attachment-badge {
        margin-top: 8px;
        font-size: 0.7rem;
        color: #c9a87c;
    }
    .no-reports {
        text-align: center;
        padding: 40px 20px;
        background: #faf3e8;
        border-radius: 32px;
        color: #8b7355;
    }
</style>

<div class="sidebar">
    <h3>✈️ Crew Menu</h3>
    <a href="dashboard.php" class="btn">🏠 Dashboard</a>
    <a href="view_memos.php" class="btn">📝 Memos</a>
    <a href="take_quiz.php" class="btn">📚 Quizzes</a>
    <a href="safety_report.php" class="btn">⚠️ New Report</a>
    <a href="my_reports.php" class="btn active">📋 My Reports</a>
    <a href="profile.php" class="btn">👤 Profile</a>
    <a href="view_manual.php" class="btn">📘 Maintenance Manual</a>
</div>

<div class="content">
    <h2>📋 My Safety Reports</h2>

    <?php if(empty($userReports)): ?>
        <div class="no-reports">
            <p>✈️ You haven't submitted any safety reports yet.</p>
            <a href="safety_report.php" class="btn btn-primary" style="margin-top: 15px;">+ Submit New Report</a>
        </div>
    <?php else: ?>
        <?php foreach($userReports as $report): ?>
            <div class="report-card">
                <div class="report-header">
                    <div>
                        <span class="report-title">
                            <?php echo htmlspecialchars($report['title']); ?>
                            <?php if (!empty($report['attachments'])): ?>
                                <span class="attachment-icon" title="Has attachments">📎</span>
                            <?php endif; ?>
                        </span>
                        <span class="reply-count">💬 <?php echo isset($report['replies']) ? count($report['replies']) : 0; ?> replies</span>
                    </div>
                    <div>
                        <span class="report-status <?php echo $report['status'] == 'unread' ? 'status-unread' : 'status-read'; ?>">
                            <?php echo $report['status'] == 'unread' ? '🔴 Unread' : '✅ Read'; ?>
                        </span>
                        <a href="view_report.php?id=<?php echo $report['id']; ?>" class="btn-view">View Details →</a>
                    </div>
                </div>
                <div class="report-body">
                    <div class="report-meta">
                        📅 Submitted: <?php echo date('F j, Y g:i A', strtotime($report['created_at'])); ?>
                    </div>
                    <div class="report-preview">
                        <?php echo nl2br(htmlspecialchars(substr($report['content'], 0, 150))); ?>
                        <?php echo strlen($report['content']) > 150 ? '…' : ''; ?>
                    </div>
                    <?php if (!empty($report['attachments'])): ?>
                        <div class="attachment-badge">
                            📎 <?php echo count($report['attachments']); ?> file(s) attached
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>