<?php
/**
 * Template for displaying product content in the single-product.php template
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-single-product.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.6.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

/**
 * Hook: woocommerce_before_single_product.
 *
 * @hooked woocommerce_output_all_notices - 10
 */
do_action( 'woocommerce_before_single_product' );

if ( post_password_required() ) {
	echo get_the_password_form(); // WPCS: XSS ok.
	return;
}
?>

<div id="product-<?php the_ID(); ?>" <?php wc_product_class( '', $product ); ?>>
	<?php
		/**
		 * Hook: woocommerce_before_single_product_summary.
		 *
		 * @hooked woocommerce_show_product_sale_flash - 10
		 * @hooked woocommerce_show_product_images - 20
		 */
		do_action( 'woocommerce_before_single_product_summary' );
	?>

	<div class="summary entry-summary">
		<?php 
			$user_id = get_current_user_id();
			$product_id = $product->get_id();
			$wishlist = get_user_meta($user_id, 'wishlist', true);
			$button_class = in_array($product_id, (array)$wishlist) ? 'added' : '';
			$nonce = wp_create_nonce('update_wishlist_' . $product_id);

			$wishlist_url = esc_url(add_query_arg(
			    array(
			        'update_wishlist' => $product_id,
			        '_wpnonce' => $nonce
			    )
			));
		?>
		<?php

			// Display product categories above the title
            echo '<div class="top-product-categories">';
            echo wc_get_product_category_list( $product->get_id(), ', ' );
            echo '</div>';


			// Display product title and short description
			//remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
			//remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
			// Remove related products from single product pages
			remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);

			// Add the title and short description
			do_action( 'woocommerce_single_product_summary' );

			// Display sale price first, then regular price
            // echo '<div class="product-price">';
            // if ( $product->is_on_sale() ) {
            //     // Sale price
            //     echo '<span class="sale-price">' . wc_price( $product->get_sale_price() ) . '</span>';
            //     // Regular price
            //     echo '<span class="regular-price">' . wc_price( $product->get_regular_price() ) . '</span>';
            // } 
            // else {
            //     // Regular price only
            //     echo '<span class="regular-price">' . wc_price( $product->get_regular_price() ) . '</span>';
            // }
            // echo '</div>';

            echo '<div class="product-price">';

				if ( $product->is_type('variable') ) {
				    // Get the minimum and maximum sale prices for variable products
				    $min_price = $product->get_variation_price('min', true);
				    $max_price = $product->get_variation_price('max', true);
				    $min_sale_price = $product->get_variation_sale_price('min', true);
				    $max_sale_price = $product->get_variation_sale_price('max', true);
				    $min_regular_price = $product->get_variation_regular_price('min', true);
				    $max_regular_price = $product->get_variation_regular_price('max', true);
				    
				    // Check if the variable product is on sale
				    if ( $product->is_on_sale() ) {
				        // Display sale price range
				        echo '<span class="sale-price">' . wc_price( $min_sale_price ) . ' - ' . wc_price( $max_sale_price ) . '</span>';
				        // Display regular price range
				       // echo '<span class="regular-price">' . wc_price( $min_regular_price ) . ' - ' . wc_price( $max_regular_price ) . '</span>';
				    } else {
				        // Display regular price range if not on sale
				        echo '<span class="sale-price">' . wc_price( $min_regular_price ) . ' - ' . wc_price( $max_regular_price ) . '</span>';
				    }
				} else {
				    // For simple products
				    if ( $product->is_on_sale() ) {
				        // Sale price
				        echo '<span class="sale-price">' . wc_price( $product->get_sale_price() ) . '</span>';
				        // Regular price
				        echo '<span class="regular-price">' . wc_price( $product->get_regular_price() ) . '</span>';
				    } 
				}

				echo '</div>';

			// Display product categories
			// echo '<div class="single-product-detials">';
			// $product_categories = wc_get_product_category_list( $product->get_id(), ', ', '<div class="product-categories"><strong>Categories: </strong>', '</div>' );
			// echo $product_categories;

			// // Display product tags
			// $product_tags = wc_get_product_tag_list( $product->get_id(), ', ', '<div class="product-tags"><strong>Tags: </strong> ', '</div>' );
			// echo $product_tags;

			$sku = $product->get_sku();
			if ( $sku ) {
			    echo '<div class="product-sku"><strong>SKU:</strong> ' . $sku . '</div>';
			}

			// Display product ID
			// echo '<div class="product-id"><strong>Product ID:</strong> ' . $product->get_id() . '</div>';
			// echo '</div>';

			// Display quantity input and add to cart button
			echo '<div class="product-btnarea">';
			echo '<div class="product-quantity">';
			woocommerce_template_single_add_to_cart(); // This includes quantity input and add-to-cart button
				//$buy_now_url = wc_get_cart_url() . '?add-to-cart=' . $product->get_id();
				//echo '<a href="' . esc_url($buy_now_url) . '" class="button buy-now-button">Buy Now</a>';

			echo '</div>';

			echo '<a href="' . $wishlist_url . '" class="add-to-wishlist-button ' . $button_class . '">
			    <span class="spinner"></span>
			    <i class="fa fa-heart' . ($button_class ? '' : '-o') . '" aria-hidden="true"></i>
			</a>'; 
			echo '</div>';
		?>
		<div class="accordion accordion-flush" id="accordionExample">
          	<?php
				$description = $product->get_description();
				if ( $description ) {
			?>
	          <div class="accordion-item">
	            <h2 class="accordion-header">
	              <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#coll1" aria-expanded="true" aria-controls="coll1">
	                <h5>Details</h5>
	              </button>
	            </h2>
	            <div id="coll1" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
	              <div class="accordion-body">
	               <?php echo $description; ?>
	              </div>
	            </div>
	          </div>
	        <?php } ?>

          	<?php
				$shipping_class_id = $product->get_shipping_class_id();
				if ( $shipping_class_id ) {
				    $shipping_class = get_term($shipping_class_id, 'product_shipping_class');
				    if ( $shipping_class ) {
			?>
	          <div class="accordion-item">
	            <h2 class="accordion-header">
	              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#coll2" aria-expanded="false" aria-controls="coll2">
	                <h5>Shipping</h5>
	              </button>
	            </h2>
	            <div id="coll2" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
	              <div class="accordion-body">
	                <?php
						echo '<div class="product-shipping"><strong>Shipping:</strong> ' . esc_html($shipping_class->name) . '</div>';
					    echo '<div class="product-shipping-slug"><strong>Shipping Slug:</strong> ' . esc_html($shipping_class->slug) . '</div>';
					    echo '<div class="product-shipping-description"><strong>Shipping Description:</strong> ' . esc_html($shipping_class->description) . '</div>';
					?>
	              </div>
	            </div>
	          </div>
         	<?php } } ?>
        </div>
	</div>

	<?php
		/**
		 * Hook: woocommerce_after_single_product_summary.
		 *
		 * @hooked woocommerce_output_product_data_tabs - 10
		 * @hooked woocommerce_upsell_display - 15
		 * @hooked woocommerce_output_related_products - 20
		 */
		do_action( 'woocommerce_after_single_product_summary' );
	?>
</div>

<?php do_action( 'woocommerce_after_single_product' ); ?>
