<?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://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 6.1.0
 */

defined( 'ABSPATH' ) || exit;

global $product;
global $post;

$current_product_id = get_the_ID();
$checkout_url = wc_get_checkout_url();

$attribute_keys  = array_keys( $attributes );
$variations_json = wp_json_encode( $available_variations );
$variations_attr = function_exists( 'wc_esc_json' ) ? wc_esc_json( $variations_json ) : _wp_specialchars( $variations_json, ENT_QUOTES, 'UTF-8', true );

do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<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 $variations_attr; // 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 echo esc_html( apply_filters( 'woocommerce_out_of_stock_message', __( 'This product is currently out of stock and unavailable.', 'woocommerce' ) ) ); ?></p>
	<?php else : ?>
		<table class="variations" cellspacing="0" role="presentation">
			<tbody>
				<div class="size-wrapper">
					<div class="select-size">SELECT SIZE</div>
					<div class="size-guide">
						<a id="imageLink" href="<?php echo get_post_meta($product->get_id(), 'size_guide', true); ?>">SIZE GUIDE <i class="fa-solid fa-angle-right"></i></a>
						<div id="popup" class="popup">
							<span class="close">&times;</span>
							<img class="popup-content" id="popupImage">
						</div>
					</div>
				</div>
				<?php foreach ( $attributes as $attribute_name => $options ) : ?>
					<tr>
						<th class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></th>
						<td class="value">
							<div class="pa_size_wrapper">
							<?php
								$variable_id = wc_dropdown_variation_attribute_options(
									array(
										'options'   => $options,
										'attribute' => $attribute_name,
										'product'   => $product,
									)
								);
								echo "</div>";
								echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';

								echo $variable_id;
							?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		
		<?php do_action( 'woocommerce_after_variations_table' ); ?>

		<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>
	<?php endif; ?>
	<?php do_action( 'woocommerce_after_variations_form' ); ?>
	<?php print_r($variable_id);?>
	<div class="single-pro-btn">
			<div class="single-product-meta-btn-wrapper">
				<div class="single-product-wishlist-btn single-product-meta-btn">
					<?php if ( class_exists( 'YITH_WCWL' ) ) {  ?>
						<div class="wishlist_text text-center">
							<?php echo do_shortcode('[yith_wcwl_add_to_wishlist]'); ?>
						</div>
					<?php 
					} ?>
				</div>
				<div class="single-product-buy-now-btn single-product-meta-btn">
					<a target="_blank" class="single-product-buy-now" id="single-product-buy-now"><span class="icon"><i class="fa-solid fa-user"></i></span> Buy Now It</a>
				</div>
				<div class="single-product-add-cart-btn single-product-meta-btn">
					<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><span class="icon"><i class="fa-solid fa-user"></i></span> Add To Bag</button>
					<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
				</div>
				
			</div>
		</div>

</form>


<script type="text/javascript">
	jQuery(document).ready(function($) {
		// Function to update the Buy It Now link
		var selectedVariationId;
		function updateBuyNowLink() {
			selectedVariationId = $('.variations_form').find('input[name="variation_id"]').val();
			var quantity = $('input[name="quantity"]').val();
			
			if (selectedVariationId) {
				$('#single-product-buy-now').attr('href', '<?php echo $checkout_url ?>?add-to-cart=' + selectedVariationId + '&quantity=' + quantity);
			}

			if(selectedVariationId == 0){
				$('#single-product-buy-now').addClass("disabled");
			}else{
				$('#single-product-buy-now').removeClass("disabled");
			}
		}

		// Update the Buy It Now link when variation is selected
		$('.variations_form').on('show_variation', function(event, variation) {
			updateBuyNowLink();
		});

		// Update the Buy It Now link when variation is selected
		$('.variations_form').on('hide_variation', function(event, variation) {
			$('.variations_form').find('input[name="variation_id"]').val(0);
			// selectedVariationId = 0;
			updateBuyNowLink();
		});

		// Update the Buy It Now link when quantity is changed
		$('input[name="quantity"]').on('change', function() {
			updateBuyNowLink();
		});

		$('#single-product-buy-now').click((event) => {
			if(selectedVariationId == 0){
				event.preventDefault();
			}
		})
		

		// Initial setup to check if there's already a selected variation and quantity
		updateBuyNowLink();
	});
</script>

<?php
do_action( 'woocommerce_after_add_to_cart_form' );
