<?php
/**
 * Price tracker table.
 *
 * @var array $data Data for render table.
 *
 * @package xts
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
?>

<div class="xts-pt-content">

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

	<?php if ( $data ) : ?>
		<table class="xts-pt-table shop_table shop_table_responsive shop-table-with-img">
			<thead>
				<tr>			
					<th class="product-remove"></th>
					<th class="product-thumbnail"></th>
					<th class="product-name">
						<?php esc_html_e( 'Product', 'xts-theme' ); ?>
					</th>
					<th class="product-current-price">
						<?php esc_html_e( 'Current price', 'xts-theme' ); ?>
					</th>

					<?php if ( xts_get_opt( 'price_tracker_desired_price' ) ) : ?>
					<th class="product-desired-price">
						<?php esc_html_e( 'Desired price', 'xts-theme' ); ?>
					</th>
					<?php endif; ?>
				</tr>
			</thead>

			<tbody>
			<?php
			foreach ( $data as $subscription ) {
				$product_id   = $subscription->product_id;
				$variation_id = $subscription->variation_id ? $subscription->variation_id : 0;

				if ( defined( 'WCML_VERSION' ) && defined( 'ICL_SITEPRESS_VERSION' ) ) {
					$current_lang = apply_filters( 'wpml_current_language', null );

					$translated_product_id = apply_filters( 'wpml_object_id', $product_id, 'product', false, $current_lang );
					if ( false !== $translated_product_id ) {
						$product_id = $translated_product_id;
					}

					$translated_variation_id = apply_filters( 'wpml_object_id', $variation_id, 'product', false, $current_lang );
					if ( false !== $translated_variation_id ) {
						$variation_id = $translated_variation_id;
					}
				}

				$product = wc_get_product( $variation_id ? $variation_id : $product_id );

				if ( empty( $product ) ) {
					continue;
				}

				$desired_price = apply_filters( 'wcml_raw_price_amount', floatval( $subscription->desired_price ) );
				$product_link  = $product->get_permalink();
				$product_image = $product->get_image( 'shop_thumbnail' );
				$product_name  = $product->get_name();
				$attributes    = array();

				if ( 'variation' === $product->get_type() ) {
					foreach ( $product->get_attributes() as $taxonomy => $value ) {
						$attributes[] = array(
							'key'     => ucfirst( wc_attribute_label( $taxonomy ) ),
							'value'   => ucfirst( $value ),
							'display' => ucfirst( $value ),
						);
					}
				}

				if ( count( $attributes ) > 2 ) {
					$product_name = $product->get_title();
				}
				?>
				<tr>
					<td class="product-remove">
						<a href="#" class="xts-pt-remove" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-variation-id="<?php echo esc_attr( $variation_id ); ?>">
							&times;
						</a>
					</td>
					<td class="product-thumbnail">
						<a class="product-image" href="<?php echo esc_url( $product_link ); ?>">
							<?php echo wp_kses_post( $product_image ); ?>
						</a>
					</td>
					<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'xts-theme' ); ?>">
						<a class="product-title" href="<?php echo esc_url( $product_link ); ?>">
							<?php echo wp_kses_post( $product_name ); ?>
						</a>
						<?php if ( isset( $attributes ) && ! empty( $attributes ) && count( $attributes ) > 2 ) : ?>
							<?php wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $attributes ) ); ?>
						<?php endif; ?>
					</td>
					<td class="product-current-price" data-title="<?php esc_attr_e( 'Price', 'xts-theme' ); ?>">
						<p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>">
							<?php echo wp_kses_post( $product->get_price_html() ); ?>
						</p>
					</td>
					<?php if ( xts_get_opt( 'price_tracker_desired_price' ) ) : ?>
						<td class="product-desired-price" data-title="<?php esc_attr_e( 'Desired price', 'xts-theme' ); ?>">
							<?php if ( ! empty( $desired_price ) ) : ?>
								<?php echo wp_kses_post( wc_price( $desired_price ) ); ?>
							<?php else : ?>
								<span class="xts-cell-empty"></span>
							<?php endif; ?>

							<div class="xts-desired-price-opener xts-action-btn xts-style-icon">
								<a href="#" aria-label="<?php esc_attr_e( 'Edit desired price', 'xts-theme' ); ?>" title="<?php esc_attr_e( 'Edit desired price', 'xts-theme' ); ?>"></a>
							</div>

							<div class="xts-desired-price-edit xts-hide" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-variation-id="<?php echo esc_attr( $variation_id ); ?>">
								<input type="number" name="xts-desired-price-change" min="0" max="<?php echo esc_attr( $product->get_regular_price() ); ?>" value="<?php echo esc_attr( $desired_price ); ?>" data-title="<?php esc_attr_e( 'Edit desired price', 'xts-theme' ); ?>">

								<a href="#" class="btn xts-desired-price-save">
									<?php esc_html_e( 'Save', 'xts-theme' ); ?>
								</a>

								<div class="xts-desired-price-cancel xts-action-btn xts-cross-btn xts-style-icon">
									<a href="#" aria-label="<?php esc_attr_e( 'Cancel edit desired price', 'xts-theme' ); ?>"></a>
								</div>
							</div>
						</td>
					<?php endif; ?>
				</tr>
				<?php
			}
			?>
			</tbody>
		</table>

		<div class="xts-loader-overlay xts-fill"></div>

		<?php wc_get_template( 'loop/pagination.php', $paginate_args ); ?>
	<?php else : ?>
		<h4 class="xts-empty-pt xts-empty-page">
			<?php esc_html_e( 'Price tracker is empty.', 'xts-theme' ); ?>
	</h4>

		<div class="xts-empty-page-text">
			<?php echo wp_kses( __( 'Your price tracker is empty. Head over to the shop and start<br>tracking items to stay updated on discounts.', 'xts-theme' ), xts_get_allowed_html() ); ?>
		</div>

		<p class="return-to-shop">
			<a class="button" href="<?php echo esc_url( apply_filters( 'xts_price_tracker_return_to_shop_url', wc_get_page_permalink( 'shop' ) ) ); ?>">
				<?php esc_html_e( 'Return to shop', 'xts-theme' ); ?>
			</a>
		</p>
	<?php endif; ?>

	<?php do_action( 'xts_after_price_tracker_table' ); ?>
</div>
