<?php
/**
 * Homepage WooCommerce products section.
 *
 * @package Shabnam_MuchWOW
 */
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'WooCommerce' ) || ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'shop_home_enabled', true ) ) ) {
	return;
}

$count    = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'shop_home_count', 4 ) ) : 4;
$title    = function_exists( 'shabnam_get_setting' ) ? shabnam_get_setting( 'shop_home_title', 'محصولات منتخب' ) : 'محصولات منتخب';
$subtitle = function_exists( 'shabnam_get_setting' ) ? shabnam_get_setting( 'shop_home_subtitle', 'محصولات تازه و پیشنهادهای فروشگاه را یک‌جا ببینید.' ) : '';
$columns  = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'shop_columns', 3 ) ) : 3;
$orderby  = function_exists( 'shabnam_get_setting' ) ? sanitize_key( (string) shabnam_get_setting( 'shop_home_orderby', 'date' ) ) : 'date';
$order    = function_exists( 'shabnam_get_setting' ) ? strtoupper( sanitize_text_field( (string) shabnam_get_setting( 'shop_home_order', 'DESC' ) ) ) : 'DESC';
$hide_oos = function_exists( 'shabnam_get_setting' ) ? (bool) shabnam_get_setting( 'shop_hide_out_of_stock', false ) : false;
$count    = max( 2, min( 12, $count ) );
$columns  = max( 2, min( 4, $columns ) );
$orderby  = in_array( $orderby, array( 'date', 'modified', 'title', 'menu_order' ), true ) ? $orderby : 'date';
$order    = in_array( $order, array( 'ASC', 'DESC' ), true ) ? $order : 'DESC';

$query = new WP_Query(
	array(
		'post_type'      => 'product',
		'post_status'    => 'publish',
		'posts_per_page' => $count,
		'no_found_rows'  => true,
		'orderby'        => $orderby,
		'order'          => $order,
		'meta_query'     => $hide_oos ? array( array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => '!=' ) ) : array(),
	)
);

if ( ! $query->have_posts() ) {
	return;
}
?>
<section class="shabnam-home-shop section panel overflow-hidden woocommerce" id="shop">
	<div class="section-outer panel py-6 sm:py-8 lg:py-9 xl:py-10">
		<div class="container sm:max-w-md xl:max-w-lg">
			<div class="section-header panel sm:max-w-800px mx-auto mb-5 sm:mb-7 text-center">
				<div class="category"><div class="-rotate-2"><span class="fs-5 xl:fs-4 text-secondary fw-bold"><?php esc_html_e( 'فروشگاه', 'shabnam-muchwow' ); ?></span></div></div>
				<h2 class="h4 sm:h3 xl:h2 mb-0 mt-2 text-white"><?php echo esc_html( $title ); ?></h2>
				<?php if ( $subtitle ) : ?><p class="fs-5 mt-2 text-white text-opacity-70"><?php echo esc_html( $subtitle ); ?></p><?php endif; ?>
			</div>
			<ul class="products shabnam-home-product-grid columns-<?php echo esc_attr( $columns ); ?>" style="--shcd-shop-columns:<?php echo esc_attr( $columns ); ?>">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();
					$GLOBALS['product'] = wc_get_product( get_the_ID() );
					if ( $GLOBALS['product'] ) {
						wc_get_template_part( 'content', 'product' );
					}
				endwhile;
				?>
			</ul>
			<div class="section-footer panel d-flex justify-center mt-5 sm:mt-6">
				<a class="btn btn-md sm:btn-xl px-4 border border-3 border-black bg-gradient-45n from-secondary to-tertiary contrast-shadow -rotate-2 hover:rotate-0" href="<?php echo esc_url( shabnam_shop_url() ); ?>"><span class="text-black"><?php esc_html_e( 'مشاهده همه محصولات', 'shabnam-muchwow' ); ?></span></a>
			</div>
		</div>
	</div>
</section>
<?php wp_reset_postdata(); if ( function_exists( 'wc_reset_loop' ) ) { wc_reset_loop(); } unset( $GLOBALS['product'] ); ?>
