<?php
/**
 * Template: search/results.php
 *
 * Product search results page.
 * Reuses the same product card component as the shop grid.
 *
 * Available variables:
 *   $query    — sanitized search string
 *   $products — array of product rows
 *   $params   — pagination info (page, total_items, total_pages)
 *   $lang     — current language code
 *
 * Override: {theme}/titancart/search/results.php
 *
 * @package TitanCart
 */

defined( 'ABSPATH' ) || exit;

$query    = $query    ?? '';
$products = $products ?? [];
$params   = $params   ?? [ 'page' => 1, 'total_items' => 0, 'total_pages' => 1 ];
$currency = apply_filters( 'titancart_currency_symbol', '$' );

get_header();
?>

<?php
$_tc_is_theme = defined( 'TITANCART_THEME_ACTIVE' );
$_banner_title = $query
    /* translators: %s: search query entered by the visitor. */
    ? sprintf( __( 'Search results for "%s"', 'titancart-lite' ), $query )
    : __( 'Search Products', 'titancart-lite' );
?>

<?php if ( $_tc_is_theme ) :
  $_b_cls = 'tc-shop__banner tc-shop__banner--theme';
  $_b_sty = ''; $_b_ov = '';
  $_b_img      = get_theme_mod( 'tct_shop_banner_image' );
  $_b_img_dark = get_theme_mod( 'tct_shop_banner_image_dark' );
  if ( $_b_img ) {
      $_b_url      = wp_get_attachment_image_url( (int) $_b_img, 'full' );
      $_b_url_dark = $_b_img_dark ? wp_get_attachment_image_url( (int) $_b_img_dark, 'full' ) : '';
      if ( $_b_url ) {
          $_b_pos = get_theme_mod( 'tct_shop_banner_position', 'center center' );
          $_b_ht  = get_theme_mod( 'tct_shop_banner_height', 280 );
          $_b_ov  = get_theme_mod( 'tct_shop_banner_overlay', 'rgba(0,0,0,0.45)' );
          $_b_sty = sprintf( 'background-image:url(%s);background-size:cover;background-position:%s;height:%dpx;',
              esc_url( $_b_url ), esc_attr( $_b_pos ), (int) $_b_ht );
          $_b_cls .= ' tc-shop__banner--has-image';
          // Pattern #97: Optional dark-mode banner image — when set in the
          // Customizer, store the URL on a CSS custom property. shop.css
          // contains the global rule that swaps `background-image` to this
          // variable when [data-theme="dark"] is active. Mirrors the wiring
          // in archive.php / single.php / dashboard.php / cart.php /
          // checkout.php / order-received.php (S151).
          if ( $_b_url_dark ) {
              $_b_sty .= sprintf( '--tc-banner-img-dark:url(%s);', esc_url( $_b_url_dark ) );
              $_b_cls .= ' tc-shop__banner--has-dark-image';
          }
      }
  }
?>
<!-- ═══ Full-Width Page Header Banner (Theme) ═══ -->
<section class="<?php echo esc_attr( $_b_cls ); ?>" style="<?php echo esc_attr( $_b_sty ); ?>">
  <?php if ( $_b_ov ) : ?><div class="tc-shop__banner-overlay" style="background:<?php echo esc_attr( $_b_ov ); ?>;position:absolute;top:0;left:0;right:0;bottom:0;"></div><?php endif; ?>
  <div class="tc-shop__banner-inner">
    <h2 class="tc-shop__banner-title"><?php echo esc_html( $_banner_title ); ?></h2>
    <nav class="tc-breadcrumb tc-breadcrumb--centered">
      <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a>
      <i class="bi bi-chevron-right"></i>
      <a href="<?php echo esc_url( titancart_shop_url() ); ?>">Shop</a>
      <i class="bi bi-chevron-right"></i>
      <span class="tc-breadcrumb__current">Search</span>
    </nav>
  </div>
</section>
<?php endif; ?>

