<?php
$u=current_user();
$companies=db()->query("SELECT c.*,ou.public_id owner_public_id,COALESCE((SELECT ROUND(AVG(r.rating),1) FROM reviews r WHERE r.target_type='company' AND r.target_id=c.id AND r.status='published'),0) rating FROM companies c LEFT JOIN users ou ON ou.id=c.owner_user_id WHERE c.status='active' ORDER BY c.premium DESC,c.verified DESC,c.name")->fetchAll();
$segments=db()->query("SELECT slug,name,icon FROM segments WHERE status='active' ORDER BY sort_order,name")->fetchAll();
$segmentIcons=[];foreach($segments as $segment)$segmentIcons[strtolower((string)$segment['slug'])]=$segment['icon']?:'🏢';
$mapCompanies=[];foreach($companies as $c){$sector=strtolower((string)($c['sector']?:$c['type']));$mapCompanies[]=['id'=>(int)$c['id'],'name'=>$c['name'],'sector'=>$sector,'sector_label'=>ucfirst(str_replace('_',' ',$sector)),'icon'=>$segmentIcons[$sector]??'🏢','address'=>trim($c['address'].' '.$c['address_number'].' '.$c['city'].'/'.$c['state']),'lat'=>$c['latitude'],'lng'=>$c['longitude'],'phone'=>$c['phone'],'logo'=>media_url($c['logo'])];}
?>
<section class="page-intro"><div><span class="mini-label">PERTO DE VOCÊ</span><h2>Mapa de empresas</h2><p>Selecione um segmento para mostrar somente empresas daquele segmento no mapa. Ao pesquisar por nome ou ID, o mapa mostra somente o resultado pesquisado.</p></div><button class="btn btn-primary" type="button" data-locate-user data-auto-locate>📍 Atualizar minha localização</button></section>
<section class="panel map-filter-panel"><div class="form-grid"><div class="form-group"><label>Pesquisar empresa ou ID</label><input class="input" data-company-search placeholder="Nome da empresa ou LXU-..."></div><div class="form-group"><label>Segmento</label><select data-company-sector><option value="">Todos os segmentos</option><?php foreach($segments as $segment):?><option value="<?= e(strtolower($segment['slug'])) ?>"><?= e(($segment['icon']?:'🏢').' '.$segment['name']) ?></option><?php endforeach;?></select></div></div><div class="notice" data-nearby-status>Localizando empresas próximas...</div></section>
<div class="map-layout" data-user-city="<?= e(strtolower((string)($u['city']??''))) ?>" data-user-lat="<?= e($u['home_lat']??'') ?>" data-user-lng="<?= e($u['home_lng']??'') ?>">
<section class="panel map-panel"><div id="companyMap" class="company-map" data-map-companies='<?= e(json_encode($mapCompanies,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)) ?>'></div><p class="map-help">👤 O boneco representa sua localização. Os outros ícones identificam o segmento de cada empresa.</p></section>
<section class="company-near-list" data-company-list><?php foreach($companies as $c):$sector=strtolower((string)($c['sector']?:$c['type']));$sectorIcon=$segmentIcons[$sector]??'🏢';?><article class="near-card" data-company-card data-company-id="<?= (int)$c['id'] ?>" data-name="<?= e(strtolower($c['name'].' '.$c['description'].' '.$c['city'].' '.$c['address'].' '.($c['owner_public_id']??'').' #'.$c['id'])) ?>" data-sector="<?= e($sector) ?>" data-city="<?= e(strtolower((string)$c['city'])) ?>" data-lat="<?= e($c['latitude']) ?>" data-lng="<?= e($c['longitude']) ?>"><div class="near-logo"><?php if($c['logo']):?><img src="<?= e(media_url($c['logo'])) ?>" alt=""><?php else:?><?= e($sectorIcon) ?><?php endif;?></div><div><div class="near-title"><strong><?= e($c['name']) ?></strong><?php if($c['verified']):?><span>✓ Verificada</span><?php endif;?></div><small><?= e($sectorIcon.' '.ucfirst(str_replace('_',' ',$c['sector']?:$c['type']))) ?> • <?= e($c['city'].'/'.$c['state']) ?></small><?php if($c['owner_public_id']):?><small style="display:block">ID: <?= e($c['owner_public_id']) ?></small><?php endif;?><p><?= e(short_text($c['address']?:$c['description'],90)) ?></p><div class="near-actions"><a class="btn btn-secondary btn-sm" target="_blank" rel="noopener" href="<?= e(google_directions_link($c['address'].' '.$c['city'].' '.$c['state'],$c['latitude'],$c['longitude'])) ?>">Como chegar</a><?php if($c['phone']):?><a class="btn btn-accent btn-sm" target="_blank" rel="noopener" href="<?= e(whatsapp_url($c['phone'],'Olá, encontrei sua empresa no LinkExpress.')) ?>">WhatsApp</a><?php endif;?><span class="distance-label" data-distance-label></span></div></div></article><?php endforeach;?></section>
</div>
