<?php
require_once __DIR__ . '/../config.php'; // adjust path to your DB connection
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/layout.php';  // includes auth + header + sidebar + topbar

$adminId = $_SESSION['admin_user_id'];

// Fetch inbox messages (received by logged-in admin)
$stmt = $pdo->prepare("
    SELECT m.*, u.username AS sender_name
    FROM messages m
    JOIN admin_users u ON m.sender_id = u.id
    WHERE m.recipient_id = ?
    ORDER BY m.sent_at DESC
");
$stmt->execute([$adminId]);
$inboxMessages = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch outbox messages (sent by logged-in admin)
$stmt = $pdo->prepare("
    SELECT m.*, u.username AS recipient_name
    FROM messages m
    JOIN admin_users u ON m.recipient_id = u.id
    WHERE m.sender_id = ?
    ORDER BY m.sent_at DESC
");
$stmt->execute([$adminId]);
$outboxMessages = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h1>Messages</h1>

<div class="d-flex justify-content-end mb-3">
    <a href="new-message.php" class="btn btn-success">New Message</a>
</div>

<!-- Bootstrap Nav Tabs -->
<ul class="nav nav-tabs" id="messagesTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="inbox-tab" data-bs-toggle="tab" data-bs-target="#inbox" type="button" role="tab" aria-controls="inbox" aria-selected="true">Inbox</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="outbox-tab" data-bs-toggle="tab" data-bs-target="#outbox" type="button" role="tab" aria-controls="outbox" aria-selected="false" style='color: #000'>Outbox</button>
  </li>
</ul>

<div class="tab-content mt-3" id="messagesTabContent">
  <!-- Inbox Tab -->
  <div class="tab-pane fade show active" id="inbox" role="tabpanel" aria-labelledby="inbox-tab">
    <table class="table table-striped table-bordered">
      <thead>
        <tr>
          <th>From</th>
          <th>Subject</th>
          <th>Received</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($inboxMessages)): ?>
          <tr><td colspan="4">No messages found.</td></tr>
        <?php else: ?>
          <?php foreach ($inboxMessages as $msg): ?>
            <tr
                onclick="window.location.href='message.php?id=<?= (int)$msg['id'] ?>'"
                style="cursor:pointer; <?= !$msg['is_read'] ? 'font-weight: bold; background-color: #e9f7fe;' : '' ?>"
                title="View Message #<?= (int)$msg['id'] ?>"
            >
              <td><?= htmlspecialchars($msg['sender_name']) ?></td>
              <td><?= htmlspecialchars($msg['subject']) ?></td>
              <td><?= htmlspecialchars($msg['sent_at']) ?></td>
              <td><?= $msg['is_read'] ? 'Read' : 'Unread' ?></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>

  <!-- Outbox Tab -->
  <div class="tab-pane fade" id="outbox" role="tabpanel" aria-labelledby="outbox-tab">
    <table class="table table-striped table-bordered">
      <thead>
        <tr>
          <th>To</th>
          <th>Subject</th>
          <th>Sent</th>
          <th>Read</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($outboxMessages)): ?>
          <tr><td colspan="3">No sent messages found.</td></tr>
        <?php else: ?>
          <?php foreach ($outboxMessages as $msg): ?>
            <tr
                onclick="window.location.href='message.php?id=<?= (int)$msg['id'] ?>'"
                style="cursor:pointer;"
                title="View Message #<?= (int)$msg['id'] ?>"
            >
              <td><?= htmlspecialchars($msg['recipient_name']) ?></td>
              <td><?= htmlspecialchars($msg['subject']) ?></td>
              <td><?= htmlspecialchars($msg['sent_at']) ?></td>
              <td><?= $msg['is_read'] ? 'Read' : 'Unread' ?></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- Make sure Bootstrap JS is loaded (usually in your footer.php) -->
