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

$compare = Dima_Compare::get_instance();
$products = $compare->get_comparison_data();
$currency = get_option('dima_currency', 'تومان');
?>

<div class="dima-compare-page" dir="rtl">
    
    <div class="compare-header">
        <h1>⚖️ مقایسه محصولات</h1>
        <span class="compare-count"><?php echo count($products); ?> محصول</span>
    </div>
    
    <?php if (empty($products)): ?>
        <div class="compare-empty">
            <div class="empty-icon">⚖️</div>
            <h2>لیست مقایسه خالی است</h2>
            <p>محصولات مورد نظر خود را برای مقایسه اضافه کنید</p>
            <a href="<?php echo esc_url(get_permalink(get_option('dima_shop_page'))); ?>" class="btn btn-primary btn-large">
                ️ مشاهده محصولات
            </a>
        </div>
    <?php else: ?>
        <div class="compare-table-wrapper">
            <table class="compare-table">
                <thead>
                    <tr>
                        <th class="feature-col">ویژگی</th>
                        <?php foreach ($products as $product): ?>
                        <th class="product-col">
                            <button class="remove-compare-btn" data-product-id="<?php echo esc_attr($product['id']); ?>" title="حذف">
                                ✕
                            </button>
                            <a href="<?php echo esc_url($product['url']); ?>" class="compare-product-image">
                                <?php if ($product['image']): ?>
                                    <img src="<?php echo esc_url($product['image']); ?>" alt="<?php echo esc_attr($product['title']); ?>">
                                <?php else: ?>
                                    <div class="no-image"></div>
                                <?php endif; ?>
                            </a>
                            <h3 class="compare-product-title">
                                <a href="<?php echo esc_url($product['url']); ?>">
                                    <?php echo esc_html($product['title']); ?>
                                </a>
                            </h3>
                            <div class="compare-product-price">
                                <?php echo number_format($product['price']); ?> <?php echo esc_html($currency); ?>
                            </div>
                            <?php if ($product['stock'] !== '' && intval($product['stock']) > 0): ?>
                                <span class="stock-badge in-stock">موجود (<?php echo intval($product['stock']); ?>)</span>
                            <?php else: ?>
                                <span class="stock-badge out-of-stock">ناموجود</span>
                            <?php endif; ?>
                        </th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <!-- قیمت -->
                    <tr>
                        <td class="feature-label">💰 قیمت</td>
                        <?php foreach ($products as $product): ?>
                        <td class="feature-value price-value">
                            <?php echo number_format($product['price']); ?> <?php echo esc_html($currency); ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    
                    <!-- موجودی -->
                    <tr>
                        <td class="feature-label">📦 موجودی</td>
                        <?php foreach ($products as $product): ?>
                        <td class="feature-value">
                            <?php if ($product['stock'] === ''): ?>
                                <span class="stock-text in-stock">نامحدود</span>
                            <?php elseif (intval($product['stock']) > 0): ?>
                                <span class="stock-text in-stock"><?php echo intval($product['stock']); ?> عدد</span>
                            <?php else: ?>
                                <span class="stock-text out-of-stock">ناموجود</span>
                            <?php endif; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    
                    <!-- امتیاز -->
                    <?php if (array_sum(array_column($products, 'rating')) > 0): ?>
                    <tr>
                        <td class="feature-label">⭐ امتیاز</td>
                        <?php foreach ($products as $product): ?>
                        <td class="feature-value">
                            <?php if ($product['rating']): ?>
                                <div class="rating-display">
                                    <span class="rating-value"><?php echo esc_html($product['rating']); ?></span>
                                    <span class="rating-stars">
                                        <?php for ($i = 1; $i <= 5; $i++): ?>
                                            <span class="star <?php echo $i <= round($product['rating']) ? 'filled' : ''; ?>">★</span>
                                        <?php endfor; ?>
                                    </span>
                                </div>
                            <?php else: ?>
                                <span class="no-rating">بدون امتیاز</span>
                            <?php endif; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endif; ?>
                    
                    <!-- SKU -->
                    <?php if (array_filter(array_column($products, 'sku'))): ?>
                    <tr>
                        <td class="feature-label">🔖 کد محصول</td>
                        <?php foreach ($products as $product): ?>
                        <td class="feature-value">
                            <?php echo $product['sku'] ? '<code>' . esc_html($product['sku']) . '</code>' : '-'; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endif; ?>
                    
                    <!-- توضیحات -->
                    <?php if (array_filter(array_column($products, 'description'))): ?>
                    <tr>
                        <td class="feature-label">📝 توضیحات</td>
                        <?php foreach ($products as $product): ?>
                        <td class="feature-value description-value">
                            <?php echo $product['description'] ? esc_html($product['description']) : '-'; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endif; ?>
                    
                    <!-- ویژگی‌ها -->
                    <?php 
                    $all_attributes = array();
                    foreach ($products as $product) {
                        foreach ($product['attributes'] as $attr) {
                            $all_attributes[$attr['id']] = $attr['name'];
                        }
                    }
                    if (!empty($all_attributes)):
                    ?>
                    <?php foreach ($all_attributes as $attr_id => $attr_name): ?>
                    <tr>
                        <td class="feature-label">✓ <?php echo esc_html($attr_name); ?></td>
                        <?php foreach ($products as $product): 
                            $has_attr = in_array($attr_id, array_column($product['attributes'], 'id'));
                        ?>
                        <td class="feature-value">
                            <?php echo $has_attr ? '<span class="check-yes">✓</span>' : '<span class="check-no">✗</span>'; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                    
                    <!-- اکشن -->
                    <tr class="action-row">
                        <td class="feature-label">🛒 اقدام</td>
                        <?php foreach ($products as $product): 
                            $is_in_stock = ($product['stock'] === '' || intval($product['stock']) > 0);
                        ?>
                        <td class="feature-value">
                            <?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; ?>
                        </td>
                        <?php endforeach; ?>
                    </tr>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
    
