<?php
$u=current_user();
$search=trim(request('q',''));
$results=[];
if($search!==''){
    $like='%'.$search.'%';
    $stmt=db()->prepare("SELECT u.*,c.name company_name FROM users u LEFT JOIN companies c ON c.id=u.company_id WHERE u.id<>? AND u.status='active' AND (u.public_id LIKE ? OR u.name LIKE ? OR COALESCE(u.username,'') LIKE ? OR u.email LIKE ?) ORDER BY u.name LIMIT 30");
    $stmt->execute([$u['id'],$like,$like,$like,$like]);$results=$stmt->fetchAll();
}
$stmt=db()->prepare("SELECT cb.id contact_book_id,u.*,c.name company_name FROM contact_book cb JOIN users u ON u.id=cb.contact_user_id LEFT JOIN companies c ON c.id=u.company_id WHERE cb.owner_user_id=? ORDER BY u.name");
$stmt->execute([$u['id']]);$contacts=$stmt->fetchAll();
?>
<section class="page-intro"><div><span class="mini-label">CONTATOS LINKEXPRESS</span><h2>Salve pessoas e empresas pelo ID</h2><p>Use o ID LinkExpress para guardar motoristas, prestadores, clientes e profissionais na sua conta.</p></div></section>
<div class="grid-2">
<section class="panel"><div class="panel-header"><div><h3>Pesquisar usuário</h3><p>Digite o ID, nome ou e-mail.</p></div></div><form class="filter-bar" method="get"><input type="hidden" name="page" value="contacts"><div class="form-group"><label>ID ou nome</label><input class="input" name="q" value="<?= e($search) ?>" placeholder="Ex.: LXU-AB7K-9Q2M"></div><button class="btn btn-primary">Pesquisar</button></form>
<?php if($search!=='' && !$results):?><div class="empty-state"><strong>Nenhum usuário encontrado</strong>Confira o ID e tente novamente.</div><?php endif;?>
<div class="cards-list"><?php foreach($results as $person):?><article class="item-card"><div class="item-body"><div class="profile-hero"><?= avatar_html($person) ?><div><h3><?= e(user_public_name($person)) ?></h3><small><?= e(public_user_id($person)) ?> • <?= e(role_label($person['role'])) ?></small></div></div><p><?= e($person['company_name']?:'Usuário independente') ?> • <?= e($person['city']?:'Cidade não informada') ?></p><div class="actions"><form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="save_contact"><input type="hidden" name="return_page" value="contacts"><input type="hidden" name="contact_user_id" value="<?= (int)$person['id'] ?>"><button class="btn btn-primary btn-sm">Salvar contato</button></form><a class="btn btn-secondary btn-sm" href="<?= e(url('perfil.php?id='.(int)$person['id'])) ?>">Ver perfil</a><a class="btn btn-danger btn-sm" href="<?= e(url('dashboard.php?page=reports&reported_id='.(int)$person['id'])) ?>">Denunciar</a><?php if($person['phone']):?><a class="btn btn-accent btn-sm" target="_blank" rel="noopener" href="<?= e(whatsapp_url($person['phone'],'Olá, encontrei seu perfil na LinkExpress.')) ?>">WhatsApp</a><?php endif;?><?php if($person['telegram']):?><a class="btn btn-secondary btn-sm" target="_blank" href="<?= e(telegram_url($person['telegram'])) ?>">Telegram</a><?php endif;?></div></div></article><?php endforeach;?></div></section>
<section class="panel"><div class="panel-header"><div><h3>Meus contatos</h3><p><?= count($contacts) ?> contato(s) salvo(s).</p></div></div><?php if(!$contacts):?><div class="empty-state"><strong>Nenhum contato salvo</strong>Pesquise pelo ID LinkExpress.</div><?php endif;?><div class="activity-list"><?php foreach($contacts as $person):?><div class="activity"><div><?= avatar_html($person) ?></div><div style="flex:1"><p><strong><?= e(user_public_name($person)) ?></strong></p><small><?= e(public_user_id($person)) ?> • <?= e(role_label($person['role'])) ?><?php if($person['company_name']):?> • <?= e($person['company_name']) ?><?php endif;?></small><div class="actions" style="margin-top:8px"><a class="btn btn-secondary btn-sm" href="<?= e(url('perfil.php?id='.(int)$person['id'])) ?>">Ver perfil</a><a class="btn btn-danger btn-sm" href="<?= e(url('dashboard.php?page=reports&reported_id='.(int)$person['id'])) ?>">Denunciar</a><?php if($person['phone']):?><a class="btn btn-accent btn-sm" target="_blank" rel="noopener" href="<?= e(whatsapp_url($person['phone'],'Olá, falando pelos contatos da LinkExpress.')) ?>">WhatsApp</a><?php endif;?><?php if($person['telegram']):?><a class="btn btn-secondary btn-sm" target="_blank" href="<?= e(telegram_url($person['telegram'])) ?>">Telegram</a><?php endif;?><form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="delete_contact"><input type="hidden" name="return_page" value="contacts"><input type="hidden" name="id" value="<?= (int)$person['contact_book_id'] ?>"><button class="btn btn-danger btn-sm" data-confirm="Remover este contato?">Remover</button></form></div></div></div><?php endforeach;?></div></section>
</div>
