<?php
$u=current_user();
$q=db()->prepare('SELECT * FROM favorite_addresses WHERE user_id=? ORDER BY label COLLATE NOCASE,address COLLATE NOCASE');
$q->execute([$u['id']]);
$addresses=$q->fetchAll();
?>
<section class="page-intro">
  <div><span class="mini-label">MOBILIDADE</span><h2>Endereços Favoritos</h2><p>Cadastre destinos usados com frequência para preencher suas corridas rapidamente.</p></div>
  <div class="actions"><a class="btn btn-secondary" href="dashboard.php?page=rides">← Corridas</a><button class="btn btn-primary" type="button" data-modal-open="#favoriteAddressModal">+ Cadastrar endereço</button></div>
</section>

<section class="panel">
  <div class="panel-header"><div><h3>Meus destinos salvos</h3><p>Você pode editar ou apagar qualquer endereço.</p></div></div>
  <div class="cards-list">
    <?php if(!$addresses):?><div class="empty-state"><strong>Nenhum endereço favorito</strong>Cadastre casa, trabalho, escola ou qualquer destino frequente.</div><?php endif;?>
    <?php foreach($addresses as $a):?>
      <article class="item-card"><div class="item-body">
        <div class="item-meta"><span>📍 Destino favorito</span></div>
        <h3><?= e($a['label']) ?></h3>
        <p><?= e($a['address']) ?></p>
        <?php if($a['latitude']!==null&&$a['longitude']!==null):?><small>Localização salva no mapa</small><?php endif;?>
        <div class="actions" style="margin-top:14px">
          <button class="btn btn-secondary btn-sm" type="button" data-modal-open="#editAddress<?= (int)$a['id'] ?>">Editar</button>
          <a class="btn btn-secondary btn-sm" target="_blank" rel="noopener" href="<?= e(maps_link($a['address'],$a['latitude'],$a['longitude'])) ?>">Google Maps</a>
          <form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="delete_favorite_address"><input type="hidden" name="return_page" value="favorite_addresses"><input type="hidden" name="id" value="<?= (int)$a['id'] ?>"><button class="btn btn-danger btn-sm" data-confirm="Apagar este endereço favorito?">Apagar</button></form>
        </div>
      </div></article>
      <div class="modal" id="editAddress<?= (int)$a['id'] ?>"><div class="modal-card"><div class="modal-head"><h2>Editar endereço</h2><button class="close-btn" type="button" data-modal-close>×</button></div>
        <form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="save_favorite_address"><input type="hidden" name="return_page" value="favorite_addresses"><input type="hidden" name="id" value="<?= (int)$a['id'] ?>"><input type="hidden" name="latitude" value="<?= e($a['latitude']) ?>"><input type="hidden" name="longitude" value="<?= e($a['longitude']) ?>">
          <div class="form-grid"><div class="form-group"><label>Nome do destino</label><input class="input" name="label" value="<?= e($a['label']) ?>" required></div><div class="form-group full"><label>Endereço</label><input class="input" name="address" value="<?= e($a['address']) ?>" data-address-autocomplete data-lat-field="latitude" data-lng-field="longitude" required></div></div>
          <div class="form-actions"><button class="btn btn-primary">Salvar alterações</button></div>
        </form>
      </div></div>
    <?php endforeach;?>
  </div>
</section>

<div class="modal" id="favoriteAddressModal"><div class="modal-card"><div class="modal-head"><h2>Novo endereço favorito</h2><button class="close-btn" type="button" data-modal-close>×</button></div>
  <form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="save_favorite_address"><input type="hidden" name="return_page" value="favorite_addresses"><input type="hidden" name="latitude"><input type="hidden" name="longitude">
    <div class="form-grid">
      <div class="form-group"><label>Nome do destino</label><input class="input" name="label" placeholder="Ex.: Casa, Trabalho, Escola" required></div>
      <div class="form-group full"><label>Endereço</label><div class="inline-filter"><input class="input" name="address" data-address-autocomplete data-lat-field="latitude" data-lng-field="longitude" required><button class="btn btn-secondary" type="button" data-geolocate-form data-address-field="address" data-lat-field="latitude" data-lng-field="longitude">📍 Localizar</button></div></div>
    </div>
    <div class="form-actions"><button class="btn btn-primary">Cadastrar endereço</button></div>
  </form>
</div></div>
