<?php
/**
 * Template Name: Custom Home
 *
 * @package Storecraft
 * @since 1.0
 */

get_header();
?>

<main id="skip-content" role="main">

	<?php
	/* ---------- Slider Section (from Pages) ---------- */
	$storecraft_slider_show = get_theme_mod( 'storecraft_slider_show', true );
	if ( $storecraft_slider_show ) :

		$storecraft_slider_pages = array();
		for ( $storecraft_i = 1; $storecraft_i <= 3; $storecraft_i++ ) {
			$storecraft_mod = absint( get_theme_mod( 'storecraft_slider_page' . $storecraft_i ) );
			if ( $storecraft_mod > 0 ) {
				$storecraft_slider_pages[] = $storecraft_mod;
			}
		}

		if ( ! empty( $storecraft_slider_pages ) ) :
			$storecraft_slider_query = new WP_Query(
				array(
					'post_type'      => 'page',
					'post__in'       => $storecraft_slider_pages,
					'orderby'        => 'post__in',
					'posts_per_page' => 3,
				)
			);

			if ( $storecraft_slider_query->have_posts() ) :
				$storecraft_slides = array();
				while ( $storecraft_slider_query->have_posts() ) {
					$storecraft_slider_query->the_post();

					$storecraft_slide_excerpt = '';
					if ( has_excerpt() ) {
						$storecraft_slide_excerpt = get_the_excerpt();
					} else {
						$storecraft_slide_raw = get_post_field( 'post_content', get_the_ID() );
						if ( $storecraft_slide_raw ) {
							$storecraft_slide_excerpt = wp_strip_all_tags( $storecraft_slide_raw );
						}
					}
					$storecraft_slide_excerpt = $storecraft_slide_excerpt ? wp_trim_words( $storecraft_slide_excerpt, 28, '…' ) : '';

					$storecraft_slides[] = array(
						'id'      => get_the_ID(),
						'title'   => get_the_title(),
						'url'     => get_permalink(),
						'excerpt' => $storecraft_slide_excerpt,
						'image'   => get_the_post_thumbnail_url( get_the_ID(), 'full' ),
						'thumb'   => get_the_post_thumbnail_url( get_the_ID(), 'medium_large' ),
					);
				}
				wp_reset_postdata();

				$storecraft_slide_count = count( $storecraft_slides );
				$storecraft_btn_text    = get_theme_mod( 'storecraft_slider_btn_text', __( 'SHOP NOW', 'storecraft' ) );
				$storecraft_next_thumb  = '';
				if ( $storecraft_slide_count > 1 ) {
					$storecraft_next_thumb = ! empty( $storecraft_slides[1]['thumb'] ) ? $storecraft_slides[1]['thumb'] : $storecraft_slides[1]['image'];
				}
				?>
				<section id="slider" class="hbs-slider-section hbs-hero-banner">
					<div id="carouselExampleIndicators" class="carousel slide hbs-hero-carousel" data-ride="carousel">
						<?php if ( $storecraft_slide_count > 1 ) : ?>
							<ol class="carousel-indicators">
								<?php for ( $storecraft_i = 0; $storecraft_i < $storecraft_slide_count; $storecraft_i++ ) : ?>
									<li data-target="#carouselExampleIndicators" data-slide-to="<?php echo esc_attr( $storecraft_i ); ?>" class="<?php echo ( 0 === $storecraft_i ) ? 'active' : ''; ?>"></li>
								<?php endfor; ?>
							</ol>
						<?php endif; ?>

						<div class="carousel-inner" role="listbox">
							<?php foreach ( $storecraft_slides as $storecraft_index => $storecraft_slide ) : ?>
								<?php
								$storecraft_slide_bg   = $storecraft_slide['image'];
								$storecraft_slide_thumb = ! empty( $storecraft_slide['thumb'] ) ? $storecraft_slide['thumb'] : $storecraft_slide['image'];
								?>
								<div class="carousel-item<?php echo ( 0 === $storecraft_index ) ? ' active' : ''; ?>" data-thumb="<?php echo esc_url( $storecraft_slide_thumb ); ?>">
									<div class="hbs-slider-slide"<?php echo $storecraft_slide_bg ? ' style="background-image:url(' . esc_url( $storecraft_slide_bg ) . ');"' : ''; ?>>
										<span class="hbs-slider-overlay" aria-hidden="true"></span>
										<div class="container hbs-slider-container">
											<div class="content hbs-slider-content">
												<?php if ( get_theme_mod( 'storecraft_slider_subheading', __( 'New Arrivals', 'storecraft' ) ) ) : ?>
													<p class="hbs-slider-subheading"><?php echo esc_html( get_theme_mod( 'storecraft_slider_subheading', __( 'New Arrivals', 'storecraft' ) ) ); ?></p>
												<?php endif; ?>
												<h2 class="hbs-slider-title"><?php echo esc_html( $storecraft_slide['title'] ); ?></h2>
												<?php if ( ! empty( $storecraft_slide['excerpt'] ) ) : ?>
													<p class="hbs-slider-description"><?php echo esc_html( $storecraft_slide['excerpt'] ); ?></p>
												<?php endif; ?>
												<?php if ( ! empty( $storecraft_btn_text ) ) : ?>
													<div class="hbs-slider-buttons read-btn">
														<a class="sbtn1" href="<?php echo esc_url( $storecraft_slide['url'] ); ?>"><?php echo esc_html( $storecraft_btn_text ); ?></a>
													</div>
												<?php endif; ?>
											</div>
										</div>
									</div>
								</div>
							<?php endforeach; ?>
						</div>

						<?php if ( $storecraft_slide_count > 1 && ! empty( $storecraft_next_thumb ) ) : ?>
							<button type="button" class="hbs-slider-next-preview" data-target="#carouselExampleIndicators" data-slide="next" aria-label="<?php esc_attr_e( 'Next slide', 'storecraft' ); ?>">
								<span class="hbs-slider-next-media">
									<img class="hbs-slider-next-img" src="<?php echo esc_url( $storecraft_next_thumb ); ?>" alt="" aria-hidden="true" loading="lazy" />
								</span>
								<span class="hbs-slider-next-meta" aria-hidden="true">
									<span class="hbs-slider-next-label"><?php esc_html_e( 'Next', 'storecraft' ); ?></span>
									<span class="hbs-slider-next-arrow">&rarr;</span>
								</span>
							</button>
						<?php endif; ?>
					</div>
				</section>
				<?php
			endif;
		endif;
	endif;
	?>

	<?php
	/* ---------- Product Category Section ---------- */
	$storecraft_cat_show = get_theme_mod( 'storecraft_category_show', true );
	if ( $storecraft_cat_show && class_exists( 'WooCommerce' ) ) :

		$storecraft_cat_count = absint( get_theme_mod( 'storecraft_category_count', 6 ) );
		if ( $storecraft_cat_count < 1 ) {
			$storecraft_cat_count = 6;
		}

		$storecraft_exclude = array();
		$storecraft_uncat   = get_option( 'default_product_cat' );
		if ( $storecraft_uncat ) {
			$storecraft_exclude[] = (int) $storecraft_uncat;
		}

		$storecraft_product_cats = get_terms(
			array(
				'taxonomy'   => 'product_cat',
				'hide_empty' => true,
				'number'     => $storecraft_cat_count,
				'exclude'    => $storecraft_exclude,
			)
		);

		if ( ! empty( $storecraft_product_cats ) && ! is_wp_error( $storecraft_product_cats ) ) :
			$storecraft_cat_title = get_theme_mod( 'storecraft_category_title', __( 'Shop by category', 'storecraft' ) );
			?>
			<section id="productcategory-section" class="hbs-category-section">
				<div class="container-fluid">
					<?php if ( ! empty( $storecraft_cat_title ) ) : ?>
						<div class="hbs-section-header hbs-section-header--center">
							<h2 class="hbs-section-title"><?php echo esc_html( $storecraft_cat_title ); ?></h2>
						</div>
					<?php endif; ?>

					<div class="hbs-cat-grid">
						<?php foreach ( $storecraft_product_cats as $storecraft_term ) : ?>
							<?php
							$storecraft_thumb_id = get_term_meta( $storecraft_term->term_id, 'thumbnail_id', true );
							$storecraft_cat_link = get_term_link( $storecraft_term );
							if ( is_wp_error( $storecraft_cat_link ) ) {
								continue;
							}
							?>
							<a class="hbs-cat-card" href="<?php echo esc_url( $storecraft_cat_link ); ?>">
								<span class="hbs-cat-media">
									<?php
									if ( $storecraft_thumb_id ) {
										echo wp_get_attachment_image( $storecraft_thumb_id, 'woocommerce_single', false, array( 'alt' => esc_attr( $storecraft_term->name ) ) );
									} elseif ( function_exists( 'wc_placeholder_img' ) ) {
										echo wp_kses_post( wc_placeholder_img( 'woocommerce_single' ) );
									}
									?>
								</span>
								<span class="hbs-cat-name"><?php echo esc_html( $storecraft_term->name ); ?></span>
							</a>
						<?php endforeach; ?>
					</div>
				</div>
			</section>
			<?php
		endif;
	endif;
	?>

	<?php
	/* ---------- Products Section ---------- */
	$storecraft_products_show = get_theme_mod( 'storecraft_products_show', true );
	if ( $storecraft_products_show && class_exists( 'WooCommerce' ) ) :

		$storecraft_products_query = new WP_Query(
			array(
				'post_type'           => 'product',
				'post_status'         => 'publish',
				'posts_per_page'      => -1,
				'ignore_sticky_posts' => true,
				'orderby'             => 'menu_order title',
				'order'               => 'ASC',
				'tax_query'           => array(
					array(
						'taxonomy' => 'product_visibility',
						'field'    => 'name',
						'terms'    => array( 'exclude-from-catalog', 'exclude-from-search' ),
						'operator' => 'NOT IN',
					),
				),
			)
		);

		if ( $storecraft_products_query->have_posts() ) :
			$storecraft_products_title = get_theme_mod( 'storecraft_products_title', __( 'Featured Products', 'storecraft' ) );
			$storecraft_shop_url       = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'shop' ) : '';
			$storecraft_products_btn   = get_theme_mod( 'storecraft_products_btn_text', __( 'View all', 'storecraft' ) );
			?>
			<section id="products-section" class="hbs-products-section">
				<div class="container-fluid">
					<div class="hbs-section-header">
						<?php if ( ! empty( $storecraft_products_title ) ) : ?>
							<h2 class="hbs-section-title"><?php echo esc_html( $storecraft_products_title ); ?></h2>
						<?php endif; ?>
						<?php if ( $storecraft_shop_url && ! empty( $storecraft_products_btn ) ) : ?>
							<a class="hbs-view-all-link" href="<?php echo esc_url( $storecraft_shop_url ); ?>"><?php echo esc_html( $storecraft_products_btn ); ?></a>
						<?php endif; ?>
					</div>

					<div class="hbs-products-grid woocommerce">
						<?php
						if ( function_exists( 'wc_set_loop_prop' ) ) {
							wc_set_loop_prop( 'columns', 4 );
						}
						woocommerce_product_loop_start();
						while ( $storecraft_products_query->have_posts() ) :
							$storecraft_products_query->the_post();
							wc_get_template_part( 'content', 'product' );
						endwhile;
						woocommerce_product_loop_end();
						wp_reset_postdata();
						?>
					</div>
				</div>
			</section>
			<?php
		endif;
	endif;
	?>

	<?php
	/* ---------- Banner Section ---------- */
	$storecraft_banner_show = get_theme_mod( 'storecraft_banner_show', true );
	if ( $storecraft_banner_show ) :

		$storecraft_banner_kicker = get_theme_mod( 'storecraft_banner_kicker', __( 'FLASH SALE', 'storecraft' ) );
		$storecraft_banner_title  = get_theme_mod( 'storecraft_banner_title', __( 'Limited Time Special Offer', 'storecraft' ) );
		$storecraft_banner_text   = get_theme_mod( 'storecraft_banner_text', __( 'Discover exclusive savings on curated pieces. This offer won\'t last — shop before it\'s gone.', 'storecraft' ) );
		$storecraft_banner_btn    = get_theme_mod( 'storecraft_banner_btn_text', __( 'SHOP NOW', 'storecraft' ) );
		$storecraft_banner_btn2   = get_theme_mod( 'storecraft_banner_btn2_text', __( 'VIEW ALL DEALS', 'storecraft' ) );
		$storecraft_banner_url    = get_theme_mod( 'storecraft_banner_btn_url', '' );
		$storecraft_banner_img    = get_theme_mod( 'storecraft_banner_image', '' );

		if ( empty( $storecraft_banner_url ) && function_exists( 'wc_get_page_permalink' ) ) {
			$storecraft_banner_url = wc_get_page_permalink( 'shop' );
		}

		if ( ! empty( $storecraft_banner_title ) || ! empty( $storecraft_banner_img ) ) :
			?>
			<section id="banner-section" class="hbs-banner-section">
				<div class="container">
					<div class="hbs-banner-inner<?php echo $storecraft_banner_img ? ' has-image' : ''; ?>">
						<div class="hbs-banner-content">
							<?php if ( ! empty( $storecraft_banner_kicker ) ) : ?>
								<p class="hbs-banner-kicker"><?php echo esc_html( $storecraft_banner_kicker ); ?></p>
							<?php endif; ?>
							<?php if ( ! empty( $storecraft_banner_title ) ) : ?>
								<h2 class="hbs-banner-title"><?php echo esc_html( $storecraft_banner_title ); ?></h2>
							<?php endif; ?>
							<?php if ( ! empty( $storecraft_banner_text ) ) : ?>
								<p class="hbs-banner-text"><?php echo esc_html( $storecraft_banner_text ); ?></p>
							<?php endif; ?>
							<div class="hbs-banner-actions">
								<?php if ( ! empty( $storecraft_banner_btn ) && ! empty( $storecraft_banner_url ) ) : ?>
									<a class="hbs-banner-btn hbs-banner-btn--primary" href="<?php echo esc_url( $storecraft_banner_url ); ?>"><?php echo esc_html( $storecraft_banner_btn ); ?></a>
								<?php endif; ?>
								<?php if ( ! empty( $storecraft_banner_btn2 ) && ! empty( $storecraft_banner_url ) ) : ?>
									<a class="hbs-banner-btn hbs-banner-btn--secondary" href="<?php echo esc_url( $storecraft_banner_url ); ?>"><?php echo esc_html( $storecraft_banner_btn2 ); ?></a>
								<?php endif; ?>
							</div>
						</div>
						<?php if ( $storecraft_banner_img ) : ?>
							<div class="hbs-banner-media">
								<img src="<?php echo esc_url( $storecraft_banner_img ); ?>" alt="<?php echo esc_attr( $storecraft_banner_title ); ?>" loading="lazy" />
							</div>
						<?php endif; ?>
					</div>
				</div>
			</section>
			<?php
		endif;
	endif;
	?>

	<?php
	/* ---------- Blog Section ---------- */
	$storecraft_blog_show = get_theme_mod( 'storecraft_blog_show', true );
	if ( $storecraft_blog_show ) :

		$storecraft_blog_num = absint( get_theme_mod( 'storecraft_blog_count', 3 ) );
		if ( $storecraft_blog_num < 1 ) {
			$storecraft_blog_num = 3;
		}

		$storecraft_blog_query = new WP_Query(
			array(
				'post_type'           => 'post',
				'post_status'         => 'publish',
				'posts_per_page'      => $storecraft_blog_num,
				'ignore_sticky_posts' => true,
			)
		);

		if ( $storecraft_blog_query->have_posts() ) :
			$storecraft_blog_title = get_theme_mod( 'storecraft_blog_title', __( 'News', 'storecraft' ) );
			$storecraft_blog_btn   = get_theme_mod( 'storecraft_blog_btn_text', __( 'Visit Blog', 'storecraft' ) );
			$storecraft_posts_url  = get_permalink( get_option( 'page_for_posts' ) );
			if ( ! $storecraft_posts_url ) {
				$storecraft_posts_url = home_url( '/' );
			}
			?>
			<section id="blogposts-section" class="hbs-blog-section">
				<div class="container">
					<div class="hbs-section-header">
						<?php if ( ! empty( $storecraft_blog_title ) ) : ?>
							<h2 class="hbs-section-title"><?php echo esc_html( $storecraft_blog_title ); ?></h2>
						<?php endif; ?>
						<?php if ( ! empty( $storecraft_blog_btn ) ) : ?>
							<a class="hbs-view-all-link" href="<?php echo esc_url( $storecraft_posts_url ); ?>"><?php echo esc_html( $storecraft_blog_btn ); ?></a>
						<?php endif; ?>
					</div>

					<div class="hbs-blog-grid">
						<?php
						while ( $storecraft_blog_query->have_posts() ) :
							$storecraft_blog_query->the_post();
							?>
							<article <?php post_class( 'hbs-blog-card' ); ?>>
								<?php if ( has_post_thumbnail() ) : ?>
									<a class="hbs-blog-card-image" href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( 'medium_large', array( 'alt' => the_title_attribute( array( 'echo' => false ) ), 'loading' => 'lazy' ) ); ?>
									</a>
								<?php endif; ?>
								<h3 class="hbs-blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
								<p class="hbs-blog-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 22, '…' ) ); ?></p>
							</article>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</section>
			<?php
		endif;
	endif;
	?>

</main>

<?php
get_footer();
?>
