/*
Theme Name: Astra Child
Theme URI: https://wpastra.com/
Template: astra
Author: Brainstorm Force
Author URI: https://wpastra.com/about/?utm_source=theme_preview&utm_medium=author_link&utm_campaign=astra_theme
Description: The Astra WordPress theme is lightning-fast and highly customizable. It has over 1 million downloads and the only theme in the world with 6,000+ five-star reviews! It’s ideal for professional web designers, solopreneurs, small businesses, eCommerce, membership sites and any type of website. It offers special features and templates so it works perfectly with all page builders like Spectra, Elementor, Beaver Builder, etc. Fast performance, clean code, mobile-first design and schema markup are all built-in, making the theme exceptionally SEO-friendly. It’s fully compatible with WooCommerce, SureCart and other eCommerce plugins and comes with lots of store-friendly features and templates. Astra also provides expert support for free users. A dedicated team of fully trained WordPress experts are on hand to help with every aspect of the theme. Try the live demo of Astra: https://zipwp.org/themes/astra/
Tags: custom-menu,custom-logo,entertainment,one-column,two-columns,left-sidebar,e-commerce,right-sidebar,custom-colors,editor-style,featured-images,full-width-template,microformats,post-formats,rtl-language-support,theme-options,threaded-comments,translation-ready,blog
Version: 4.11.13.1762002868
Updated: 2025-11-01 13:14:28
add_action( 'wp_enqueue_scripts', function() {
*/
<?php
<?php
/* Template Name: IFMOSA Search Results */
get_header();
?>

<div id="ifmosa-results-wrapper" class="ifmosa-search-container">
  <h2 class="ifmosa-heading">IFMOSA Go — Search Results</h2>
  <div id="ifs-results" class="ifs-results-area">
    <div class="ifs-no-results">Loading results…</div>
  </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', async function () {
  const params = new URLSearchParams(window.location.search);
  const keywordRaw = (params.get('keyword') || '').trim();
  const keyword = keywordRaw.toLowerCase();
  const region = (params.get('region') || '').trim();

  const CPTS = ['go_gulf','go_asia','go_canada','go_europe','go_uk','go_oceania','go_usa'];
  const resultsDiv = document.getElementById('ifs-results');

  async function fetchForType(type) {
    try {
      const base = '/wp-json/wp/v2/';
      const query = new URLSearchParams({ per_page: 100, _embed: 1 });
      const res = await fetch(base + encodeURIComponent(type) + '?' + query.toString());
      if (!res.ok) return [];
      const json = await res.json();
      return json.map(p => ({ ...p, __post_type: type }));
    } catch (e) {
      console.error('fetch error', e);
      return [];
    }
  }

  function uniqById(arr) {
    const map = new Map();
    arr.forEach(i => map.set(i.id, i));
    return Array.from(map.values());
  }

  function renderPosts(posts) {
    if (!posts || posts.length === 0) {
      resultsDiv.innerHTML = '<div class="ifs-no-results">No results found.</div>';
      return;
    }
    posts.sort((a,b) => new Date(b.date) - new Date(a.date));
    const html = ['<div class="ifs-job-grid">'];
    posts.forEach(p => {
      const title = p.title?.rendered || 'Untitled';
      const excerpt = (p.excerpt?.rendered || '').replace(/(<([^>]+)>)/gi, "");
      const link = p.link || '#';
      const img = p._embedded?.['wp:featuredmedia']?.[0]?.source_url || '';
      html.push(`
        <div class="ifs-job-card">
          <a href="${link}">
            ${img ? `<img src="${img}" alt="${title}">` : ''}
            <h3>${title}</h3>
            <p>${excerpt}</p>
          </a>
        </div>
      `);
    });
    html.push('</div>');
    resultsDiv.innerHTML = html.join('');
  }

  const types = region ? [region] : CPTS.slice();
  resultsDiv.innerHTML = '<div class="ifs-no-results">Searching…</div>';

  const all = (await Promise.all(types.map(fetchForType))).flat();
  const filtered = keyword ? all.filter(p => {
    const t = (p.title?.rendered || '').toLowerCase();
    const e = (p.excerpt?.rendered || '').toLowerCase();
    return t.includes(keyword) || e.includes(keyword);
  }) : all;

  renderPosts(uniqById(filtered));
});
</script>

<?php get_footer(); ?>