<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs, the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 9.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

if ( ! is_a( $product, WC_Product::class ) || ! $product->is_visible() ) {
	return;
}

$post        = get_post();
$product_id  = $product->get_id();

$average_rating = (float) $product->get_average_rating();
$review_count   = (int) $product->get_review_count();
$is_on_sale     = $product->is_on_sale();
$is_in_stock    = $product->is_in_stock();

$badge_text  = '';
$badge_class = '';

if ( ! $is_in_stock ) {
	$badge_text  = __( 'Sold Out', 'shopbar' );
	$badge_class = 'sold-out';
} elseif ( $is_on_sale ) {
	$badge_text  = shopbar_get_product_sale_percentage( $product );
	$badge_class = 'sale-badge';
} elseif ( function_exists( 'shopbar_is_product_new' ) && shopbar_is_product_new( $product_id ) ) {
	$badge_text  = __( 'New', 'shopbar' );
	$badge_class = 'new';
}

$brand_name         = shopbar_get_product_brand( $product_id );
$product_categories = wc_get_product_category_list( $product_id, ', ' );

// Meta line
$short_desc = trim( wp_strip_all_tags( $product->get_short_description() ) );
?>

<div <?php wc_product_class( 'product-card', $product ); ?> data-product-id="<?php echo esc_attr( $product_id ); ?>">

	<div class="product-card-img">
		<a href="<?php the_permalink(); ?>" class="product-card-img-link">
			<?php
			if ( function_exists( 'shopbar_template_loop_product_thumbnail' ) ) {
				shopbar_template_loop_product_thumbnail();
			} else {
				woocommerce_template_loop_product_thumbnail();
			}
			?>
			<?php do_action( 'shopbar_product_second_image', $product_id ); ?>
		</a>

		<?php if ( ! empty( $badge_text ) ) : ?>
			<span class="product-badge <?php echo esc_attr( $badge_class ); ?>"><?php echo esc_html( $badge_text ); ?></span>
		<?php endif; ?>
	</div>

	<div class="product-card-info">

		<?php if ( ! empty( $product_categories ) ) : ?>
			<div class="product-card-category"><?php echo wp_kses_post( $product_categories ); ?></div>
		<?php elseif ( ! empty( $brand_name ) ) : ?>
			<div class="product-card-category"><?php echo esc_html( $brand_name ); ?></div>
		<?php endif; ?>

		<h3 class="product-card-name">
			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		</h3>

		<?php if ( ! empty( $short_desc ) ) : ?>
			<p class="product-card-desc"><?php echo esc_html( wp_trim_words( $short_desc, 9, '&hellip;' ) ); ?></p>
		<?php endif; ?>

		<?php if ( $average_rating > 0 ) : ?>
			<div class="product-card-rating">
				<?php echo wc_get_rating_html( $average_rating, $review_count ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<?php if ( $review_count > 0 ) : ?>
					<span class="rating-count">(<?php echo esc_html( number_format_i18n( $review_count ) ); ?>)</span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php
		do_action( 'woocommerce_after_shop_loop_item_title' );

		shopbar_archive_swatches();
		?>

		<div class="product-card-footer">
			<div class="product-card-price">
				<?php woocommerce_template_loop_price(); ?>
			</div>

			<div class="product-card-actions">
				<?php if ( $is_in_stock ) :
					$is_in_cart = false;
					if ( function_exists( 'WC' ) && WC()->cart ) {
						foreach ( WC()->cart->get_cart() as $cart_item ) {
							if ( $cart_item['product_id'] === $product_id ) {
								$is_in_cart = true;
								break;
							}
						}
					}

					if ( $is_in_cart ) : ?>
						<button class="product-card-wishlist quick-view-added" disabled aria-label="<?php esc_attr_e( 'Already in cart', 'shopbar' ); ?>" title="<?php esc_attr_e( 'Added to cart', 'shopbar' ); ?>">
							<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>
						</button>
					<?php else : ?>
						<button data-quick-add-to-cart="<?php echo esc_attr( $product_id ); ?>" aria-label="<?php esc_attr_e( 'Add to cart', 'shopbar' ); ?>" title="<?php esc_attr_e( 'Add to cart', 'shopbar' ); ?>">
							<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>
						</button>
					<?php endif; ?>
				<?php endif; ?>

				<?php do_action( 'shopbar_before_compare_button', $product_id ); ?>
				<?php do_action( 'shopbar_after_wishlist_button', $product_id ); ?>
				<?php do_action( 'shopbar_after_compare_button', $product_id ); ?>
			</div>
		</div>

		<?php shopbar_countdown_loop(); ?>
	</div>

</div>
