<?php

namespace Rtc\PlantShopWC\Frontend\Views;

use WP_Query;

class SingleProductView {


	public function render_instructions( $data ) {

		?>

		<div class="plantshop-wc-product-instructions alignwide" style="margin-bottom: 2rem;">

			<?php
			if ( ! empty( $data['care'] ) ) {
				?>

				<div class="plantshop-wc-product-instructions-care">
					<h2>
					<?php
						echo esc_html__( 'Care Instructions', 'plantshop-wc' )
					?>
					</h2>
					<p>
					<?php
						echo esc_html( $data['care'] )
					?>
					</p>
				</div>

				<?php
			}
			?>

			<?php
			if ( ! empty( $data['planting'] ) ) {
				?>

				<div class="plantshop-wc-product-instructions-planting">
					<h2>
					<?php
						echo esc_html__( 'Planting Instructions', 'plantshop-wc' )
					?>
					</h2>
					<p>
					<?php
						echo esc_html( $data['planting'] )
					?>
					</p>
				</div>

				<?php
			}
			?>
		</div>

		<?php
	}

	public function render_neighbors( array $groups ): void {

		?>
		<div class="plantshop-wc-neighbors alignwide">
			<?php

			foreach ( $groups as $group ) {

				$ids = $group['ids'] ?? array();
				if ( empty( $ids ) ) {
					continue;
				}

				$q = new WP_Query(
					array(
						'post_type'      => 'product',
						'post_status'    => 'publish',
						'posts_per_page' => count( $ids ),
						'post__in'       => $ids,
						'orderby'        => 'post__in',
						'no_found_rows'  => true,
					)
				);

				if ( ! $q->have_posts() ) {

					wp_reset_postdata();
					continue;

				}

				?>

				<section class="plantshop-wc-neighbors-
				<?php
				echo esc_attr( $group['slug'] )
				?>
				neighbors" style="margin-bottom: 2rem;">
					<h2> 
					<?php
						echo esc_html( $group['title'] )
					?>
					</h2>

					<?php
					woocommerce_product_loop_start();

					while ( $q->have_posts() ) {

						$q->the_post();

						?>
						<div class="plantshop-wc-neighbor product">

							<?php
							wc_get_template_part( 'content', 'product' );

							$pid = get_the_ID();
							if ( ! empty( $group['notes'][ $pid ] ) ) {

								?>
								<div class="plantshop-wc-neighbor-note">
								<?php
									echo esc_html( $group['notes'][ $pid ] )
								?>
								</div>
								<?php

							}
							?>
						</div>
						<?php

					}

					woocommerce_product_loop_end();

					?>

				</section>
				<?php
				wp_reset_postdata();

			}

			?>
		</div>
		<?php
	}
}