<div class="tc-search" id="tc-search">

    <?php if ( ! $_tc_is_theme ) : ?>
    <!-- Breadcrumb banner (Plugin) -->
    <div class="tc-search__banner">
        <div class="tc-search__banner-inner">
            <h1 class="tc-search__banner-title"><?php echo esc_html( $_banner_title ); ?></h1>
            <nav class="tc-search__breadcrumb">
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a>
                <span class="tc-sep">›</span>
                <a href="<?php echo esc_url( titancart_shop_url() ); ?>">Shop</a>
                <span class="tc-sep">›</span>
                <span>Search</span>
            </nav>
        </div>
    </div>
    <?php endif; ?>

    <div class="tc-search__content">

        <!-- Inline search form (SR5/SR14: always present, pre-filled with current query) -->
        <form class="tc-search__inline-form" action="<?php echo esc_url( home_url( '/search/' ) ); ?>" method="get">
            <div class="tc-search__input-wrap">
                <i class="bi bi-search"></i>
                <input type="text" name="q" class="tc-search__input"
                       value="<?php echo esc_attr( $query ); ?>"
                       placeholder="<?php esc_attr_e( 'Search products...', 'titancart-lite' ); ?>"
                       autocomplete="off" autofocus />
                <button type="submit" class="tc-btn tc-btn--primary tc-btn--sm"><?php esc_html_e( 'Search', 'titancart-lite' ); ?></button>
            </div>
        </form>

        <?php if ( $query ) : ?>

            <!-- Results count -->
            <p class="tc-search__count">
                <?php
                $total = (int) $params['total_items'];
                if ( $total > 0 ) {
                    printf(
                        /* translators: %d: number of products found. */
                        esc_html( _n( '%d product found', '%d products found', $total, 'titancart-lite' ) ),
                        (int) $total
                    );
                } else {
                    esc_html_e( 'No products found.', 'titancart-lite' );
                }
                ?>
            </p>

            <?php if ( ! empty( $products ) ) : ?>

                <!-- Product grid (reuses same card component as shop) -->
                <div class="tc-product-grid" id="tc-search-grid">
                    <?php foreach ( $products as $product ) :
                        \TitanCart\Storefront\TemplateLoader::get_template( 'product/card.php', compact( 'product' ) );
                    endforeach; ?>
                </div>

                <!-- Pagination -->
                <?php if ( (int) $params['total_pages'] > 1 ) :
                    $current = (int) $params['page'];
                    $total_p = (int) $params['total_pages'];
                    $base_url = home_url( '/search/?q=' . urlencode( $query ) );
                ?>
                <nav class="tc-pagination" id="tc-search-pagination">
                    <?php if ( $current > 1 ) : ?>
                        <a href="<?php echo esc_url( $base_url . '&tc_page=' . ( $current - 1 ) ); ?>" class="tc-pagination__item">
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    <?php endif; ?>

                    <?php for ( $i = 1; $i <= $total_p; $i++ ) :
                        if ( $total_p > 7 && $i > 3 && $i < $total_p - 2 && abs( $i - $current ) > 1 ) {
                            if ( $i === 4 || $i === $total_p - 3 ) echo '<span class="tc-pagination__dots">…</span>';
                            continue;
                        }
                        $cls = $i === $current ? 'tc-pagination__item tc-pagination__item--active' : 'tc-pagination__item';
                    ?>
                        <a href="<?php echo esc_url( $base_url . '&tc_page=' . $i ); ?>" class="<?php echo esc_attr( $cls ); ?>"><?php echo (int) $i; ?></a>
                    <?php endfor; ?>

                    <?php if ( $current < $total_p ) : ?>
                        <a href="<?php echo esc_url( $base_url . '&tc_page=' . ( $current + 1 ) ); ?>" class="tc-pagination__item">
                            <i class="bi bi-chevron-right"></i>
                        </a>
                    <?php endif; ?>
                </nav>
                <?php endif; ?>

            <?php else : ?>

                <!-- Empty state -->
                <div class="tc-search__empty">
                    <i class="bi bi-search"></i>
                    <h3><?php esc_html_e( 'No results found', 'titancart-lite' ); ?></h3>
                    <?php /* translators: %s: search query entered by the visitor. */ ?>
                    <p><?php printf( esc_html__( 'We couldn\'t find any products matching "%s". Try a different search term or browse our categories.', 'titancart-lite' ), esc_html( $query ) ); ?></p>
                    <a href="<?php echo esc_url( titancart_shop_url() ); ?>" class="tc-btn tc-btn--primary">Browse All Products</a>
                </div>

            <?php endif; ?>

            <?php
            // Pages matching search (when search_in_pages setting is ON).
            $pages = $pages ?? [];
            if ( ! empty( $pages ) ) : ?>
                <div class="tc-search__pages" style="margin-top:32px">
                    <h3 style="font-size:16px;font-weight:600;margin-bottom:16px;padding-bottom:8px;border-bottom:1px solid #e5e5e5">
                        <?php esc_html_e( 'Pages', 'titancart-lite' ); ?>
                    </h3>
                    <?php foreach ( $pages as $pg ) : ?>
                        <div style="margin-bottom:16px">
                            <a href="<?php echo esc_url( $pg['url'] ); ?>" style="font-size:15px;font-weight:500;color:#2563EB;text-decoration:none">
                                <?php echo esc_html( $pg['title'] ); ?>
                            </a>
                            <?php if ( ! empty( $pg['excerpt'] ) ) : ?>
                                <p style="font-size:13px;color:#737373;margin-top:4px;line-height:1.5"><?php echo esc_html( $pg['excerpt'] ); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

        <?php else : ?>

            <!-- No query yet (SR1) -->
            <div class="tc-search__empty">
                <i class="bi bi-search"></i>
                <h3><?php esc_html_e( 'What are you looking for?', 'titancart-lite' ); ?></h3>
                <p><?php esc_html_e( 'Enter a product name, category, or keyword above to find what you need.', 'titancart-lite' ); ?></p>
            </div>

        <?php endif; ?>

    </div>
</div>

<?php get_footer(); ?>
