<?php
/**
 * Wishlist page template
 *
 * @author Your Inspiration Themes
 * @package YITH WooCommerce Wishlist
 * @version 2.0.12
 */

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

<?php do_action( 'yith_wcwl_before_wishlist_form', $wishlist_meta ); ?>

<form id="yith-wcwl-form" action="<?php echo esc_url( YITH_WCWL()->get_wishlist_url( 'view' . ( $wishlist_meta['is_default'] != 1 ? '/' . $wishlist_meta['wishlist_token'] : '' ) ) ) ?>" method="post" class="woocommerce">

	<?php wp_nonce_field( 'yith-wcwl-form', 'yith_wcwl_form_nonce' ) ?>

	<!-- TITLE -->
	<?php
	do_action( 'yith_wcwl_before_wishlist_title' );

	if( ! empty( $page_title ) ) :
	?>
		<?php if( $wishlist_meta['is_default'] != 1 && $is_user_owner ): ?>
			<div class="hidden-title-form">
				<input type="text" value="<?php echo esc_attr( $page_title ); ?>" name="wishlist_name"/>
				<button>
					<?php echo apply_filters( 'yith_wcwl_save_wishlist_title_icon', '<i class="fa fa-check"></i>' )?>
					<?php esc_html_e( 'Save', 'stockie' )?>
				</button>
				<a class="hide-title-form btn button">
					<?php echo apply_filters( 'yith_wcwl_cancel_wishlist_title_icon', '<i class="fa fa-remove"></i>' )?>
					<?php esc_html_e( 'Cancel', 'stockie' )?>
				</a>
			</div>
		<?php endif; ?>
	<?php
	endif;

 	do_action( 'yith_wcwl_before_wishlist' ); ?>


	<div class="vc_row">
		<div class="woo-c_wishlist vc_col-lg-8 vc_col-lg-push-2 vc_col-md-10 vc_col-md-push-1">
			<div class="woo-c_cart_table" data-pagination="<?php echo esc_attr( $pagination )?>" data-per-page="<?php echo esc_attr( $per_page )?>" data-page="<?php echo esc_attr( $current_page )?>" data-id="<?php echo ( is_user_logged_in() ) ? esc_attr( $wishlist_meta['ID'] ) : '' ?>" data-token="<?php echo ( ! empty( $wishlist_meta['wishlist_token'] ) && is_user_logged_in() ) ? esc_attr( $wishlist_meta['wishlist_token'] ) : '' ?>">

				<?php $column_count = 2; ?>

				<?php
				if( count( $wishlist_items ) > 0 ) :
					foreach( $wishlist_items as $item ) :
						global $product;
						if( function_exists( 'wc_get_product' ) ) {
							$product = wc_get_product( $item['prod_id'] );
						}
						else{
							$product = get_product( $item['prod_id'] );
						}

						if( $product !== false && $product->exists() ) :
							$availability = $product->get_availability();
							$stock_status = $availability['class'];
							?>

							<div class="woo-c_cart_table_item" id="yith-wcwl-row-<?php echo esc_attr( $item['prod_id'] ); ?>" data-row-id="<?php echo esc_attr( $item['prod_id'] ); ?>">
								<?php if( $show_cb ) : ?>
									<div class="product-checkbox">
										<input type="checkbox" value="<?php echo esc_attr( $item['prod_id'] ) ?>" name="add_to_cart[]" <?php echo esc_html( $product->product_type != 'simple' ) ? 'disabled="disabled"' : '' ?>/>
									</div>
								<?php endif ?>

									<div class="woo-c_cart_table_item_thumbnail">
										<a href="<?php echo esc_url( get_permalink( apply_filters( 'woocommerce_in_cart_product', $item['prod_id'] ) ) ) ?>">
											<?php if ($product->get_image()): ?>
												<?php echo woocommerce_get_product_thumbnail() ?>
											<?php endif; ?>
										</a>
									</div>

								<div class="woo-c_cart_table_item_name">
									<div class="woo-c_product">
										<a href="<?php echo esc_url( get_permalink( apply_filters( 'woocommerce_in_cart_product', $item['prod_id'] ) ) ) ?>">	<?php echo apply_filters( 'woocommerce_in_cartproduct_obj_title', get_post( $product->get_id() )->post_title, $product ) ?>
										</a>
										<?php do_action( 'yith_wcwl_table_after_product_name', $item ); ?>

										<!-- Date added -->
										<?php
										if( $show_dateadded && isset( $item['dateadded'] ) ):
											echo '<div class="dateadded">' . sprintf( esc_html__( 'Added: %s', 'stockie'  ), date_i18n( get_option( 'date_format' ), strtotime( $item['dateadded'] ) ) ) . '</div>';
										endif;
										?>
									</div>
								</div>

								<?php if( $show_price ) : ?>
								<div class="woo-c_cart_table_item_price">
									<?php
									// print_r($product);
									if( is_a( $product, 'WC_Product_Variable' ) ){
										if( $product->get_variation_price('min') != $product->get_variation_price('max') ){
											echo sprintf( '%s - %s', wc_price( $product->get_variation_price('min') ), wc_price( $product->get_variation_price('max') ) );
										}
										else{
											echo wc_price( $product->get_variation_price('min') );
										}
									}
									elseif( $product->get_price() != '0' ) {
										echo wc_price( $product->get_price() );
									}
									else {
										echo apply_filters( 'yith_free_text', esc_html__( 'Free!', 'stockie'  ) );
									}
									?>
								</div>
								<?php endif ?>

								<?php if( $show_stock_status ) : ?>
								<div class="woo-c_cart_table_item_status">
									<?php
									if( $stock_status == 'out-of-stock' ) {
										$stock_status = "Out";
										echo '<span class="wishlist-out-of-stock">' . esc_html__( 'Out of Stock', 'stockie'  ) . '</span>';
									} else {
										$stock_status = "In";
										echo '<span class="wishlist-in-stock">' . esc_html__( 'In Stock', 'stockie'  ) . '</span>';
									}
									?>
								</div>
								<?php endif ?>

								<?php if( $show_last_column ): ?>
								<div class="product-add-to-cart">

									<!-- Add to cart button -->
									<?php if( $show_add_to_cart && isset( $stock_status ) && $stock_status != 'Out' ): ?>
										<?php
										if( function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
											woocommerce_template_loop_add_to_cart();
										}
										else{
											wc_get_template( 'loop/add-to-cart.php' );
										}
										?>
									<?php endif ?>

									<!-- Change wishlist -->
									<?php if( $available_multi_wishlist && is_user_logged_in() && count( $users_wishlists ) > 1 && $move_to_another_wishlist ): ?>
									<select class="change-wishlist selectBox">
										<option value=""><?php esc_html_e( 'Move', 'stockie'  ) ?></option>
										<?php
										foreach( $users_wishlists as $wl ):
											if( $wl['wishlist_token'] == $wishlist_meta['wishlist_token'] ){
												continue;
											}

										?>
											<option value="<?php echo esc_attr( $wl['wishlist_token'] ) ?>">
												<?php
												$wl_title = ! empty( $wl['wishlist_name'] ) ? $wl['wishlist_name'] : $default_wishlsit_title;
												if( $wl['wishlist_privacy'] == 1 ) {
													$wl_privacy = esc_html__( 'Shared', 'stockie'  );
												}
												elseif ( $wl['wishlist_privacy'] == 2 ) {
													$wl_privacy = esc_html__( 'Private', 'stockie'  );
												}
												else {
													$wl_privacy = esc_html__( 'Public', 'stockie'  );
												}

												echo sprintf( '%s - %s', $wl_title, $wl_privacy );
												?>
											</option>
										<?php
										endforeach;
										?>
									</select>
									<?php endif; ?>

									<!-- Remove from wishlist -->
									<?php if( $is_user_owner && $repeat_remove_button ): ?>
										<a href="<?php echo esc_url( add_query_arg( 'remove_from_wishlist', $item['prod_id'] ) ) ?>" class="remove_from_wishlist btn btn-small brand-bg-color brand-border-color" title="<?php esc_html_e( 'Remove this product', 'stockie'  ) ?>"><?php esc_html_e( 'Remove', 'stockie'  ) ?></a>
									<?php endif; ?>
								</div>

								<?php if( $is_user_owner ): ?>
								<div class="woo-c_cart_table_item_remove">
									<a href="<?php echo esc_url( add_query_arg( 'remove_from_wishlist', $item['prod_id'] ) ) ?>" class="remove-link" title="<?php esc_attr_e( 'Remove this product', 'stockie'  ) ?>"><i class="ion ion-md-close"></i></a>
								</div>
								<?php endif; ?>

							<?php endif; ?>
							</div>
						<?php
						endif;
					endforeach;
				else: ?>
				
					<!-- EMPT Container -->
					<div class="empt-container" style="width: 100%;">
						<div class="empt-container-image">
							<i class="ion ion-md-heart-empty"></i>
						</div>
						<h3 class="heading-md empt-container-headline">
							<?php esc_html_e( 'No products were added to the wishlist', 'stockie' ); ?>
						</h3><br>
						<div class="empt-container-cta">
							<a class="btn" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
								<?php esc_html_e( 'Go Shopping', 'stockie' ) ?> <i class="ion ion-right ion-ios-arrow-forward"></i>
							</a>
						</div>
					</div>

					</tr>
				<?php
				endif;

				if( ! empty( $page_links ) ) : ?>
					<tr class="pagination-row">
						<div><?php echo wp_kses( $page_links, 'default' ); ?></div>
					</tr>
				<?php endif ?>


				<div>
					<?php if( $show_cb ) : ?>
						<div class="custom-add-to-cart-button-cotaniner">
							<a href="<?php echo esc_url( add_query_arg( array( 'wishlist_products_to_add_to_cart' => '', 'wishlist_token' => $wishlist_meta['wishlist_token'] ) ) ) ?>" class="button alt" id="custom_add_to_cart"><?php echo apply_filters( 'yith_wcwl_custom_add_to_cart_text', esc_html__( 'Add the selected products to the cart', 'stockie'  ) ) ?></a>
						</div>
					<?php endif; ?>

					<?php if ( is_user_logged_in() && $is_user_owner && $show_ask_estimate_button && $count > 0 ): ?>
						<div class="ask-an-estimate-button-container">
							<a href="<?php echo esc_html( $additional_info ) ? '#ask_an_estimate_popup' : esc_url( $ask_estimate_url ) ?>" class="btn button ask-an-estimate-button" <?php echo asc_attr( $additional_info ) ? 'data-rel="prettyPhoto[ask_an_estimate]"' : '' ?> >
							<?php echo apply_filters( 'yith_wcwl_ask_an_estimate_icon', '<i class="fa fa-shopping-cart"></i>' )?>
							<?php echo apply_filters( 'yith_wcwl_ask_an_estimate_text', esc_html__( 'Ask for an estimate', 'stockie'  ) ) ?>
							</a>
						</div>
					<?php endif; ?>

					<?php do_action( 'yith_wcwl_before_wishlist_share' );

						if ( is_user_logged_in() && $is_user_owner && $wishlist_meta['wishlist_privacy'] != 2 && $share_enabled ){
							yith_wcwl_get_template( 'share.php', $share_atts );
						}

					do_action( 'yith_wcwl_after_wishlist_share' );?>
				</div>
			</div>
		</div>
	</div>

	<?php wp_nonce_field( 'yith_wcwl_edit_wishlist_action', 'yith_wcwl_edit_wishlist' ); ?>

	<?php if( $wishlist_meta['is_default'] != 1 ): ?>
		<input type="hidden" value="<?php echo esc_attr( $wishlist_meta['wishlist_token'] ); ?>" name="wishlist_id" id="wishlist_id">
	<?php endif; ?>

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

</form>

<?php do_action( 'yith_wcwl_after_wishlist_form', $wishlist_meta ); ?>

<?php if( $additional_info ): ?>
	<div id="ask_an_estimate_popup">
		<form action="<?php echo esc_url( $ask_estimate_url ); ?>" method="post" class="wishlist-ask-an-estimate-popup">
			<?php if( ! empty( $additional_info_label ) ):?>
				<label for="additional_notes"><?php echo wp_kses( $additional_info_label, 'default' ); ?></label>
			<?php endif; ?>
			<textarea id="additional_notes" name="additional_notes"></textarea>

			<button class="btn button ask-an-estimate-button ask-an-estimate-button-popup" >
				<?php echo apply_filters( 'yith_wcwl_ask_an_estimate_icon', '<i class="fa fa-shopping-cart"></i>' )?>
				<?php esc_html_e( 'Ask for an estimate', 'stockie'  ) ?>
			</button>
		</form>
	</div>
<?php endif; ?>