<?php
/**
 * Custom Wishlist page template - Card Grid Layout
 *
 * @author YITH <plugins@yithemes.com>
 * @package YITH\Wishlist\Templates\Wishlist\View
 * @version 3.0.0
 */

/**
 * Template variables:
 *
 * @var $wishlist                      \YITH_WCWL_Wishlist Current wishlist
 * @var $wishlist_items                array Array of items to show for current page
 * @var $wishlist_token                string Current wishlist token
 * @var $wishlist_id                   int Current wishlist id
 * @var $users_wishlists               array Array of current user wishlists
 * @var $pagination                    string yes/no
 * @var $per_page                      int Items per page
 * @var $current_page                  int Current page
 * @var $page_links                    array Array of page links
 * @var $is_user_owner                 bool Whether current user is wishlist owner
 * @var $show_price                    bool Whether to show price column
 * @var $show_dateadded                bool Whether to show item date of addition
 * @var $show_stock_status             bool Whether to show product stock status
 * @var $show_add_to_cart              bool Whether to show Add to Cart button
 * @var $show_remove_product           bool Whether to show Remove button
 * @var $show_price_variations         bool Whether to show price variation over time
 * @var $show_variation                bool Whether to show variation attributes when possible
 * @var $show_cb                       bool Whether to show checkbox column
 * @var $show_quantity                 bool Whether to show input quantity or not
 * @var $show_ask_estimate_button      bool Whether to show Ask an Estimate form
 * @var $show_last_column              bool Whether to show last column (calculated basing on previous flags)
 * @var $move_to_another_wishlist      bool Whether to show Move to another wishlist select
 * @var $move_to_another_wishlist_type string Whether to show a select or a popup for wishlist change
 * @var $additional_info               bool Whether to show Additional info textarea in Ask an estimate form
 * @var $price_excl_tax                bool Whether to show price excluding taxes
 * @var $enable_drag_n_drop            bool Whether to enable drag n drop feature
 * @var $repeat_remove_button          bool Whether to repeat remove button in last column
 * @var $available_multi_wishlist      bool Whether multi wishlist is enabled and available
 * @var $no_interactions               bool
 */

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

