<section class="card">
    <div class="toolbar"><h2>Global Search</h2><span class="badge">V13</span></div>
    <form method="get" action="index.php" class="toolbar" style="margin-top:12px;gap:10px;align-items:end;">
        <input type="hidden" name="route" value="search/index">
        <div style="flex:1;min-width:260px;"><label>Search</label><input type="text" name="q" value="<?= e($q) ?>" placeholder="Contacts, messages, templates, campaigns, accounts, users"></div>
        <button class="btn primary" type="submit">Search</button>
    </form>
</section>

<?php if ($q !== ''): ?>
<section class="cards four">
    <div class="card stat"><h3>Contacts</h3><p><?= count($contacts) ?></p></div>
    <div class="card stat"><h3>Messages</h3><p><?= count($messages) ?></p></div>
    <div class="card stat"><h3>Templates</h3><p><?= count($templates) ?></p></div>
    <div class="card stat"><h3>Campaigns</h3><p><?= count($campaigns) ?></p></div>
</section>

<section class="grid two">
    <div class="card"><h2>Contacts</h2><table><thead><tr><th>Name</th><th>Company</th><th>Email</th></tr></thead><tbody><?php foreach ($contacts as $row): ?><tr><td><a href="index.php?route=contacts/show&id=<?= (int)$row['id'] ?>"><?= e(trim(($row['first_name'] ?? '') . ' ' . ($row['last_name'] ?? ''))) ?></a></td><td><?= e($row['company'] ?? '') ?></td><td><?= e($row['email'] ?? '') ?></td></tr><?php endforeach; ?></tbody></table></div>
    <div class="card"><h2>Messages</h2><table><thead><tr><th>When</th><th>Channel</th><th>Address</th></tr></thead><tbody><?php foreach ($messages as $row): ?><tr><td><?= e($row['created_at']) ?></td><td><?= e(strtoupper($row['channel'])) ?></td><td><?= e($row['to_address'] ?: $row['from_address']) ?></td></tr><?php endforeach; ?></tbody></table></div>
</section>
<section class="grid two">
    <div class="card"><h2>Templates</h2><table><thead><tr><th>Name</th><th>Type</th><th>Subject</th></tr></thead><tbody><?php foreach ($templates as $row): ?><tr><td><?= e($row['name']) ?></td><td><?= e($row['type']) ?></td><td><?= e($row['subject'] ?? '') ?></td></tr><?php endforeach; ?></tbody></table></div>
    <div class="card"><h2>Campaigns</h2><table><thead><tr><th>Name</th><th>Channel</th><th>Status</th></tr></thead><tbody><?php foreach ($campaigns as $row): ?><tr><td><?= e($row['name']) ?></td><td><?= e($row['channel']) ?></td><td><?= e($row['status']) ?></td></tr><?php endforeach; ?></tbody></table></div>
</section>
<section class="grid two">
    <div class="card"><h2>Accounts</h2><table><thead><tr><th>Name</th><th>Industry</th></tr></thead><tbody><?php foreach ($accounts as $row): ?><tr><td><?= e($row['name']) ?></td><td><?= e($row['industry'] ?? '') ?></td></tr><?php endforeach; ?></tbody></table></div>
    <div class="card"><h2>Users</h2><table><thead><tr><th>Name</th><th>Email</th><th>Role</th></tr></thead><tbody><?php foreach ($users as $row): ?><tr><td><?= e($row['name']) ?></td><td><?= e($row['email']) ?></td><td><?= e($row['role']) ?></td></tr><?php endforeach; ?></tbody></table></div>
</section>
<?php endif; ?>
