<?php
/**
 * قالب علاقه‌مندی‌ها Dima Shop
 * نسخه: 1.0.0
 */
if (!defined('ABSPATH')) exit;

$wishlist = Dima_Wishlist::get_instance();
$items = $wishlist->get_wishlist();
$currency = get_option('dima_currency', 'تومان');
$shop_url = get_permalink(get_option('dima_shop_page'));
?>

<div class="dima-wishlist-page" dir="rtl">
    
    <div class="wishlist-header">
        <h1>❤️ علاقه‌مندی‌های من</h1>
        <span class="wishlist-count"><?php echo count($items); ?> محصول</span>
    </div>
    
    <?php if (empty($items)): ?>
        <div class="wishlist-empty">
            <div class="empty-icon">💔</div>
            <h2>لیست علاقه‌مندی‌های شما خالی است</h2>
            <p>محصولات مورد علاقه خود را به این لیست اضافه کنید</p>
            <a href="<?php echo esc_url($shop_url); ?>" class="btn btn-primary btn-large">
                🛍️ مشاهده محصولات
            </a>
        </div>
    <?php else: ?>
        <div class="wishlist-grid">
            <?php foreach ($items as $product_id): 
                $product = get_post($product_id);
                if (!$product || $product->post_status !== 'publish') continue;
                
                $price = Dima_Product::get_price($product_id);
                $stock = Dima_Product::get_stock($product_id);
                $is_in_stock = ($stock === '' || intval($stock) > 0);
            ?>
            <div class="wishlist-card" data-product-id="<?php echo esc_attr($product_id); ?>">
                <button class="remove-wishlist-btn" data-product-id="<?php echo esc_attr($product_id); ?>" title="حذف از علاقه‌مندی‌ها">
                    ✕
                </button>
                
                <a href="<?php echo esc_url(get_permalink($product_id)); ?>" class="wishlist-image">
                    <?php if (has_post_thumbnail($product_id)): ?>
                        <?php the_post_thumbnail($product_id, 'medium'); ?>
                    <?php else: ?>
                        <div class="no-image">📦</div>
                    <?php endif; ?>
                </a>
                
                <div class="wishlist-info">
                    <h3 class="wishlist-title">
                        <a href="<?php echo esc_url(get_permalink($product_id)); ?>">
                            <?php echo esc_html($product->post_title); ?>
                        </a>
                    </h3>
                    
                    <div class="wishlist-price">
                        <span class="price-value"><?php echo number_format($price); ?></span>
                        <span class="price-currency"><?php echo esc_html($currency); ?></span>
                    </div>
                    
                    <?php if ($is_in_stock): ?>
                        <button class="btn btn-primary btn-small add-to-cart-btn" data-product-id="<?php echo esc_attr($product_id); ?>">
                            🛒 افزودن به سبد
                        </button>
                    <?php else: ?>
                        <button class="btn btn-disabled btn-small" disabled>ناموجود</button>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
</div>

<style>
.dima-wishlist-page {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
    font-family: 'Vazir', Tahoma, sans-serif;
}

.wishlist-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e5e7eb;
}

.wishlist-header h1 {
    font-size: 28px;
    color: #1f2937;
    margin: 0;
}

.wishlist-count {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 700;
}

.wishlist-empty {
    text-align: center;
    padding: 80px 20px;
    background: white;
    border-radius: 20px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.06);
}

.wishlist-empty .empty-icon {
    font-size: 80px;
    margin-bottom: 20px;
    opacity: 0.6;
}

.wishlist-empty h2 {
    font-size: 24px;
    color: #1f2937;
    margin-bottom: 10px;
}

.wishlist-empty p {
    color: #6b7280;
    margin-bottom: 30px;
}

.wishlist-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
}

.wishlist-card {
    background: white;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    border: 1px solid #e5e7eb;
    position: relative;
    transition: all 0.3s;
}

.wishlist-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.12);
}

.remove-wishlist-btn {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
    color: #ef4444;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    transition: all 0.2s;
    z-index: 10;
}

.remove-wishlist-btn:hover {
    background: #ef4444;
    color: white;
    transform: scale(1.1);
}

.wishlist-image {
    display: block;
    aspect-ratio: 1/1;
    overflow: hidden;
    background: #f9fafb;
}

.wishlist-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.wishlist-card:hover .wishlist-image img {
    transform: scale(1.05);
}

.no-image {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 60px;
}

.wishlist-info {
    padding: 15px;
}

.wishlist-title {
    font-size: 15px;
    margin: 0 0 10px 0;
    line-height: 1.4;
}

.wishlist-title a {
    color: #1f2937;
    text-decoration: none;
}

.wishlist-title a:hover {
    color: #6366f1;
}

.wishlist-price {
    margin-bottom: 12px;
}

.price-value {
    font-size: 18px;
    font-weight: 800;
    color: #6366f1;
}

.price-currency {
    font-size: 12px;
    color: #6b7280;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 10px;
    font-family: inherit;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    border: none;
    text-decoration: none;
    transition: all 0.2s;
}

.btn-primary {
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
}

.btn-small {
    padding: 8px 16px;
    font-size: 13px;
    width: 100%;
}

.btn-disabled {
    background: #e5e7eb;
    color: #9ca3af;
    cursor: not-allowed;
}

.btn-large {
    padding: 14px 28px;
    font-size: 16px;
}
</style>

<script>
jQuery(document).ready(function($) {
    // حذف از علاقه‌مندی
    $(document).on('click', '.remove-wishlist-btn', function(e) {
        e.preventDefault();
        var productId = $(this).data('product-id');
        var $card = $(this).closest('.wishlist-card');
        
        if (!confirm('حذف این محصول از علاقه‌مندی‌ها؟')) return;
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: {
                action: 'dima_toggle_wishlist',
                product_id: productId,
                nonce: dima_shop.nonce
            },
            success: function(response) {
                if (response.success) {
                    $card.fadeOut(300, function() {
                        $(this).remove();
                        if ($('.wishlist-card').length === 0) {
                            location.reload();
                        }
                    });
                    $('.wishlist-count').text(response.data.count + ' محصول');
                }
            }
        });
    });
    
    // افزودن به سبد
    $(document).on('click', '.add-to-cart-btn', function(e) {
        e.preventDefault();
        var $btn = $(this);
        var productId = $btn.data('product-id');
        var originalText = $btn.html();
        
        $btn.prop('disabled', true).html('⏳ در حال افزودن...');
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: {
                action: 'dima_add_to_cart',
                product_id: productId,
                quantity: 1,
                nonce: dima_shop.nonce
            },
            success: function(response) {
                if (response.success) {
                    $btn.html('✓ اضافه شد!');
                    $('.cart-count-badge').text(response.data.cart_count);
                    setTimeout(function() {
                        $btn.html(originalText).prop('disabled', false);
                    }, 2000);
                }
            }
        });
    });
});
</script>