<?php
/**
 * Shop Hero Section Template Part
 *
 *
 * @package shopbar
 */

defined( 'ABSPATH' ) || exit;

// Resolve the current archive title and description.
$term             = get_queried_object();
$term_description = isset( $term->description ) ? $term->description : '';

if ( is_product_category() || is_product_tag() ) {
	$page_title = single_term_title( '', false );
	$page_desc  = $term_description;
} elseif ( is_shop() ) {
	$page_title = get_the_title( get_option( 'woocommerce_shop_page_id' ) );
	if ( empty( $page_title ) ) {
		$page_title = __( 'Shop', 'shopbar' );
	}
	$page_desc = '';
} else {
	$page_title = __( 'Products', 'shopbar' );
	$page_desc  = '';
}

// Customizer overrides for the product archive header.
$archive_title    = get_theme_mod( 'shopbar_archive_page_title', '' );
$archive_subtitle = get_theme_mod( 'shopbar_archive_page_subtitle', '' );

if ( ! empty( $archive_title ) ) {
	$page_title = $archive_title;
}
if ( ! empty( $archive_subtitle ) ) {
	$page_desc = $archive_subtitle;
}

$total_products = (int) wc_get_loop_prop( 'total', 0 );

$shop_url  = get_permalink( wc_get_page_id( 'shop' ) );
$deals_url = add_query_arg( 'on_sale', '1', $shop_url );

// Promo banner options.
$promo_enable   = (bool) get_theme_mod( 'shopbar_archive_promo_enable', true );
$promo_eyebrow  = get_theme_mod( 'shopbar_archive_promo_eyebrow', '' );
$promo_headline = get_theme_mod( 'shopbar_archive_promo_headline', '' );
$promo_subtitle = get_theme_mod( 'shopbar_archive_promo_subtitle', '' );
$promo_cta_text = get_theme_mod( 'shopbar_archive_promo_cta_text', '' );
$promo_cta_url  = get_theme_mod( 'shopbar_archive_promo_cta_url', '' );

// Fallbacks when a field is left empty.
if ( empty( $promo_eyebrow ) ) {
	$promo_eyebrow = __( 'Limited Time', 'shopbar' );
}
if ( empty( $promo_headline ) ) {
	$promo_headline = __( 'Up to 40% off top picks', 'shopbar' );
}
if ( empty( $promo_subtitle ) ) {
	$promo_subtitle = __( 'Hand-selected favourites, freshly marked down — while they last.', 'shopbar' );
}
if ( empty( $promo_cta_text ) ) {
	$promo_cta_text = __( 'Shop the Deals', 'shopbar' );
}
if ( empty( $promo_cta_url ) ) {
	$promo_cta_url = $deals_url;
}

$grid_class = $promo_enable ? 'shop-page-header-grid' : 'shop-page-header-grid no-promo';
?>

<section class="shop-page-header">
	<div class="<?php echo esc_attr( $grid_class ); ?>">

		<div class="shop-page-intro">
			<nav class="breadcrumb shop-breadcrumb-nav" aria-label="<?php esc_attr_e( 'Breadcrumb', 'shopbar' ); ?>">
				<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php esc_html_e( 'Home', 'shopbar' ); ?></a>
				<span class="shop-breadcrumb-sep">/</span>
				<?php
				if ( is_product_category() ) {
					echo '<a href="' . esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ) . '">' . esc_html__( 'Shop', 'shopbar' ) . '</a>';
					echo '<span class="shop-breadcrumb-sep">/</span>';
					echo '<span class="shop-breadcrumb-current">' . esc_html( single_term_title( '', false ) ) . '</span>';
				} elseif ( is_product_tag() ) {
					echo '<a href="' . esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ) . '">' . esc_html__( 'Shop', 'shopbar' ) . '</a>';
					echo '<span class="shop-breadcrumb-sep">/</span>';
					echo '<span class="shop-breadcrumb-current">' . esc_html( single_term_title( '', false ) ) . '</span>';
				} elseif ( is_shop() ) {
					echo '<span class="shop-breadcrumb-current">' . esc_html__( 'Shop All', 'shopbar' ) . '</span>';
				} else {
					echo '<span class="shop-breadcrumb-current">' . esc_html( $page_title ) . '</span>';
				}
				?>
			</nav>

			<h1 class="archive-page-title"><?php echo esc_html( $page_title ); ?></h1>

			<p class="archive-page-desc">
				<?php
				if ( ! empty( $page_desc ) ) {
					echo esc_html( $page_desc );
				} else {
					esc_html_e( 'Curated pieces, considered details and the small things that make everyday shopping feel good.', 'shopbar' );
				}
				?>
			</p>

			<?php if ( $total_products > 0 ) : ?>
				<p class="archive-page-result-count">
					<?php
					printf(
						/* translators: %d: number of products. */
						esc_html__( '%d products in the collection', 'shopbar' ),
						$total_products
					);
					?>
				</p>
			<?php endif; ?>
		</div>

		<?php if ( $promo_enable ) : ?>
		<aside class="shop-promo-banner" aria-label="<?php esc_attr_e( 'Shop promotion', 'shopbar' ); ?>">
			<span class="promo-deco" aria-hidden="true">
				<span class="promo-deco-num">40<i>%</i></span>
				<span class="promo-deco-shape"></span>
			</span>
			<div class="promo-banner-content">
				<span class="promo-eyebrow"><?php echo esc_html( $promo_eyebrow ); ?></span>
				<h2 class="promo-headline"><?php echo esc_html( $promo_headline ); ?></h2>
				<p class="promo-subtext"><?php echo esc_html( $promo_subtitle ); ?></p>
				<a class="promo-cta" href="<?php echo esc_url( $promo_cta_url ); ?>">
					<?php echo esc_html( $promo_cta_text ); ?>
					<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
				</a>
			</div>
		</aside>
		<?php endif; ?>

	</div>
</section>
