<?php
/**
 * Warranty Selector Template
 *
 * @package WarrantyCart
 * @version 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variables are local scope and don't need prefixing

$main_warranties = $args['main_warranties'] ?? array();
$services        = $args['services'] ?? array();
$layout          = $args['layout'] ?? 'grid';

/**
 * Filter warranty selector arguments
 *
 * @param array $args Template arguments.
 */
$args = \apply_filters( 'warranty_cart_selector_args', $args );
?>

<div id="warranty-selector" class="warranty-selector warranty-layout-<?php echo \esc_attr( $layout ); ?>">
	<?php
	/**
	 * Action before warranty selector content
	 *
	 * @param array $main_warranties Main warranty products.
	 * @param array $services Additional services.
	 * @param string $layout Layout type.
	 */
	\do_action( 'warranty_cart_before_selector_content', $main_warranties, $services, $layout );
	?>

	<?php if ( 'grid' === $layout ) : ?>
		<div class="warranty-grid">
			<?php if ( ! empty( $main_warranties ) ) : ?>
				<div class="warranty-section warranty-main-section">
					<h3 class="section-title"><?php \esc_html_e( 'Select Main Warranty', 'warranty-cart' ); ?></h3>
					<div class="products-grid">
						<?php
						foreach ( $main_warranties as $product ) :
							?>
							<div class="product-card warranty-card" data-product-id="<?php echo \esc_attr( $product->get_id() ); ?>" data-price="<?php echo \esc_attr( $product->get_price() ); ?>">
								<div class="product-header">
									<h4><?php echo \esc_html( $product->get_name() ); ?></h4>
									<span class="product-price"><?php echo \wp_kses_post( \wc_price( $product->get_price() ) ); ?></span>
								</div>
								<div class="product-description">
									<?php echo \wp_kses_post( \wp_trim_words( $product->get_short_description() ? $product->get_short_description() : $product->get_description(), 20 ) ); ?>
								</div>
								<label class="product-selector">
									<input type="radio" name="main_warranty" value="<?php echo \esc_attr( $product->get_id() ); ?>">
									<span class="checkmark"></span>
									<?php \esc_html_e( 'Select this warranty', 'warranty-cart' ); ?>
								</label>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>

			<?php if ( ! empty( $services ) ) : ?>
				<div class="warranty-section warranty-services-section">
					<h3 class="section-title"><?php \esc_html_e( 'Add Associated Services', 'warranty-cart' ); ?></h3>
					<div class="products-grid">
						<?php
						foreach ( $services as $product ) :
							?>
							<div class="product-card service-card" data-product-id="<?php echo \esc_attr( $product->get_id() ); ?>" data-price="<?php echo \esc_attr( $product->get_price() ); ?>">
								<div class="product-header">
									<h4><?php echo \esc_html( $product->get_name() ); ?></h4>
									<span class="product-price"><?php echo \wp_kses_post( '+ ' . \wc_price( $product->get_price() ) ); ?></span>
								</div>
								<div class="product-description">
									<?php echo \wp_kses_post( \wp_trim_words( $product->get_short_description() ? $product->get_short_description() : $product->get_description(), 15 ) ); ?>
								</div>
								<label class="product-selector">
									<input type="checkbox" name="services[]" value="<?php echo \esc_attr( $product->get_id() ); ?>">
									<span class="checkmark"></span>
									<?php \esc_html_e( 'Add this service', 'warranty-cart' ); ?>
								</label>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	<?php else : // List layout ?>
		<div class="warranty-list">
			<?php if ( ! empty( $main_warranties ) ) : ?>
				<div class="warranty-section warranty-main-section">
					<h3 class="section-title"><?php \esc_html_e( 'Select Main Warranty', 'warranty-cart' ); ?></h3>
					<div class="products-list">
						<?php
						foreach ( $main_warranties as $product ) :
							?>
							<div class="product-item warranty-item" data-product-id="<?php echo \esc_attr( $product->get_id() ); ?>" data-price="<?php echo \esc_attr( $product->get_price() ); ?>">
								<div class="product-info">
									<h4><?php echo \esc_html( $product->get_name() ); ?></h4>
									<div class="product-description">
										<?php echo \wp_kses_post( \wp_trim_words( $product->get_short_description() ? $product->get_short_description() : $product->get_description(), 30 ) ); ?>
									</div>
								</div>
								<div class="product-price"><?php echo \wp_kses_post( \wc_price( $product->get_price() ) ); ?></div>
								<label class="product-selector">
									<input type="radio" name="main_warranty" value="<?php echo \esc_attr( $product->get_id() ); ?>">
									<span class="checkmark"></span>
								</label>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>

			<?php if ( ! empty( $services ) ) : ?>
				<div class="warranty-section warranty-services-section">
					<h3 class="section-title"><?php \esc_html_e( 'Add Associated Services', 'warranty-cart' ); ?></h3>
					<div class="products-list">
						<?php
						foreach ( $services as $product ) :
							?>
							<div class="product-item service-item" data-product-id="<?php echo \esc_attr( $product->get_id() ); ?>" data-price="<?php echo \esc_attr( $product->get_price() ); ?>">
								<div class="product-info">
									<h4><?php echo \esc_html( $product->get_name() ); ?></h4>
									<div class="product-description">
										<?php echo \wp_kses_post( \wp_trim_words( $product->get_short_description() ? $product->get_short_description() : $product->get_description(), 25 ) ); ?>
									</div>
								</div>
								<div class="product-price"><?php echo \wp_kses_post( '+ ' . \wc_price( $product->get_price() ) ); ?></div>
								<label class="product-selector">
									<input type="checkbox" name="services[]" value="<?php echo \esc_attr( $product->get_id() ); ?>">
									<span class="checkmark"></span>
								</label>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<div class="warranty-summary">
		<div class="total-price">
			<span class="total-label"><?php \esc_html_e( 'Total:', 'warranty-cart' ); ?></span>
			<span class="total-amount"><?php echo \wp_kses_post( \wc_price( 0 ) ); ?></span>
		</div>
		<div class="selected-items" style="display: none;">
			<h4><?php \esc_html_e( 'Selected Items:', 'warranty-cart' ); ?></h4>
			<ul id="selected-items-list"></ul>
		</div>
		<button id="add-to-cart-btn" class="button alt warranty-add-to-cart" disabled>
			<?php \esc_html_e( 'Add to Cart & Checkout', 'warranty-cart' ); ?>
		</button>
	</div>

	<?php
	/**
	 * Action after warranty selector content
	 *
	 * @param array $main_warranties Main warranty products.
	 * @param array $services Additional services.
	 * @param string $layout Layout type.
	 */
	\do_action( 'warranty_cart_after_selector_content', $main_warranties, $services, $layout );
	?>
</div>
<?php // phpcs:enable ?>