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

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

$employees = getAllEmployees();
$today = date('Y-m-d');
$ninetyDays = date('Y-m-d', strtotime('+90 days'));

function isExpiringWithin90($date) {
    if (empty($date)) return false;
    $today = date('Y-m-d');
    $ninety = date('Y-m-d', strtotime('+90 days'));
    return ($date >= $today && $date <= $ninety);
}
?>
<?php include('../includes/header.php'); ?>
<style>
    @media print {
        .sidebar, .navbar, .menu-toggle, .print-hide, .btn, .propeller-loader, .user-info-block, .logout-btn {
            display: none !important;
        }
        .content {
            width: 100% !important;
            margin: 0 !important;
            padding: 0 !important;
            background: white;
        }
        .employee-table {
            border-collapse: collapse;
            width: 100%;
            font-size: 10pt;
        }
        .employee-table th, .employee-table td {
            border: 1px solid #000 !important;
            padding: 6px;
        }
        .expiry-warning {
            color: black !important;
            font-weight: bold;
            background: none !important;
        }
        body {
            background: white;
        }
    }
    .employee-table {
        width: 100%;
        border-collapse: collapse;
        background: white;
        border-radius: 16px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    }
    .employee-table th {
        background: #6b1d2f;
        color: #f9e4c2;
        padding: 12px 10px;
        text-align: left;
        font-weight: 600;
        border: 1px solid #8b2635;
    }
    .employee-table td {
        padding: 10px;
        border: 1px solid #e2cfb3;
        vertical-align: top;
    }
    .employee-table tr:nth-child(even) {
        background-color: #fefcf8;
    }
    .employee-table tr:hover {
        background-color: #f8efe5;
    }
    .expiry-warning {
        background-color: #f8d7da;
        color: #721c24;
        font-weight: bold;
    }
    .status-active {
        color: #27ae60;
        font-weight: bold;
    }
    .status-inactive {
        color: #e74c3c;
        font-weight: bold;
    }
    .endorsement-badge {
        display: inline-block;
        background: #c9a87c;
        color: #2c1e12;
        padding: 2px 6px;
        border-radius: 12px;
        font-size: 0.7rem;
        margin: 2px;
    }
    .print-btn {
        background: #6b1d2f;
        color: #f9e4c2;
        border: none;
        padding: 8px 16px;
        border-radius: 30px;
        cursor: pointer;
        margin-bottom: 20px;
    }
    .doc-link {
        font-size: 1.2rem;
        text-decoration: none;
    }
    @media (max-width: 768px) {
        .employee-table th, .employee-table td {
            padding: 6px;
            font-size: 0.75rem;
        }
    }
</style>
<div class="sidebar">
    <h3>📋 Admin Console</h3>
    <a href="dashboard.php" class="btn">📊 Dashboard</a>
    <a href="add_user.php" class="btn">👤 Add Crew</a>
    <a href="manage_users.php" class="btn">📋 Crew List</a>
    <a href="employee_list.php" class="btn active">📋 Full Employee Data</a>
    <a href="edit_user.php" class="btn">✏️ Edit Crew</a>
    <a href="manage_endorsements.php" class="btn">✈️ Endorsements</a>
    <a href="add_memo.php" class="btn">📝 New Memo</a>
    <a href="add_quiz.php" class="btn">📚 New Quiz</a>
    <a href="inbox.php" class="btn">📥 Safety Reports</a>
    <a href="chat.php" class="btn">💬 Group Chat</a>
    <a href="tools.php" class="btn">🔧 Tools Management</a>
    <a href="upload_manual.php" class="btn">📘 Upload Manual</a>
</div>
<div class="content">
    <div class="flex-between" style="align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 10px;">
        <h2>📋 Complete Employee Data</h2>
        <button class="print-btn" onclick="window.print()">🖨️ Print / Save as PDF</button>
    </div>
    <div class="table-responsive">
        <table class="employee-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Full Name</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>AME License</th>
                    <th>AME Expiry</th>
                    <th>RAIC #</th>
                    <th>RAIC Expiry</th>
                    <th>DA #</th>
                    <th>DA Expiry</th>
                    <th>AMO Stamp #</th>
                    <th>AMO Stamp Expiry</th>
                    <th>Endorsements</th>
                    <th>Status</th>
                    <th>Documents</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($employees as $emp): 
                    $isActive = isset($emp['is_active']) ? $emp['is_active'] : true;
                    $hasDocs = !empty(getUserDocuments($emp['id']));
                ?>
                <tr>
                    <td>#<?php echo $emp['id']; ?></td>
                    <td><?php echo htmlspecialchars($emp['full_name']); ?></td>
                    <td><?php echo htmlspecialchars($emp['username']); ?></td>
                    <td><?php echo htmlspecialchars($emp['email']); ?></td>
                    <td><?php echo htmlspecialchars($emp['phone']); ?></td>
                    <td><?php echo htmlspecialchars($emp['ame_license'] ?: '-'); ?></td>
                    <td class="<?php echo isExpiringWithin90($emp['ame_expiry']) ? 'expiry-warning' : ''; ?>"><?php echo $emp['ame_expiry'] ?: '-'; ?></td>
                    <td><?php echo htmlspecialchars($emp['raic_number'] ?: '-'); ?></td>
                    <td class="<?php echo isExpiringWithin90($emp['raic_expiry']) ? 'expiry-warning' : ''; ?>"><?php echo $emp['raic_expiry'] ?: '-'; ?></td>
                    <td><?php echo htmlspecialchars($emp['da_number'] ?: '-'); ?></td>
                    <td class="<?php echo isExpiringWithin90($emp['da_expiry']) ? 'expiry-warning' : ''; ?>"><?php echo $emp['da_expiry'] ?: '-'; ?></td>
                    <td><?php echo htmlspecialchars($emp['amo_stamp_number'] ?: '-'); ?></td>
                    <td class="<?php echo isExpiringWithin90($emp['amo_stamp_expiry']) ? 'expiry-warning' : ''; ?>"><?php echo $emp['amo_stamp_expiry'] ?: '-'; ?></td>
                    <td>
                        <?php 
                        $endorsements = getEmployeeEndorsements($emp['id']);
                        if (!empty($endorsements)) {
                            foreach ($endorsements as $code) {
                                echo '<span class="endorsement-badge">✈️ ' . $code . '</span> ';
                            }
                        } else {
                            echo '-';
                        }
                        ?>
                    </td>
                    <td class="<?php echo $isActive ? 'status-active' : 'status-inactive'; ?>"><?php echo $isActive ? 'Active' : 'Inactive'; ?></td>
                    <td>
                        <a href="edit_user.php?user_id=<?php echo $emp['id']; ?>" class="doc-link" title="Manage Documents">📄</a>
                        <?php if($hasDocs): ?>
                            <span style="font-size:0.7rem; color:#27ae60;">(<?php echo count(getUserDocuments($emp['id'])); ?>)</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>