<?php
/**
 * Product card.
 *
 * @package Foliora
 */

defined( 'ABSPATH' ) || exit;

global $product;
if ( ! is_a( $product, 'WC_Product' ) || ! $product->is_visible() ) {
	return;
}

$copy   = foliora_copy_for_product( $product );
$img    = foliora_product_card_image( $product );
$hover  = foliora_hover_image_url( $product );
$badge  = $copy['badge'];
$sku    = $product->get_sku();
?>
<li <?php wc_product_class( 'p-card', $product ); ?>>
	<a class="p-card__media" href="<?php echo esc_url( $product->get_permalink() ); ?>">
		<img class="p-card__img" src="<?php echo esc_url( $img ); ?>" alt="" width="800" height="1000">
		<?php if ( $hover && $hover !== $img ) : ?>
			<img class="p-card__img p-card__img--hover" src="<?php echo esc_url( $hover ); ?>" alt="" width="800" height="1000">
		<?php endif; ?>
		<?php if ( $badge ) : ?>
			<span class="p-card__badge"><?php echo esc_html( $badge ); ?></span>
		<?php endif; ?>
	</a>
	<div class="p-card__body">
		<h2 class="woocommerce-loop-product__title p-card__title">
			<a href="<?php echo esc_url( $product->get_permalink() ); ?>"><?php echo esc_html( $product->get_name() ); ?></a>
		</h2>
		<p class="p-card__stars"><span class="stars" aria-hidden="true">★★★★★</span> <?php echo esc_html( $copy['rating_display'] ); ?></p>
		<p class="p-card__price"><?php echo wp_kses_post( $product->get_price_html() ); ?></p>
		<?php if ( 0 === strpos( (string) $sku, 'FOL-SLV-VIN' ) ) : ?>
			<p class="swatches">
				<?php foreach ( foliora_sleeve_swatches() as $color => $file ) : ?>
					<a class="swatch swatch--<?php echo esc_attr( sanitize_title( $color ) ); ?>" href="<?php echo esc_url( $product->get_permalink() ); ?>"><span class="visually-hidden"><?php echo esc_html( $color ); ?></span></a>
				<?php endforeach; ?>
			</p>
		<?php endif; ?>
		<?php if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) : ?>
			<a
				class="button button--small ajax_add_to_cart add_to_cart_button"
				href="<?php echo esc_url( $product->add_to_cart_url() ); ?>"
				data-product_id="<?php echo esc_attr( (string) $product->get_id() ); ?>"
				data-product_sku="<?php echo esc_attr( $sku ); ?>"
				data-quantity="1"
			>Quick Add</a>
		<?php else : ?>
			<a class="button button--small" href="<?php echo esc_url( $product->get_permalink() ); ?>">View</a>
		<?php endif; ?>
	</div>
</li>
