<?php
// This file is included by pages that may have already emitted output, so only
// start a session if it is still safe to do so.
if (session_status() === PHP_SESSION_NONE && !headers_sent()) {
    session_start();
}
require_once('common.php');

$open_count = 0;
try {
    $stmt = $pdo->prepare("SELECT COUNT(*) AS open_count FROM sm_editions WHERE is_open = 1");
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $open_count = (int)($row['open_count'] ?? 0);
} catch (PDOException $e) {
    error_log('poets-menu: ' . $e->getMessage());
}

// Display name preference: pen_name if set, else first + last.
$display_name = '';
if (isset($cPoet) && is_array($cPoet)) {
    $pen = trim((string)($cPoet['pen_name'] ?? ''));
    if ($pen !== '') {
        $display_name = $pen;
    } elseif (!empty($cPoet['author_name']) || !empty($cPoet['author_lastname'])) {
        $display_name = trim(($cPoet['author_name'] ?? '') . ' ' . ($cPoet['author_lastname'] ?? ''));
    }
}
?>
<nav class="site-nav site-nav--member" aria-label="Member navigation">
  <div class="site-nav__row site-nav__row--journal">
    <div class="site-nav__inner">
      <a href="main.php">Home</a>
      <a href="poets-guidelines.php" target="_blank" rel="noopener">Guidelines</a>
      <?php if ($open_count > 0): ?>
        <a href="new-submission.php">Submit</a>
      <?php else: ?>
        <span class="nav-text site-nav__closed">Submissions closed</span>
      <?php endif; ?>
      <a href="login.php?logout" class="nav-logout">Logout</a>
    </div>
  </div>
  <div class="site-nav__row site-nav__row--personal">
    <div class="site-nav__inner">
      <span class="site-nav__label">Your Account</span>
      <a href="my-submissions.php">My Submissions</a>
      <a href="my-bio.php">My Biography</a>
      <a href="my-profile.php">My Profile</a>
      <?php if ($display_name !== ''): ?>
        <span class="nav-status">Signed in as <b><?= htmlspecialchars($display_name, ENT_QUOTES, 'UTF-8') ?></b></span>
      <?php endif; ?>
    </div>
  </div>
</nav>
