<?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);
$endorsements = getEmployeeEndorsements($userId);
$userDocuments = getUserDocuments($userId);
?>
<?php include('../includes/header.php'); ?>
<style>
    .profile-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);
    }
    .profile-grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 16px;
    }
    @media (min-width: 640px) {
        .profile-grid {
            grid-template-columns: 1fr 1fr;
        }
    }
    .info-row {
        display: flex;
        flex-wrap: wrap;
        align-items: baseline;
        padding: 8px 0;
        border-bottom: 1px solid #f0e2d0;
    }
    .info-label {
        font-weight: 700;
        width: 140px;
        color: #6b1d2f;
        font-size: 0.85rem;
    }
    .info-value {
        flex: 1;
        color: #2c1e12;
        word-break: break-word;
        font-size: 0.95rem;
    }
    .endorsements-container {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        margin-top: 8px;
    }
    .endorsement-badge {
        background: #c9a87c;
        color: #2c1e12;
        padding: 4px 12px;
        border-radius: 50px;
        font-size: 0.75rem;
        font-weight: bold;
    }
    .section-title {
        font-size: 1.2rem;
        font-weight: bold;
        color: #6b1d2f;
        border-left: 4px solid #c9a87c;
        padding-left: 12px;
        margin-bottom: 16px;
        font-family: 'Georgia', serif;
    }
    .doc-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 12px;
        background: #f8efe5;
        border-radius: 12px;
        margin-bottom: 8px;
    }
    .doc-item a {
        color: #6b1d2f;
        text-decoration: none;
    }
    .doc-item a:hover {
        text-decoration: underline;
    }
</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="chat.php" class="btn">💬 Group Chat</a>
    <a href="profile.php" class="btn active">👤 My Profile</a>
    <a href="view_manual.php" class="btn">📘 Maintenance Manual</a>
</div>
<div class="content">
    <h2>👤 My Profile</h2>
    
    <!-- Personal Information -->
    <div class="profile-card">
        <div class="section-title">Personal Information</div>
        <div class="profile-grid">
            <div class="info-row"><span class="info-label">Full Name:</span><span class="info-value"><?php echo htmlspecialchars($user['full_name']); ?></span></div>
            <div class="info-row"><span class="info-label">Username:</span><span class="info-value"><?php echo htmlspecialchars($user['username']); ?></span></div>
            <div class="info-row"><span class="info-label">Email:</span><span class="info-value"><?php echo htmlspecialchars($user['email']); ?></span></div>
            <div class="info-row"><span class="info-label">Phone:</span><span class="info-value"><?php echo htmlspecialchars($user['phone']); ?></span></div>
            <div class="info-row"><span class="info-label">Role:</span><span class="info-value"><?php echo ucfirst($user['role']); ?></span></div>
            <div class="info-row"><span class="info-label">Member Since:</span><span class="info-value"><?php echo date('F j, Y', strtotime($user['created_at'])); ?></span></div>
        </div>
    </div>
    
    <!-- Aircraft Endorsements -->
    <div class="profile-card">
        <div class="section-title">✈️ Aircraft Endorsements</div>
        <?php if(!empty($endorsements)): ?>
            <div class="endorsements-container">
                <?php foreach($endorsements as $code): ?>
                    <span class="endorsement-badge">✈️ <?php echo $code; ?></span>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <p class="info-value" style="color:#8b7355;">No endorsements added yet.</p>
        <?php endif; ?>
    </div>
    
    <!-- Aviation Credentials -->
    <div class="profile-card">
        <div class="section-title">✈️ Aviation Credentials</div>
        <div class="profile-grid">
            <div class="info-row"><span class="info-label">AME License:</span><span class="info-value"><?php echo htmlspecialchars($user['ame_license'] ?: 'Not provided'); ?></span></div>
            <div class="info-row"><span class="info-label">AME Expiry:</span><span class="info-value"><?php echo $user['ame_expiry'] ?: 'Not set'; ?></span></div>
            <div class="info-row"><span class="info-label">RAIC Number:</span><span class="info-value"><?php echo htmlspecialchars($user['raic_number'] ?: 'Not provided'); ?></span></div>
            <div class="info-row"><span class="info-label">RAIC Expiry:</span><span class="info-value"><?php echo $user['raic_expiry'] ?: 'Not set'; ?></span></div>
            <div class="info-row"><span class="info-label">DA Number:</span><span class="info-value"><?php echo htmlspecialchars($user['da_number'] ?: 'Not provided'); ?></span></div>
            <div class="info-row"><span class="info-label">DA Expiry:</span><span class="info-value"><?php echo $user['da_expiry'] ?: 'Not set'; ?></span></div>
            <div class="info-row"><span class="info-label">AMO Stamp Number:</span><span class="info-value" style="color:#6b1d2f; font-weight:500;"><?php echo htmlspecialchars($user['amo_stamp_number'] ?: 'Not provided'); ?></span></div>
            <div class="info-row"><span class="info-label">AMO Stamp Expiry:</span><span class="info-value"><?php echo $user['amo_stamp_expiry'] ?: 'Not set'; ?></span></div>
        </div>
    </div>
    
    <!-- My Documents -->
    <div class="profile-card">
        <div class="section-title">📄 My Documents (Resume & Certificates)</div>
        <?php if(empty($userDocuments)): ?>
            <p>No documents have been uploaded yet.</p>
        <?php else: ?>
            <?php foreach($userDocuments as $doc): ?>
                <div class="doc-item">
                    <div>
                        <a href="<?php echo $doc['path']; ?>" target="_blank">
                            <?php echo $doc['type'] == 'resume' ? '📄 Resume / CV' : '📜 Certificate / License'; ?>
                            (<?php echo round($doc['size'] / 1024, 1); ?> KB)
                        </a>
                        <small> – uploaded <?php echo date('M j, Y', strtotime($doc['uploaded_at'])); ?></small>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>
<div class="clearfix"></div>
<?php include('../includes/footer.php'); ?>