<!-- WISHLIST GRID -->
<div class="container">

  <div class="cs_height_50 cs_height_lg_40"></div>
  
  <?php if ( $wishlist && $wishlist->has_items() ) : ?>
    <div class="cs_grid_5_column">
      <?php
      foreach ( $wishlist_items as $item ) :
        global $product;
        $product = $item->get_product();
        
        if ( $product && $product->exists() ) :
          // Get product data
          $product_id = $item->get_product_id();
          $product_url = get_permalink(apply_filters('woocommerce_in_cart_product', $product_id));
          $product_image = $product->get_image();
          $product_title = apply_filters('woocommerce_in_cartproduct_obj_title', $product->get_title(), $product);
          $product_price = $item->get_formatted_product_price();
          $product_rating = $product->get_average_rating();
          $product_rating_count = $product->get_review_count();
          
          // Get product categories
          $product_cats = get_the_terms($product_id, 'product_cat');
          $cat_name = '';
          if ($product_cats && !is_wp_error($product_cats)) {
            $cat_name = $product_cats[0]->name;
          }
          
          // Calculate rating percentage
          $rating_percentage = ($product_rating / 5) * 100;
      ?>
        <div class="cs_grid_col" id="yith-wcwl-row-<?php echo esc_attr($item->get_product_id()); ?>" data-row-id="<?php echo esc_attr($item->get_product_id()); ?>">
          <div class="cs_product_card cs_style_1 cs_radius_12">
            <div class="cs_product_card_in">
              <div class="cs_product_thumb cs_center">
                <?php echo $product_image; ?>
                <div class="cs_products_btns cs_plr_25">
                  <?php if ($show_add_to_cart && $item->is_purchasable() && 'out-of-stock' !== $item->get_stock_status()) : ?>
                   <?php
                      do_action( 'woocommerce_after_shop_loop_item' );
                    ?>
                  <?php endif; ?>
                </div>
              </div>
              <div class="cs_product_info">
                <div class="cs_product_category text-uppercase cs_fs_14 cs_light">
                  <a href="<?php echo esc_url(get_term_link($product_cats[0]->term_id, 'product_cat')); ?>"><?php echo esc_html($cat_name); ?></a>
                </div>
                <h2 class="cs_product_title cs_fs_18 cs_normal cs_secondary_font"><?php echo esc_html($product_title); ?></h2>
                <p class="cs_product_price cs_fs_24 cs_primary_color cs_medium"><?php echo wp_kses_post($product_price); ?></p>
                
                <?php if ($product_rating > 0) : ?>
                <div class="cs_product_ratings">
                  <div class="cs_rating" data-rating="<?php echo esc_attr($product_rating); ?>">
                    <div class="cs_rating_percentage" style="width: <?php echo esc_attr($rating_percentage); ?>%;"></div>
                  </div>
                  <div class="cs_rating_number cs_ternary_color cs_fs_14 cs_light">
                    <span>(<?php echo esc_html($product_rating); ?>/5)</span>
                    <?php if ($product_rating_count > 0) : ?>
                      <span>| <?php echo esc_html($product_rating_count); ?> <?php esc_html_e('Reviews', 'glowify-wp');?></span>
                    <?php endif; ?>
                  </div>
                </div>
                <?php endif; ?>
                
                <?php if ($show_stock_status) : ?>
                  <div class="cs_stock_status">
                    <?php
                    $stock_status_html = 'out-of-stock' === $item->get_stock_status() 
                      ? '<span class="wishlist-out-of-stock">' . esc_html(apply_filters('yith_wcwl_out_of_stock_label', __('Out of stock', 'glowify-wp'))) . '</span>' 
                      : '<span class="wishlist-in-stock">' . esc_html(apply_filters('yith_wcwl_in_stock_label', __('In Stock', 'glowify-wp'))) . '</span>';
                    
                    echo apply_filters('yith_wcwl_stock_status', $stock_status_html, $item, $wishlist);
                    ?>
                  </div>
                <?php endif; ?>
              </div>
            </div>
            <a href="<?php echo esc_url($product_url); ?>" class="cs_product_link"></a>
          </div>
          
          <?php if ($show_remove_product) : ?>
            <a href="<?php echo esc_url($item->get_remove_url()); ?>" class="cs_card_remove_btn w-100 remove_from_wishlist">
              <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0.714286 3.99996H2.14286V17.6533C2.14474 18.2751 2.41024 18.871 2.88136 19.3107C3.35247 19.7504 3.99089 19.9982 4.65714 20H15.3714C16.0327 19.9912 16.6638 19.7403 17.1288 19.3014C17.5938 18.8624 17.8553 18.2705 17.8571 17.6533V3.99996H19.2857C19.4752 3.99996 19.6568 3.92972 19.7908 3.8047C19.9247 3.67967 20 3.5101 20 3.33329C20 3.15648 19.9247 2.98691 19.7908 2.86189C19.6568 2.73686 19.4752 2.66663 19.2857 2.66663H0.714286C0.524845 2.66663 0.343164 2.73686 0.209209 2.86189C0.0752549 2.98691 0 3.15648 0 3.33329C0 3.5101 0.0752549 3.67967 0.209209 3.8047C0.343164 3.92972 0.524845 3.99996 0.714286 3.99996ZM16.4286 3.99996V17.6533C16.4286 17.922 16.3142 18.1798 16.1106 18.3698C15.907 18.5599 15.6308 18.6666 15.3429 18.6666H4.62857C4.3456 18.6597 4.07673 18.5499 3.87926 18.3606C3.68179 18.1713 3.57133 17.9175 3.57143 17.6533V3.99996H16.4286Z" fill="currentColor"/>
                <path d="M7.14397 1.33333H12.8583C13.0477 1.33333 13.2294 1.2631 13.3633 1.13807C13.4973 1.01305 13.5725 0.843478 13.5725 0.666667C13.5725 0.489856 13.4973 0.320286 13.3633 0.195262C13.2294 0.0702379 13.0477 0 12.8583 0H7.14397C6.95453 0 6.77285 0.0702379 6.6389 0.195262C6.50494 0.320286 6.42969 0.489856 6.42969 0.666667C6.42969 0.843478 6.50494 1.01305 6.6389 1.13807C6.77285 1.2631 6.95453 1.33333 7.14397 1.33333Z" fill="currentColor"/>
                <path d="M7.62835 16C7.81779 16 7.99947 15.9297 8.13342 15.8047C8.26738 15.6797 8.34263 15.5101 8.34263 15.3333V7.33329C8.34263 7.15648 8.26738 6.98691 8.13342 6.86189C7.99947 6.73686 7.81779 6.66663 7.62835 6.66663C7.43891 6.66663 7.25723 6.73686 7.12327 6.86189C6.98932 6.98691 6.91406 7.15648 6.91406 7.33329V15.3333C6.91406 15.5101 6.98932 15.6797 7.12327 15.8047C7.25723 15.9297 7.43891 16 7.62835 16Z" fill="currentColor"/>
                <path d="M12.3705 16C12.56 16 12.7417 15.9297 12.8756 15.8047C13.0096 15.6797 13.0848 15.5101 13.0848 15.3333V7.33329C13.0848 7.15648 13.0096 6.98691 12.8756 6.86189C12.7417 6.73686 12.56 6.66663 12.3705 6.66663C12.1811 6.66663 11.9994 6.73686 11.8655 6.86189C11.7315 6.98691 11.6562 7.15648 11.6562 7.33329V15.3333C11.6562 15.5101 11.7315 15.6797 11.8655 15.8047C11.9994 15.9297 12.1811 16 12.3705 16Z" fill="currentColor"/>
              </svg>  
              <?php esc_html_e('REMOVE', 'glowify-wp');?>
            </a>
          <?php endif; ?>
        </div>
      <?php 
        endif;
      endforeach; 
      ?>
    </div>
  <?php else : ?>
    <div class="cs_empty_wishlist">
      <p class="cs_fs_20 cs_medium"><?php echo esc_html(apply_filters('yith_wcwl_no_product_to_remove_message', __('No products added to the wishlist', 'glowify-wp'), $wishlist)); ?></p>
      <div class="cs_height_30"></div>
      <a href="<?php echo esc_url(get_permalink(wc_get_page_id('shop'))); ?>" class="cs_btn cs_style_1 cs_medium">
        <span><?php esc_html_e('Continue Shopping', 'glowify-wp');?></span>
      </a>
    </div>
  <?php endif; ?>
  
  <?php if (!empty($page_links)) : ?>
    <div class="cs_height_50 cs_height_lg_40"></div>
    <div class="cs_pagination">
      <?php echo wp_kses_post($page_links); ?>
    </div>
  <?php endif; ?>
</div>