<?php
/**
 * Compare Table Template
 * 
 * This template can be overridden by copying it to yourtheme/product-compare-kav/compare-table.php
 */

if (!defined('ABSPATH')) {
    exit;
}

$compare_list = $this->get_compare_list();
$products_data = array();

// Get all products data
foreach ($compare_list as $product_id) {
    $product = wc_get_product($product_id);
    if ($product) {
        $products_data[] = $this->get_product_data($product);
    }
}

// Get all unique attributes
$all_attributes = array();
foreach ($products_data as $product_data) {
    $all_attributes = array_merge($all_attributes, array_keys($product_data['attributes']));
}
$all_attributes = array_unique($all_attributes);
?>

<div class="app">
    <div class="compare-kav">
        <div class="compare-kav__container">
            <!-- Start Heading -->
            <div class="compare-kav__heading compare-kav__flex compare-kav__items-center">
                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M16 12.9v4.2c0 3.5-1.4 4.9-4.9 4.9H6.9C3.4 22 2 20.6 2 17.1v-4.2C2 9.4 3.4 8 6.9 8h4.2c3.5 0 4.9 1.4 4.9 4.9Z"/>
                    <path fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M22 6.9v4.2c0 3.5-1.4 4.9-4.9 4.9H16v-3.1C16 9.4 14.6 8 11.1 8H8V6.9C8 3.4 9.4 2 12.9 2h4.2C20.6 2 22 3.4 22 6.9Z"/>
                </svg>
                <?php _e('مقایسه محصولات', 'kavehTheme'); ?>
            </div>
            <!-- End Heading -->
            
            <div id="compares" class="compare-kav__compares compare-kav__flex">
                <?php if (!empty($products_data)) : ?>
                    <?php foreach ($products_data as $index => $product_data) : ?>
                        <!-- Start Product Compare -->
                        <div class="compare-kav__compares__item compare-kav__relative" data-product-id="<?php echo esc_attr($product_data['id']); ?>">
                            <!-- Start Info Product -->
                            <div class="compare-kav__compares__item__product">
                                <button type="button" class="remove compare-kav__absolute">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
                                        <path fill="#E5E8F3" stroke="#E5E8F3" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 22c5.5 0 10-4.5 10-10S17.5 2 12 2 2 6.5 2 12s4.5 10 10 10Z"/>
                                        <path stroke="#9397AF" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m9.17 14.83 5.66-5.66m0 5.66L9.17 9.17"/>
                                    </svg>
                                </button>
                                
                                <?php if ($product_data['image']) : ?>
                                    <img src="<?php echo esc_url($product_data['image']); ?>" alt="<?php echo esc_attr($product_data['title']); ?>" width="150" height="150">
                                <?php else : ?>
                                    <div class="no-image" style="width: 150px; height: 150px; background: #f5f5f5; display: flex; align-items: center; justify-content: center;">
                                        <?php _e('No Image', 'kavehTheme'); ?>
                                    </div>
                                <?php endif; ?>
                                
                                <h2 class="title"><?php echo esc_html($product_data['title']); ?></h2>
                                
                                <div class="compare-kav__flex compare-kav__items-center compare-kav__justify-between">
                                    <ul class="sizes compare-kav__flex">
                                        <li><?php _e('محصول', 'kavehTheme'); ?></li>
                                    </ul>
                                    <div class="rate compare-kav__flex">
                                        <?php echo number_format($product_data['rating'], 1); ?>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" viewBox="0 0 16 16">
                                            <path fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="m9.153 2.34 1.174 2.347c.16.326.586.64.946.7l2.127.353c1.36.227 1.68 1.213.7 2.187L12.447 9.58c-.28.28-.434.82-.347 1.207l.473 2.046c.374 1.62-.486 2.247-1.92 1.4l-1.993-1.18c-.36-.213-.953-.213-1.32 0l-1.993 1.18c-1.427.847-2.294.214-1.92-1.4l.473-2.046c.087-.387-.067-.927-.347-1.207L1.9 7.927c-.973-.974-.66-1.96.7-2.187l2.127-.353c.353-.06.78-.374.94-.7L6.84 2.34c.64-1.273 1.68-1.273 2.313 0Z"/>
                                        </svg>
                                    </div>
                                </div>
                                
                                <div class="price-view compare-kav__flex compare-kav__items-center compare-kav__justify-between">
                                    <div class="price">
                                        <?php echo $product->get_price_html(); ?>
                                    </div>
                                    <a href="<?php echo esc_url($product_data['url']); ?>" class="compare-kav__btn compare-kav__btn__primary compare-kav__flex compare-kav__items-center compare-kav__justify-center">
                                        <?php _e('مشاهده و خرید', 'kavehTheme'); ?>
                                    </a>
                                </div>
                            </div>
                            <!-- End Info Product --> 
                            
                            <div class="total-info compare-kav__flex">
                                <?php foreach ($all_attributes as $attribute_name) : ?>
                                    <div class="label compare-kav__relative">
                                        <?php echo ($index === 0) ? esc_html($attribute_name) : '&nbsp;'; ?>
                                    </div>
                                    <div class="value">
                                        <?php echo isset($product_data['attributes'][$attribute_name]) ? esc_html($product_data['attributes'][$attribute_name]) : '-'; ?>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <!-- End Product Compare -->
                    <?php endforeach; ?>
                <?php endif; ?>
                
                <!-- Start Add Product -->
                <div class="compare-kav__compares__add__product compare-kav__relative">
                    <div class="box-add compare-kav__flex compare-kav__items-center compare-kav__justify-center">
                        <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" fill="none" viewBox="0 0 64 64">
                            <path stroke="currentColor" stroke-linecap="round" stroke-width="4" d="M38.663 17.334h8m0 0h8m-8 0v8m0-8v-8"/>
                            <path fill="currentColor" stroke="currentColor" stroke-width="4" d="M6.662 17.333c0-5.028 0-7.542 1.563-9.104 1.562-1.563 4.076-1.563 9.104-1.563 5.029 0 7.543 0 9.105 1.563 1.562 1.562 1.562 4.076 1.562 9.104s0 7.543-1.562 9.105C24.872 28 22.358 28 17.329 28c-5.028 0-7.542 0-9.104-1.562-1.563-1.563-1.563-4.077-1.563-9.105ZM36 46.667c0-5.029 0-7.543 1.562-9.105C39.124 36 41.638 36 46.667 36c5.028 0 7.542 0 9.104 1.562 1.562 1.562 1.562 4.076 1.562 9.105 0 5.028 0 7.542-1.562 9.104-1.562 1.562-4.076 1.562-9.104 1.562-5.029 0-7.543 0-9.105-1.562C36 54.21 36 51.695 36 46.667Zm-29.338 0c0-5.029 0-7.543 1.563-9.105C9.787 36 12.3 36 17.329 36c5.029 0 7.543 0 9.105 1.562 1.562 1.562 1.562 4.076 1.562 9.105 0 5.028 0 7.542-1.562 9.104-1.562 1.562-4.076 1.562-9.105 1.562-5.028 0-7.542 0-9.104-1.562-1.563-1.562-1.563-4.076-1.563-9.104Z"/>
                        </svg>
                        <button type="button" class="compare-kav__btn compare-kav__btn__outline__primary compare-kav__flex compare-kav__items-center compare-kav__justify-center" data-compare-kav-open="#compare-kav-modal">
                            <?php _e('افزودن کالا', 'kavehTheme'); ?>
                        </button>
                    </div>
                </div>
                <!-- End Add Product -->
            </div>
        </div>
    </div>
</div>