<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Name: Wishlist Page
 * 
 * @package Pawfect
 */

get_header(); ?>

<main id="primary-content" class="pawfect-main-content pawfect-bg-background pawfect-min-h-screen" tabindex="-1">
    
    <?php
// Page header
if (!get_theme_mod('pawfect_page_header_disabled', false)) {
    get_template_part('template-parts/page-header');
}
?>

    <div class="pawfect-container pawfect-mx-auto pawfect-px-4 pawfect-py-12">
        
        <?php
$wishlist_items = pawfect_get_wishlist_items();
$wishlist_count = count($wishlist_items);
?>

        <div class="pawfect-max-w-6xl pawfect-mx-auto">
            
            <!-- Wishlist Header -->
            <div class="pawfect-text-center pawfect-mb-12">
                <h1 class="pawfect-text-3xl md:pawfect-text-4xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                    <?php esc_html_e('My Wishlist', 'pawfect'); ?>
                </h1>
                <p class="pawfect-text-gray-600 pawfect-text-lg">
                    <?php
if ($wishlist_count > 0) {
    printf(esc_html(_n('You have %d item in your wishlist', 'You have %d items in your wishlist', $wishlist_count, 'pawfect')), $wishlist_count);
}
else {
    esc_html_e('Your wishlist is empty', 'pawfect');
}
?>
                </p>
            </div>

            <?php if (!empty($wishlist_items)): ?>
                
                <!-- Wishlist Items Table -->
                <div class="pawfect-bg-white pawfect-rounded-lg pawfect-shadow-md pawfect-overflow-hidden pawfect-mb-12">
                    <div class="pawfect-overflow-x-auto">
                        <table class="pawfect-w-full pawfect-table-auto">
                            <thead class="pawfect-bg-gray-50 pawfect-border-b pawfect-border-gray-200">
                                <tr>
                                    <th class="pawfect-px-6 pawfect-py-4 pawfect-text-left pawfect-text-sm pawfect-font-semibold pawfect-text-gray-900 pawfect-uppercase pawfect-tracking-wider">
                                        <?php esc_html_e('Product Image', 'pawfect'); ?>
                                    </th>
                                    <th class="pawfect-px-6 pawfect-py-4 pawfect-text-left pawfect-text-sm pawfect-font-semibold pawfect-text-gray-900 pawfect-uppercase pawfect-tracking-wider">
                                        <?php esc_html_e('Product Name', 'pawfect'); ?>
                                    </th>
                                    <th class="pawfect-px-6 pawfect-py-4 pawfect-text-left pawfect-text-sm pawfect-font-semibold pawfect-text-gray-900 pawfect-uppercase pawfect-tracking-wider">
                                        <?php esc_html_e('Unit Price', 'pawfect'); ?>
                                    </th>
                                    <th class="pawfect-px-6 pawfect-py-4 pawfect-text-left pawfect-text-sm pawfect-font-semibold pawfect-text-gray-900 pawfect-uppercase pawfect-tracking-wider">
                                        <?php esc_html_e('Stock Status', 'pawfect'); ?>
                                    </th>
                                    <th class="pawfect-px-6 pawfect-py-4 pawfect-text-center pawfect-text-sm pawfect-font-semibold pawfect-text-gray-900 pawfect-uppercase pawfect-tracking-wider">
                                        <?php esc_html_e('Action', 'pawfect'); ?>
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="pawfect-bg-white pawfect-divide-y pawfect-divide-gray-200">
                                <?php foreach ($wishlist_items as $product_id):
        $product = wc_get_product($product_id);
        if (!$product || !$product->is_visible())
            continue;

        $product_image = $product->get_image('thumbnail');
        $product_name = $product->get_name();
        $product_price = $product->get_price_html();
        $product_url = $product->get_permalink();
        $is_in_stock = $product->is_in_stock();
