<?php
require_once __DIR__.'/includes/functions.php';
$u=require_user();
mj263_schema();
mj_trust_bundle_ensure_schema();

$pageTitle='Saved Listings & Companies';

$savedListings = [];
$savedCompanies = [];
try {
    $savedListings = all_rows('SELECT l.*, c.currency_code, c.flag_emoji, ci.name city, cat.name category
        FROM mj_saved_listings s
        INNER JOIN mj_listings l ON l.id=s.listing_id
        LEFT JOIN mj_countries c ON c.id=l.country_id
        LEFT JOIN mj_cities ci ON ci.id=l.city_id
        LEFT JOIN mj_categories cat ON cat.id=l.category_id
        WHERE s.user_id=?
        ORDER BY s.created_at DESC
        LIMIT 80', [(int)$u['id']]);
} catch (Throwable $e) {}
try {
    $savedCompanies = all_rows('SELECT co.*, c.name country, c.flag_emoji, ci.name city
        FROM mj_saved_companies s
        INNER JOIN mj_companies co ON co.id=s.company_id
        LEFT JOIN mj_countries c ON c.id=co.country_id
        LEFT JOIN mj_cities ci ON ci.id=co.city_id
        WHERE s.user_id=?
        ORDER BY s.created_at DESC
        LIMIT 80', [(int)$u['id']]);
} catch (Throwable $e) {}

include __DIR__.'/includes/header.php';
?>
<div class="container py-5">
  <div class="sidebar-layout">
    <?php include __DIR__.'/includes/account-sidebar.php'; ?>
    <section>
      <div class="mj-page-hero rounded-4 mb-4">
        <div class="container-fluid">
          <span class="mj-badge mb-2"><i class="bi bi-heart-fill"></i>Saved Items</span>
          <h1 class="fw-bold mb-1">Saved Listings & Companies</h1>
          <p class="text-muted mb-0">Keep useful listings and trusted companies here so you can contact them later.</p>
        </div>
      </div>

      <?php show_flash(); ?>

      <div class="row g-3 mb-4">
        <div class="col-md-6">
          <div class="mj-account-tile h-100">
            <i class="bi bi-box-seam"></i>
            <h5 class="fw-bold mt-3"><?=e(count($savedListings))?> saved listings</h5>
            <p class="text-muted mb-0">Quickly return to listings you want to compare, contact or make an offer.</p>
          </div>
        </div>
        <div class="col-md-6">
          <div class="mj-account-tile h-100">
            <i class="bi bi-building-check"></i>
            <h5 class="fw-bold mt-3"><?=e(count($savedCompanies))?> saved companies</h5>
            <p class="text-muted mb-0">Follow trusted companies and review their services, profile and active listings.</p>
          </div>
        </div>
      </div>

      <div class="mj-section-title mt-4">
        <div>
          <h2>Saved listings</h2>
          <p class="text-muted mb-0">Listings you saved for later.</p>
        </div>
        <a class="btn btn-outline-primary rounded-pill" href="<?=e(base_url('browse.php'))?>">Browse more</a>
      </div>

      <?php if($savedListings): ?>
        <div class="row g-4">
          <?php foreach($savedListings as $r): ?>
            <div class="col-md-6 col-xl-4"><?php include __DIR__.'/partials/listing-card.php'; ?></div>
          <?php endforeach; ?>
        </div>
      <?php else: ?>
        <div class="mj-card p-4 text-center">
          <h5 class="fw-bold">No saved listings yet</h5>
          <p class="text-muted">When you find a useful listing, click Save Listing to keep it here.</p>
          <a class="btn btn-primary rounded-pill" href="<?=e(base_url('browse.php'))?>">Browse Listings</a>
        </div>
      <?php endif; ?>

      <div class="mj-section-title mt-5">
        <div>
          <h2>Saved companies</h2>
          <p class="text-muted mb-0">Companies you saved for future contact or comparison.</p>
        </div>
        <a class="btn btn-outline-primary rounded-pill" href="<?=e(base_url('companies.php'))?>">Browse companies</a>
      </div>

      <?php if($savedCompanies): ?>
        <div class="row g-4">
          <?php foreach($savedCompanies as $r): ?>
            <div class="col-md-6 col-xl-4"><?php include __DIR__.'/partials/company-card.php'; ?></div>
          <?php endforeach; ?>
        </div>
      <?php else: ?>
        <div class="mj-card p-4 text-center">
          <h5 class="fw-bold">No saved companies yet</h5>
          <p class="text-muted">Save company profiles to find their services, listings and contact options faster.</p>
          <a class="btn btn-primary rounded-pill" href="<?=e(base_url('companies.php'))?>">Explore Companies</a>
        </div>
      <?php endif; ?>
    </section>
  </div>
</div>
<?php include __DIR__.'/includes/footer.php'; ?>
