<?php
/**
 * Variable product add to cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.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://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.4.1
 */

defined( 'ABSPATH' ) || exit;

global $product;
global $wp;

$attribute_keys = array_keys( $attributes );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<? 
	$related_products = get_posts(array(
        'post_type' => 'group-products',
        'meta_query' => array(
		  array(
		    'key' => 'group',  
		    'value' => get_the_id(), 
		    'compare' => 'LIKE'
		    )
		)
    ));      
?>

<form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ); // WPCS: XSS ok. ?>">
	<?php do_action( 'woocommerce_before_variations_form' ); ?>

	<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
		<p class="stock out-of-stock"><?php esc_html_e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
	<?php else : ?>
	
	<div class="box variations">
		
				<?php foreach ( $attributes as $attribute_name => $options ) : ?>
				<?php
					if  ($attribute_name == 'pa_kolor') {
								 
								echo "<div style='display: none;'>";
									$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
									 
									wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
									echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
								echo "</div>";
								$orderby = wc_attribute_orderby( $name );
								
								
	
								switch ( $orderby ) {
									case 'name' :
										$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
									break;
									case 'id' :
										$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false );
									break;
									case 'menu_order' :
										$args = array( 'menu_order' => 'ASC' );
									break;
								}
					}			
				?>
					<div class="box--row row">
						<div class="label col-lg-2"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></div>
						<div class="value col-lg-10 <?if  ($attribute_name == 'pa_rozmiar') { ?> d-flex<?}?>">
							<?
								if  ($attribute_name == 'pa_kolor') {
									
// 									print_r($attributes["pa_kolor"]);
								    echo "<div class='color-swatch-listing'>";
								    $args = array( 'orderby' => 'date',  'order' => "ASC", 'hide_empty' => false, 'menu_order' => false );
// 								    $args = array(" 'options' => $options, 'attribute' => pa_kolor, 'product' => $product, 'selected' => $selected");
								    
								    
// 								    $terms = get_terms( 'pa_kolor', $args );
								    $i=0;
								   
									
								    $product_id = get_the_id();
									

								   
									
									
								    foreach ( $options as $option ) :
								    $terms = get_term_by('slug', $option, $attribute_name );
								    
								    $term_name = $terms->name;
								    $term_desc = $terms->description;
								    $term_id = $terms->term_taxonomy_id;
								    $term_slug = $terms->slug;
								    $link = get_the_permalink().__("color","052b").'-'.$term_slug.'/';
								    $currentLink = home_url( $wp->request );
                                    
                                    $currentLink = rtrim($currentLink, '/') . '/';
                                    
								    if($term_desc === "colorful") {
									    echo '<a href="'.$link.'" title="'.$term_name.'" class="single-swatch '.($currentLink == $link ? "active" : "").' " style="background:'.$term_desc.'" data-attribute_name="'.$term_slug.'"><span class="single-swatch--yellow"></span><span class="single-swatch--green"></span><span class="single-swatch--red"></span><span class="single-swatch--blue"></span></a>';
									} else {
										echo '<a href="'.$link.'" title="'.$term_name.'" class="single-swatch '.($currentLink == $link ? "active" : "").' " style="background:'.$term_desc.'" data-attribute_name="'.$term_slug.'"></a>';
									};
									    
								    $i++;
								    endforeach;

								    echo "</div>";
								} else if ($attribute_name == "pa_rozmiar") {
								
									wc_dropdown_variation_attribute_options( array(
										'options'   => $options,
										'attribute' => $attribute_name,
										'product'   => $product
									) );
									
									?>
									<div class="quantity-box <?if($product->get_stock_quantity() == 1) { ?> d-none<?}?>">
										<span class="label"><?_e("Quantity", '052b');?>:</span>
									<?
										if($product->get_stock_quantity() == 0) { ?>
											<span class="unavailable_label"><?_e("Product temporary unavailable", "052b");?></span>
									<?		
										} else {
									?>
										<?php
											do_action( 'woocommerce_before_add_to_cart_quantity' );
										
											woocommerce_quantity_input( array(
												'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
												'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
												'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
											) );
										
											do_action( 'woocommerce_after_add_to_cart_quantity' );
										?>
									<?}?>
									</div>

								<?
								}


							?>
						</div>
						
					</div>
				<?php endforeach; ?>
				<div class="box--row row">
					<div class="label col-lg-2">
						<?_e("Price", '052b')?>:
					</div>
					<div class="value value-price col-lg-3">
						<span><? wc_get_template( 'single-product/price.php' );?></span>
					</div>
					<div class="add-to-cart col-lg-7">
						
						
						<div class="single_variation_wrap">
							<?php
								/**
								 * Hook: woocommerce_before_single_variation.
								 */
								do_action( 'woocommerce_before_single_variation' );
				
								/**
								 * Hook: woocommerce_single_variation. Used to output the cart button and placeholder for variation data.
								 *
								 * @since 2.4.0
								 * @hooked woocommerce_single_variation - 10 Empty div for variation data.
								 * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
								 */
								do_action( 'woocommerce_single_variation' );
				
								/**
								 * Hook: woocommerce_after_single_variation.
								 */
								do_action( 'woocommerce_after_single_variation' );
							?>
						</div>
					</div>
				</div>
				
		</div>
		
		<div class="single_variation_wrap">
		
			<div class="woocommerce-variation single_variation" style="">
				<div class="woocommerce-variation-availability">
					
				</div>
			</div>
		
		</div>

	<?php endif; ?>

	<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>

<?php
do_action( 'woocommerce_after_add_to_cart_form' );
?>
<div class="short-product-desc">
	<?
		$ing = get_field("ingredients");
		$maint = get_field("maintance")
	?>
<?if($ing) { ?>	
<p><span><?_e("Composition", '052b')?>:</span> <?=$ing?></p>
<?}?>
<?if($maint) { ?>
<p><span><?_e("Care and maintenance", '052b')?>:</span> <?=$maint?></p>
<?}?>
</div>