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

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

$currentManual = readJson('maintenance_manual');
$manualsHistory = readJson('maintenance_manuals_history');
?>
<?php include('../includes/header.php'); ?>
<style>
    .manual-container {
        background: white;
        border-radius: 24px;
        padding: 24px;
        border: 1px solid #e2cfb3;
        box-shadow: 0 4px 12px rgba(0,0,0,0.03);
    }
    .version-badge {
        background: #c9a87c;
        color: #2c1e12;
        padding: 4px 12px;
        border-radius: 50px;
        font-size: 0.75rem;
        font-weight: bold;
        display: inline-block;
    }
    .manual-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;
    }
    .manual-title {
        font-size: 1.4rem;
        font-weight: bold;
        color: #6b1d2f;
        font-family: 'Georgia', serif;
    }
    .manual-meta {
        color: #8b7355;
        font-size: 0.8rem;
        margin-bottom: 20px;
    }
    .pdf-viewer {
        background: #faf3e8;
        padding: 20px;
        border-radius: 20px;
        margin: 20px 0;
        border: 1px solid #e2cfb3;
    }
    .pdf-iframe {
        width: 100%;
        height: 600px;
        border: none;
        border-radius: 12px;
    }
    .version-history {
        margin-top: 24px;
        background: #fefcf8;
        border-radius: 20px;
        padding: 16px;
        border: 1px solid #f0e2d0;
    }
    .history-table {
        width: 100%;
        border-collapse: collapse;
    }
    .history-table th {
        text-align: left;
        padding: 10px;
        background: #f8efe5;
        color: #6b1d2f;
        border-bottom: 1px solid #e2cfb3;
    }
    .history-table td {
        padding: 10px;
        border-bottom: 1px solid #f0e2d0;
    }
    .btn-sm {
        padding: 4px 12px;
        font-size: 0.7rem;
        background: #6b1d2f;
        color: #f9e4c2;
        border-radius: 40px;
        text-decoration: none;
    }
    .btn-sm:hover {
        background: #8b2635;
    }
    @media (max-width: 640px) {
        .manual-container { padding: 16px; }
        .pdf-iframe { height: 350px; }
        .manual-title { font-size: 1.2rem; }
    }
</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">📋 My Reports</a>
    <a href="profile.php" class="btn">👤 Profile</a>
    <a href="view_manual.php" class="btn active">📘 Maintenance Manual</a>
</div>

<div class="content">
    <h2>📘 Maintenance Policy Manual</h2>

    <?php if(empty($currentManual)): ?>
        <div class="manual-container" style="text-align: center;">
            <p>No maintenance policy manual has been uploaded yet.</p>
            <p>Please check back later.</p>
        </div>
    <?php else: ?>
        <div class="manual-container">
            <div class="manual-header">
                <div class="manual-title">
                    Current Version <span class="version-badge">v<?php echo $currentManual['version']; ?></span>
                </div>
                <a href="../<?php echo $currentManual['filepath']; ?>" class="btn btn-primary" target="_blank">📖 Open Full PDF</a>
            </div>

            <div class="manual-meta">
                <strong>Last updated:</strong> <?php echo date('F j, Y g:i A', strtotime($currentManual['uploaded_at'])); ?>
                <?php if(!empty($currentManual['description'])): ?>
                    <br><strong>Notes:</strong> <?php echo nl2br(htmlspecialchars($currentManual['description'])); ?>
                <?php endif; ?>
            </div>

            <!-- PDF inline viewer -->
            <div class="pdf-viewer">
                <iframe class="pdf-iframe" src="../<?php echo $currentManual['filepath']; ?>#toolbar=0" title="Maintenance Manual"></iframe>
            </div>
        </div>

        <!-- Previous versions -->
        <?php if(!empty($manualsHistory) && count($manualsHistory) > 1): ?>
            <div class="version-history">
                <h3>📜 Previous Versions</h3>
                <div class="table-responsive">
                    <table class="history-table">
                        <thead>
                            <tr><th>Version</th><th>Upload Date</th><th>Description</th><th>Action</th></tr>
                        </thead>
                        <tbody>
                            <?php 
                            $history = array_reverse($manualsHistory);
                            foreach($history as $manual):
                                if($manual['version'] == $currentManual['version']) continue;
                            ?>
                            <tr>
                                <td><strong>v<?php echo $manual['version']; ?></strong></td>
                                <td><?php echo date('M j, Y', strtotime($manual['uploaded_at'])); ?></td>
                                <td><?php echo htmlspecialchars(substr($manual['description'] ?? '', 0, 60)) . (strlen($manual['description'] ?? '') > 60 ? '…' : ''); ?></td>
                                <td><a href="../<?php echo $manual['filepath']; ?>" target="_blank" class="btn-sm">View</a></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>

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