<?php
require_once __DIR__ . '/includes/auth.php';
require_admin();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_check() && !empty($_POST['lead_id'])) {
    $stmt = db()->prepare('UPDATE leads SET status = ? WHERE id = ?');
    $stmt->execute([$_POST['status'], (int)$_POST['lead_id']]);
    header('Location: /admin/leads.php' . (!empty($_GET['type']) ? '?type=' . urlencode($_GET['type']) : ''));
    exit;
}

$type = $_GET['type'] ?? '';
$where = $type ? 'WHERE type = ?' : '';
$params = $type ? [$type] : [];
$stmt = db()->prepare("SELECT l.*, p.title AS property_title FROM leads l LEFT JOIN properties p ON p.id = l.property_id $where ORDER BY l.created_at DESC");
$stmt->execute($params);
$leads = $stmt->fetchAll();

$pageTitle = 'Leads Inbox';
include __DIR__ . '/includes/layout-header.php';
?>
<div class="flex gap-2 mb-6">
  <a href="/admin/leads.php" class="filter-chip <?= $type === '' ? 'active' : '' ?>">All</a>
  <a href="/admin/leads.php?type=contact" class="filter-chip <?= $type === 'contact' ? 'active' : '' ?>">Contact</a>
  <a href="/admin/leads.php?type=tour" class="filter-chip <?= $type === 'tour' ? 'active' : '' ?>">Tour Requests</a>
  <a href="/admin/leads.php?type=valuation" class="filter-chip <?= $type === 'valuation' ? 'active' : '' ?>">Valuations</a>
  <a href="/admin/leads.php?type=list_property" class="filter-chip <?= $type === 'list_property' ? 'active' : '' ?>">Sell Submissions</a>
</div>
<div class="admin-card" style="padding:0;overflow-x:auto;">
  <table class="admin-table">
    <thead><tr><th>Type</th><th>Name</th><th>Contact</th><th>Property</th><th>Message</th><th>Date</th><th>Status</th></tr></thead>
    <tbody>
      <?php foreach ($leads as $l): ?>
        <tr>
          <td><span class="badge badge-outline"><?= e($l['type']) ?></span></td>
          <td><?= e($l['name']) ?></td>
          <td><?= e($l['email']) ?><br><span class="text-muted"><?= e($l['phone']) ?></span></td>
          <td><?= e($l['property_title'] ?? '—') ?><?= $l['preferred_date'] ? '<br><span class="text-xs text-muted">Preferred: ' . e($l['preferred_date']) . '</span>' : '' ?></td>
          <td style="max-width:280px;white-space:pre-wrap;"><?= e(truncate($l['message'] ?? '', 160)) ?></td>
          <td><?= date('M j, Y', strtotime($l['created_at'])) ?></td>
          <td>
            <form method="post" class="flex gap-2">
              <input type="hidden" name="csrf" value="<?= e(csrf_token()) ?>">
              <input type="hidden" name="lead_id" value="<?= (int)$l['id'] ?>">
              <select name="status" onchange="this.form.submit()">
                <option value="new" <?= $l['status'] === 'new' ? 'selected' : '' ?>>New</option>
                <option value="contacted" <?= $l['status'] === 'contacted' ? 'selected' : '' ?>>Contacted</option>
                <option value="closed" <?= $l['status'] === 'closed' ? 'selected' : '' ?>>Closed</option>
              </select>
            </form>
          </td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($leads)): ?><tr><td colspan="7" class="text-center text-muted">No leads found.</td></tr><?php endif; ?>
    </tbody>
  </table>
</div>
<?php include __DIR__ . '/includes/layout-footer.php'; ?>
