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

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

$userId = $_SESSION['user_id'];
$userMemos = getUserMemos($userId);
$unreadCount = count(array_filter($userMemos, function($memo) {
    return !$memo['is_read'];
}));
?>
<?php include('../includes/header.php'); ?>
<style>
    .memo-table {
        width: 100%;
        border-collapse: collapse;
        background: white;
        border-radius: 20px;
        overflow: hidden;
    }
    .memo-table th {
        background: #f8efe5;
        color: #6b1d2f;
        font-weight: 600;
        padding: 12px;
        border-bottom: 1px solid #e2cfb3;
    }
    .memo-table td {
        padding: 12px;
        border-bottom: 1px solid #f0e2d0;
        vertical-align: middle;
    }
    .memo-unread {
        background: #fef7e8;
    }
    .status-badge {
        display: inline-block;
        padding: 4px 10px;
        border-radius: 50px;
        font-size: 0.7rem;
        font-weight: 600;
    }
    .status-read {
        background: #8b6b42;
        color: #faf3e8;
    }
    .status-unread {
        background: #a03e4b;
        color: white;
    }
    .attachment-icon {
        color: #c9a87c;
        font-size: 1rem;
    }
    .btn-sm {
        padding: 5px 12px;
        font-size: 0.7rem;
        background: #6b1d2f;
        color: #f9e4c2;
        border-radius: 40px;
        text-decoration: none;
        transition: 0.2s;
        display: inline-block;
    }
    .btn-sm:hover {
        background: #8b2635;
    }
    .no-memos {
        text-align: center;
        padding: 40px;
        background: #faf3e8;
        border-radius: 20px;
        color: #8b7355;
    }
    @media (max-width: 640px) {
        .memo-table th, .memo-table td {
            padding: 8px;
            font-size: 0.8rem;
        }
        .btn-sm {
            padding: 4px 8px;
            font-size: 0.65rem;
        }
    }
</style>

<div class="sidebar">
    <h3>✈️ Crew Menu</h3>
    <a href="dashboard.php" class="btn">🏠 Dashboard</a>
    <a href="view_memos.php" class="btn active">📝 All 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">📋 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">
    <div class="flex-between" style="margin-bottom: 20px;">
        <h2>📝 My Memos</h2>
        <?php if($unreadCount > 0): ?>
            <span class="badge badge-danger"><?php echo $unreadCount; ?> Unread</span>
        <?php endif; ?>
    </div>

    <?php if(empty($userMemos)): ?>
        <div class="no-memos">
            <p>✈️ No memos have been assigned to you yet.</p>
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="memo-table">
                <thead>
                    <tr>
                        <th>Status</th>
                        <th>Title</th>
                        <th>Attachment</th>
                        <th>Date Issued</th>
                        <th>Acknowledged On</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($userMemos as $memo): ?>
                        <tr class="<?php echo !$memo['is_read'] ? 'memo-unread' : ''; ?>">
                            <td>
                                <?php if($memo['is_read']): ?>
                                    <span class="status-badge status-read">✓ Read</span>
                                <?php else: ?>
                                    <span class="status-badge status-unread">● Unread</span>
                                <?php endif; ?>
                            </td>
                            <td><strong><?php echo htmlspecialchars($memo['title']); ?></strong></td>
                            <td class="text-center">
                                <?php if(isset($memo['has_pdf']) && $memo['has_pdf']): ?>
                                    <span class="attachment-icon" title="PDF attached">📎</span>
                                <?php else: ?>
                                    <span style="color:#e2cfb3;">—</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo date('M j, Y', strtotime($memo['created_at'])); ?></td>
                            <td>
                                <?php if(isset($memo['read_at'][$userId])): ?>
                                    <?php echo date('M j, Y g:i A', strtotime($memo['read_at'][$userId])); ?>
                                <?php else: ?>
                                    <span style="color:#b58b3c;">Not yet</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="view_memo.php?id=<?php echo $memo['id']; ?>" class="btn-sm">
                                    <?php echo !$memo['is_read'] ? 'Read & Acknowledge' : 'View'; ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>