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

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

$userId = $_SESSION['user_id'];
$user = getUserById($userId);
$unreadMemos = getUnreadMemos($userId);
$pendingQuizzes = getPendingQuizzes($userId);
$expiringLicenses = getUserExpiringLicenses($userId);

// Daily popup reset
if (isset($_SESSION['expiry_popup_date']) && $_SESSION['expiry_popup_date'] != date('Y-m-d')) {
    $_SESSION['expiry_popup_shown'] = false;
}
$_SESSION['expiry_popup_date'] = date('Y-m-d');
if (!isset($_SESSION['expiry_popup_shown'])) {
    $_SESSION['expiry_popup_shown'] = false;
}
?>
<?php include('../includes/header.php'); ?>

<!-- Expiry Notification Popup -->
<?php if (!empty($expiringLicenses) && !$_SESSION['expiry_popup_shown']): 
    $_SESSION['expiry_popup_shown'] = true; ?>
<div style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 2000;">
    <div style="background: #faf3e8; border-radius: 24px; max-width: 90%; width: 400px; padding: 20px; border-top: 6px solid #c9a87c;">
        <h3 style="color: #6b1d2f;">⚠️ License Expiry Warning</h3>
        <?php foreach($expiringLicenses as $item): ?>
            <p><strong><?php echo $item['type']; ?>:</strong> expires <?php echo date('M j, Y', strtotime($item['expiry_date'])); ?> (<?php echo $item['days_left']; ?> days left)</p>
        <?php endforeach; ?>
        <button onclick="this.parentElement.parentElement.style.display='none'" class="btn btn-primary" style="margin-top: 15px;">I Understand</button>
    </div>
</div>
<?php endif; ?>

<div class="sidebar">
    <h3>✈️ Crew Menu</h3>
    <a href="dashboard.php" class="btn active">🏠 Dashboard</a>
    <a href="view_memos.php" class="btn">📝 Memos <?php if(count($unreadMemos) > 0): ?> (<?php echo count($unreadMemos); ?>)<?php endif; ?></a>
    <a href="take_quiz.php" class="btn">📚 Quizzes <?php if(count($pendingQuizzes) > 0): ?> (<?php echo count($pendingQuizzes); ?>)<?php endif; ?></a>
    <a href="safety_report.php" class="btn">⚠️ Safety Report</a>
    <a href="my_reports.php" class="btn">📋 My Reports</a>
    <a href="chat.php" class="btn">💬 Group Chat</a>
    <a href="profile.php" class="btn">👤 Profile</a>
    <a href="view_manual.php" class="btn">📘 Maintenance Manual</a>
</div>
<div class="content">
    <div class="flex-between" style="align-items: center;">
        <h2>Welcome, <?php echo htmlspecialchars($user['full_name']); ?> ✈️</h2>
        <div class="propeller-loader" style="width: 32px; height: 32px;"></div>
    </div>
    
    <?php $expiring30 = getExpiringLicenses($user); ?>
    <?php if(!empty($expiring30)): ?>
        <div class="alert alert-warning">⚠️ License(s) expiring within 30 days: <?php echo implode(', ', $expiring30); ?></div>
    <?php endif; ?>
    
    <div class="grid-2">
        <div class="card radar-sweep">
            <h3>📬 Unread Memos</h3>
            <p style="font-size: 2rem; font-weight: bold;"><?php echo count($unreadMemos); ?></p>
            <a href="view_memos.php" class="btn btn-sm btn-primary">Read Now</a>
        </div>
        <div class="card radar-sweep">
            <h3>📝 Pending Quizzes</h3>
            <p style="font-size: 2rem; font-weight: bold;"><?php echo count($pendingQuizzes); ?></p>
            <a href="take_quiz.php" class="btn btn-sm btn-primary">Take Quiz</a>
        </div>
    </div>
    
    <!-- The safety report form has been removed from dashboard (available via sidebar) -->
</div>
<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>