<?php
$img = $r['image_path'] ?? $r['cover_image'] ?? '';
$imgUrl = $img ? base_url($img) : asset('images/empty-state.svg');
$status = strtolower((string)($r['status'] ?? 'active'));
$mode = function_exists('mj263_status_public_mode') ? mj263_status_public_mode($status) : 'active';
$url = base_url('listing.php?id='.(int)$r['id']);
$views=(int)($r['views']??0); $eng=(int)($r['engagement_count']??0); $offers=(int)($r['offer_count']??0); $shares=(int)($r['share_count']??0);
?>
<div class="mj-card h-100 listing-card <?= $status==='sold'?'mj-status-sold':'' ?>" data-aos="fade-up">
  <div class="position-relative">
    <a href="<?= e($url) ?>" class="d-block">
      <img class="mj-card-img" src="<?= e($imgUrl) ?>" alt="<?= e($r['title'] ?? 'Listing') ?>">
    </a>
    <button type="button" class="btn btn-light btn-sm position-absolute bottom-0 start-0 m-2 rounded-pill" data-gallery-src="<?= e($imgUrl) ?>" data-gallery-title="<?= e($r['title'] ?? 'Listing') ?>" data-listing-id="<?= (int)$r['id'] ?>" data-mj-track="image_view"><i class="bi bi-arrows-fullscreen"></i></button>
    <button type="button" class="btn btn-light btn-sm position-absolute bottom-0 end-0 m-2 rounded-pill" data-native-share data-url="<?= e($url) ?>" data-title="<?= e($r['title'] ?? 'Listing') ?>" data-listing-id="<?= (int)$r['id'] ?>" data-mj-track="share" data-share-type="native"><i class="bi bi-share"></i></button>
    <div class="position-absolute top-0 start-0 m-2 d-flex gap-1 flex-wrap"><?= function_exists('mj263_public_badges') ? mj263_public_badges($r) : '' ?></div>
  </div>
  <div class="p-3">
    <div class="d-flex justify-content-between gap-2 mb-2"><span class="mj-badge"><i class="bi bi-tag"></i><?= e($r['condition_label'] ?? $r['listing_type'] ?? 'Listing') ?></span><?php if($mode==='sold'): ?><span class="mj-badge dark">Sold</span><?php elseif($mode==='reserved'): ?><span class="mj-badge warn">Reserved</span><?php else: ?><span class="mj-badge good">Available</span><?php endif; ?></div>
    <h5 class="fw-bold mb-1"><a href="<?= e($url) ?>" class="text-decoration-none text-dark"><?= e($r['title'] ?? 'Untitled') ?></a></h5>
    <p class="text-muted small mb-2"><i class="bi bi-geo-alt me-1"></i><?= e(($r['city'] ?? '') ?: ($r['country'] ?? 'Global')) ?> <?= !empty($r['created_at']) ? '• '.e(local_datetime($r['created_at'], $r['timezone'] ?? 'UTC')) : '' ?></p>
    <div class="mj-listing-stats mb-2"><span><i class="bi bi-eye"></i><?= e($views) ?></span><span><i class="bi bi-fire"></i><?= e($eng) ?> engaged</span><span><i class="bi bi-cash-coin"></i><?= e($offers) ?> offers</span><?php if($shares): ?><span><i class="bi bi-share"></i><?= e($shares) ?></span><?php endif; ?></div>
    <div class="d-flex align-items-center justify-content-between"><span class="mj-listing-price"><?= money($r['price'] ?? 0, $r['currency_code'] ?? 'USD') ?></span><?php if($mode==='sold'): ?><button class="btn btn-sm btn-dark" disabled>Sold</button><?php elseif($mode==='reserved'): ?><button class="btn btn-sm btn-warning" disabled>Reserved</button><?php else: ?><a class="btn btn-sm btn-outline-primary" href="<?= base_url('chat.php?listing_id='.(int)$r['id']) ?>" data-listing-id="<?= (int)$r['id'] ?>" data-mj-track="chat_click"><i class="bi bi-chat-dots"></i></a><?php endif; ?></div>
  </div>
</div>
