<?php
// header.php - Zentraler Header mit Einladungs-Check und dem KORREKTEN, vollständigen Menü

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

$current_username = isset($_SESSION['username']) ? $_SESSION['username'] : 'Gast';
$pending_invitation = null;

if (isset($_SESSION['user_id'])) {
    require_once 'db_connect.php'; 
    
    $stmt_invite_check = $conn->prepare(
        "SELECT hi.id, h.name AS household_name, u.username AS inviter_name 
         FROM household_invitations hi 
         JOIN households h ON hi.household_id = h.id 
         JOIN users u ON hi.inviter_user_id = u.id 
         WHERE hi.invited_user_id = ? AND hi.status = 'pending'"
    );
    if ($stmt_invite_check) {
        $stmt_invite_check->bind_param("i", $_SESSION['user_id']);
        $stmt_invite_check->execute();
        $result = $stmt_invite_check->get_result();
        if ($result->num_rows > 0) {
            $pending_invitation = $result->fetch_assoc();
        }
        $stmt_invite_check->close();
    }
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($page_title) ? htmlspecialchars($page_title) . ' - ' : ''; ?>Trekking Packliste</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@400;600;700&family=Poppins:wght@500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/style.css?v=<?php echo time(); ?>">
    <link rel="icon" type="image/png" href="logo.png">
</head>
<body>
    <div class="page-wrapper">
        <div class="sidebar">
            <a class="navbar-brand" href="index.php">
                <img src="./logo.png" alt="Logo" class="navbar-brand-logo">
                <h4 class="mt-2">Packliste</h4>
            </a>
            <ul class="nav flex-column">
                <li class="nav-item"><a class="nav-link" href="index.php"><i class="fas fa-home fa-fw"></i>Start</a></li>
                <li class="nav-item"><a class="nav-link" href="articles.php"><i class="fas fa-box fa-fw"></i>Artikel</a></li>
                <li class="nav-item"><a class="nav-link" href="backpacks.php"><i class="fas fa-hiking fa-fw"></i>Rucksäcke</a></li>
                <li class="nav-item"><a class="nav-link" href="packing_lists.php"><i class="fas fa-clipboard-list fa-fw"></i>Packlisten</a></li>
                <li class="nav-item"><a class="nav-link" href="storage_locations.php"><i class="fas fa-archive fa-fw"></i>Lagerorte</a></li>
                <li class="nav-item"><a class="nav-link" href="categories.php"><i class="fas fa-tags fa-fw"></i>Kategorien</a></li>
                <li class="nav-item"><a class="nav-link" href="manufacturers.php"><i class="fas fa-industry fa-fw"></i>Hersteller</a></li>
                <li class="nav-item"><a class="nav-link" href="household.php"><i class="fas fa-users-cog fa-fw"></i>Haushalt</a></li>
                <li class="nav-item"><a class="nav-link" href="user_profile.php"><i class="fas fa-user-cog fa-fw"></i>Profil</a></li>
                <li class="nav-item"><a class="nav-link" href="help.php"><i class="fas fa-question-circle fa-fw"></i>Hilfe</a></li>
            </ul>
            <div class="username-display">
                <div>Hallo, <strong><?php echo htmlspecialchars($current_username); ?></strong></div>
                 <div class="mt-3">
                    <a class="btn btn-sm btn-outline-light w-100" href="logout.php"><i class="fas fa-sign-out-alt me-2"></i>Abmelden</a>
                </div>
            </div>
        </div>
        <div class="main-content">

<?php if ($pending_invitation): ?>
    <div class="modal fade" id="invitationModal" tabindex="-1" aria-labelledby="invitationModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="invitationModalLabel"><i class="fas fa-users me-2"></i>Einladung zu einem Haushalt</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <p>
                Du wurdest von <strong><?php echo htmlspecialchars($pending_invitation['inviter_name']); ?></strong> 
                eingeladen, dem Haushalt "<strong><?php echo htmlspecialchars($pending_invitation['household_name']); ?></strong>" beizutreten.
            </p>
            <p>Möchtest du die Einladung annehmen?</p>
          </div>
          <div class="modal-footer">
            <form action="household_actions.php" method="post" class="d-inline">
                <input type="hidden" name="action" value="respond_invitation">
                <input type="hidden" name="invitation_id" value="<?php echo $pending_invitation['id']; ?>">
                <button type="submit" name="response" value="decline" class="btn btn-secondary">Ablehnen</button>
            </form>
            <form action="household_actions.php" method="post" class="d-inline">
                <input type="hidden" name="action" value="respond_invitation">
                <input type="hidden" name="invitation_id" value="<?php echo $pending_invitation['id']; ?>">
                <button type="submit" name="response" value="accept" class="btn btn-primary">Annehmen</button>
            </form>
          </div>
        </div>
      </div>
    </div>
<?php endif; ?>