?>

                                    <tr class="pawfect-wishlist-item pawfect-hover:pawfect-bg-gray-50 pawfect-transition-colors">
                                        <!-- Product Image -->
                                        <td class="pawfect-px-6 pawfect-py-4 pawfect-whitespace-nowrap">
                                            <div class="pawfect-relative pawfect-w-16 pawfect-h-16 pawfect-rounded-lg pawfect-overflow-hidden pawfect-bg-gray-100">
                                                <a href="<?php echo esc_url($product_url); ?>">
                                                    <?php echo wp_kses_post($product_image); ?>
                                                </a>
                                                <!-- Remove Button -->
                                                <button class="pawfect-wishlist-btn pawfect-wishlist-active pawfect-absolute pawfect--top-2 pawfect--right-2 pawfect-bg-red-500 pawfect-text-white pawfect-rounded-full pawfect-w-6 pawfect-h-6 pawfect-flex pawfect-items-center pawfect-justify-center pawfect-text-xs pawfect-opacity-0 pawfect-transition-opacity hover:pawfect-opacity-100"
                                                        data-product-id="<?php echo esc_attr($product_id); ?>"
                                                        title="<?php esc_attr_e('Remove from wishlist', 'pawfect'); ?>">
                                                    ×
                                                </button>
                                            </div>
                                        </td>

                                        <!-- Product Name -->
                                        <td class="pawfect-px-6 pawfect-py-4">
                                            <div class="pawfect-text-sm pawfect-font-medium pawfect-text-gray-900">
                                                <a href="<?php echo esc_url($product_url); ?>" class="hover:pawfect-text-pawfect-primary pawfect-transition-colors">
                                                    <?php echo esc_html($product_name); ?>
                                                </a>
                                            </div>
                                        </td>

                                        <!-- Unit Price -->
                                        <td class="pawfect-px-6 pawfect-py-4 pawfect-whitespace-nowrap">
                                            <div class="pawfect-text-sm pawfect-font-semibold pawfect-text-pawfect-primary">
                                                <?php echo wp_kses_post($product_price); ?>
                                            </div>
                                        </td>

                                        <!-- Stock Status -->
                                        <td class="pawfect-px-6 pawfect-py-4 pawfect-whitespace-nowrap">
                                            <?php if ($is_in_stock): ?>
                                                <span class="pawfect-inline-flex pawfect-items-center pawfect-px-2.5 pawfect-py-0.5 pawfect-rounded-full pawfect-text-xs pawfect-font-medium pawfect-bg-green-100 pawfect-text-green-800">
                                                    <?php esc_html_e('In Stock', 'pawfect'); ?>
                                                </span>
                                            <?php
        else: ?>
                                                <span class="pawfect-inline-flex pawfect-items-center pawfect-px-2.5 pawfect-py-0.5 pawfect-rounded-full pawfect-text-xs pawfect-font-medium pawfect-bg-red-100 pawfect-text-red-800">
                                                    <?php esc_html_e('Out of Stock', 'pawfect'); ?>
                                                </span>
                                            <?php
        endif; ?>
                                        </td>

                                        <!-- Action -->
                                        <td class="pawfect-px-6 pawfect-py-4 pawfect-whitespace-nowrap pawfect-text-center">
                                            <?php if ($is_in_stock): ?>
                                                <a href="<?php echo esc_url($product_url); ?>" class="pawfect-btn">
                                                    <?php esc_html_e('Shop Now', 'pawfect'); ?>
                                                </a>
                                            <?php
        else: ?>
                                                <span class="pawfect-inline-flex pawfect-items-center pawfect-px-4 pawfect-py-2 pawfect-bg-gray-300 pawfect-text-gray-500 pawfect-text-sm pawfect-font-medium pawfect-rounded-md pawfect-cursor-not-allowed">
                                                    <?php esc_html_e('Out of Stock', 'pawfect'); ?>
                                                </span>
                                            <?php
        endif; ?>
                                        </td>
                                    </tr>

                                <?php
    endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Continue Shopping -->
                <div class="pawfect-text-center">
                    <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="pawfect-btn">
                        <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16l-4-4m0 0l4-4m-4 4h18"></path>
                        </svg>
                        <?php esc_html_e('Continue Shopping', 'pawfect'); ?>
                    </a>
                </div>

            <?php
else: ?>
                
                <!-- Empty Wishlist -->
                <div class="pawfect-text-center pawfect-py-16">
                    <div class="pawfect-max-w-md pawfect-mx-auto">
                        <svg class="pawfect-w-24 pawfect-h-24 pawfect-mx-auto pawfect-mb-6 pawfect-text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                        </svg>
                        
                        <h2 class="pawfect-text-2xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                            <?php esc_html_e('Your wishlist is empty', 'pawfect'); ?>
                        </h2>
                        
                        <p class="pawfect-text-gray-600 pawfect-mb-8">
                            <?php esc_html_e('Start adding products you love to your wishlist by clicking the heart icon on any product.', 'pawfect'); ?>
                        </p>
                        
                        <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="pawfect-btn">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l-1 12H6L5 9z"></path>
                            </svg>
                            <?php esc_html_e('Start Shopping', 'pawfect'); ?>
                        </a>
                    </div>
                </div>
                
            <?php
endif; ?>
        </div>
    </div>
</main>

<?php get_footer(); ?>
