<?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'];
$memoId = isset($_GET['id']) ? intval($_GET['id']) : 0;

$memos = readJson('memos');
$currentMemo = null;
foreach ($memos as $memo) {
    if ($memo['id'] == $memoId && in_array($userId, $memo['assigned_to'])) {
        $currentMemo = $memo;
        break;
    }
}

if (!$currentMemo) {
    header("Location: view_memos.php");
    exit();
}

$message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['mark_read'])) {
    if (markMemoAsRead($memoId, $userId)) {
        $message = "✅ Memo marked as read and understood!";
        // Refresh memo data
        $memos = readJson('memos');
        foreach ($memos as $memo) {
            if ($memo['id'] == $memoId) {
                $currentMemo = $memo;
                break;
            }
        }
    }
}

$isRead = isMemoRead($memoId, $userId);
?>
<?php include('../includes/header.php'); ?>
<style>
    .memo-detail-card {
        background: white;
        border-radius: 24px;
        padding: 24px;
        margin-bottom: 24px;
        border: 1px solid #e2cfb3;
        box-shadow: 0 4px 12px rgba(0,0,0,0.03);
    }
    .memo-header {
        border-bottom: 2px solid #c9a87c;
        padding-bottom: 12px;
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
    }
    .memo-title {
        font-size: 1.5rem;
        font-weight: bold;
        color: #6b1d2f;
        font-family: 'Georgia', serif;
    }
    .read-badge {
        display: inline-block;
        padding: 6px 14px;
        border-radius: 40px;
        font-size: 0.8rem;
        font-weight: bold;
    }
    .badge-read {
        background: #8b6b42;
        color: #faf3e8;
    }
    .badge-unread {
        background: #a03e4b;
        color: white;
    }
    .memo-meta {
        color: #8b7355;
        font-size: 0.8rem;
        margin-bottom: 20px;
    }
    .memo-content {
        background: #fefcf8;
        padding: 20px;
        border-radius: 16px;
        line-height: 1.6;
        border: 1px solid #f0e2d0;
        margin-bottom: 20px;
    }
    .pdf-container {
        background: #faf3e8;
        padding: 20px;
        border-radius: 20px;
        margin: 20px 0;
        border: 1px solid #e2cfb3;
    }
    .pdf-iframe {
        width: 100%;
        height: 500px;
        border: none;
        border-radius: 12px;
    }
    .acknowledge-btn {
        background: #6b1d2f;
        color: #f9e4c2;
        padding: 14px 24px;
        font-size: 1rem;
        font-weight: bold;
        border: none;
        border-radius: 60px;
        width: 100%;
        cursor: pointer;
        transition: 0.2s;
        font-family: 'Georgia', serif;
    }
    .acknowledge-btn:hover {
        background: #8b2635;
    }
    .info-box {
        background: #e3d9cd;
        border-left: 6px solid #c9a87c;
        padding: 15px;
        border-radius: 16px;
        text-align: center;
    }
    @media (max-width: 640px) {
        .memo-detail-card { padding: 16px; }
        .memo-title { font-size: 1.2rem; }
        .pdf-iframe { height: 300px; }
    }
</style>

<div class="sidebar">
    <h3>✈️ Crew Menu</h3>
    <a href="dashboard.php" class="btn">🏠 Dashboard</a>
    <a href="view_memos.php" class="btn">📝 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">
    <?php if($message): ?>
        <div class="alert alert-success"><?php echo $message; ?></div>
    <?php endif; ?>

    <div class="memo-detail-card">
        <div class="memo-header">
            <div class="memo-title"><?php echo htmlspecialchars($currentMemo['title']); ?></div>
            <div>
                <?php if($isRead): ?>
                    <span class="read-badge badge-read">✓ Read & Understood</span>
                <?php else: ?>
                    <span class="read-badge badge-unread">● Unread</span>
                <?php endif; ?>
            </div>
        </div>

        <div class="memo-meta">
            📅 Issued: <?php echo date('F j, Y g:i A', strtotime($currentMemo['created_at'])); ?>
            <?php if(isset($currentMemo['has_pdf']) && $currentMemo['has_pdf']): ?>
                &nbsp;|&nbsp; 📎 PDF attachment included
            <?php endif; ?>
        </div>

        <div class="memo-content">
            <?php echo nl2br(htmlspecialchars($currentMemo['content'])); ?>
        </div>

        <?php if(isset($currentMemo['pdf_file']) && !empty($currentMemo['pdf_file']) && file_exists('../' . $currentMemo['pdf_file'])): ?>
            <div class="pdf-container">
                <h4>📄 Attached Document</h4>
                <iframe class="pdf-iframe" src="../<?php echo $currentMemo['pdf_file']; ?>#toolbar=0"></iframe>
                <div style="text-align: center; margin-top: 12px;">
                    <a href="../<?php echo $currentMemo['pdf_file']; ?>" target="_blank" class="btn btn-primary">📎 Open PDF in new tab</a>
                </div>
            </div>
        <?php endif; ?>

        <?php if(!$isRead): ?>
            <form method="POST" onsubmit="return confirm('By confirming, you acknowledge that you have read and fully understood this memo. This action will be recorded for compliance.');">
                <button type="submit" name="mark_read" class="acknowledge-btn">✓ I Have Read and Understood This Memo</button>
            </form>
        <?php else: ?>
            <div class="info-box">
                <strong>✓ Acknowledgement Recorded</strong><br>
                You acknowledged this memo on <?php echo date('F j, Y g:i A', strtotime($currentMemo['read_at'][$userId] ?? 'now')); ?>
            </div>
        <?php endif; ?>

        <div style="margin-top: 20px; text-align: center;">
            <a href="view_memos.php" class="btn btn-secondary">← Back to My Memos</a>
        </div>
    </div>
</div>

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