</div>

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

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

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

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

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

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

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

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

.compare-table-wrapper {
    overflow-x: auto;
    background: white;
    border-radius: 16px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}

.compare-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 800px;
}

.compare-table th,
.compare-table td {
    padding: 20px;
    text-align: center;
    border-bottom: 1px solid #f3f4f6;
    vertical-align: middle;
}

.feature-col, .feature-label {
    background: #f9fafb;
    font-weight: 700;
    color: #374151;
    text-align: right;
    width: 150px;
    position: sticky;
    right: 0;
    z-index: 10;
}

.product-col {
    background: white;
    min-width: 200px;
}

.remove-compare-btn {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #fee2e2;
    border: none;
    cursor: pointer;
    color: #ef4444;
    font-size: 14px;
    transition: all 0.2s;
}

.remove-compare-btn:hover {
    background: #ef4444;
    color: white;
}

.compare-product-image {
    display: block;
    margin: 0 auto 15px;
    width: 150px;
    height: 150px;
    border-radius: 12px;
    overflow: hidden;
    background: #f9fafb;
}

.compare-product-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

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

.compare-product-title {
    font-size: 16px;
    margin: 0 0 10px 0;
}

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

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

.compare-product-price {
    font-size: 20px;
    font-weight: 800;
    color: #6366f1;
    margin-bottom: 10px;
}

.stock-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.in-stock {
    background: #d1fae5;
    color: #065f46;
}

.out-of-stock {
    background: #fee2e2;
    color: #991b1b;
}

.feature-value {
    font-size: 14px;
    color: #4b5563;
}

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

.stock-text {
    font-weight: 600;
}

.rating-display {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.rating-value {
    font-size: 20px;
    font-weight: 800;
    color: #fbbf24;
}

.rating-stars {
    display: flex;
    gap: 2px;
    justify-content: center;
    color: #e5e7eb;
}

.rating-stars .star.filled {
    color: #fbbf24;
}

.no-rating {
    color: #9ca3af;
    font-size: 13px;
}

.description-value {
    text-align: right;
    line-height: 1.6;
    font-size: 13px;
}

.check-yes {
    color: #10b981;
    font-size: 20px;
    font-weight: 700;
}

.check-no {
    color: #ef4444;
    font-size: 20px;
    font-weight: 700;
}

.action-row td {
    background: #f9fafb;
}

.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;
}

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

.btn-large {
    padding: 14px 28px;
    font-size: 16px;
}

code {
    background: #f3f4f6;
    padding: 2px 8px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 13px;
    color: #6366f1;
}
</style>

<script>
jQuery(document).ready(function($) {
    // حذف از مقایسه
    $(document).on('click', '.remove-compare-btn', function(e) {
        e.preventDefault();
        var productId = $(this).data('product-id');
        
        if (!confirm('حذف این محصول از لیست مقایسه؟')) return;
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: {
                action: 'dima_remove_compare',
                product_id: productId,
                nonce: dima_shop.nonce
            },
            success: function(response) {
                if (response.success) {
                    location.reload();
                }
            }
        });
    });
    
    // افزودن به سبد
    $(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>