<?php

use iwbvel\classes\helpers\Sanitizer;

if (!defined('ABSPATH')) exit; // Exit if accessed directly 

if (!empty($product_object) && $product_object instanceof \WC_Product_Variable) :
    $iwbvel_available_variations = $product_object->get_available_variations();
    $iwbvel_attributes = $product_object->get_variation_attributes();
    if (empty($iwbvel_available_variations)) {
?>
        <div class="iwbvel-alert iwbvel-alert-warning" style="border-radius: 4px; font-weight: 600;">
            <span>
                <i class="iwbvel-icon-info1"></i>
                <?php esc_html_e('There is not any variations for this product Or the price has not been set for them yet!', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>
            </span>
        </div>
    <?php
        return;
    }
    $iwbvel_selected_attributes = $product_object->get_default_attributes();
    $iwbvel_variable_image = (!empty($product_object->get_image_id())) ? wp_get_attachment_url($product_object->get_image_id()) : IWBVEL_IMAGES_URL . "/woocommerce-placeholder.png";
    $iwbvel_image_items = '';
    $iwbvel_price_items = '';
    $iwbvel_available_attributes = [];

    if (!empty($iwbvel_available_variations)) {
        foreach ($iwbvel_available_variations as $iwbvel_variation) {
            $iwbvel_element_attrs = '';
            if (!empty($iwbvel_variation['attributes'])) {
                foreach ($iwbvel_variation['attributes'] as $iwbvel_name => $iwbvel_term) {
                    $iwbvel_attr_name = str_replace('attribute_pa_', 'pa_', $iwbvel_name);
                    $iwbvel_available_attributes[$iwbvel_attr_name][] = $iwbvel_term;
                    $iwbvel_element_attrs .= 'data-' . esc_attr($iwbvel_attr_name) . '="' . esc_attr($iwbvel_term) . '" ';
                }
            }

            $iwbvel_image_items .= (!empty($iwbvel_variation['image']['url'])) ? '<img class="variation-image" ' . $iwbvel_element_attrs . ' src="' . esc_url($iwbvel_variation['image']['url']) . '">' : '<img class="variation-image" ' . $iwbvel_element_attrs . ' src="' . IWBVEL_IMAGES_URL . '/woocommerce-placeholder.png">';
            $iwbvel_price_items .= '<span class="iwbvel-variations-view-product-price-item" ' . $iwbvel_element_attrs . ' >Price: ' . $iwbvel_variation['price_html'] . '</span>';
        }
    }

    $iwbvel_available_attributes = array_map('array_unique', $iwbvel_available_attributes);

    ?>
    <div class="iwbvel-variations-view-product-left-side">
        <div class="iwbvel-variations-view-product-image">
            <img class="variable-image" src="<?php echo esc_url($iwbvel_variable_image); ?>" alt="<?php echo esc_attr($product_object->get_title()); ?>">
            <?php echo wp_kses($iwbvel_image_items, Sanitizer::allowed_html()); ?>
        </div>
    </div>
    <div class="iwbvel-variations-view-product-right-side">
        <strong><?php echo esc_html($product_object->get_title()); ?></strong>

        <?php
        if (!empty($iwbvel_attributes)) :
            foreach ($iwbvel_attributes as $iwbvel_attribute_name => $iwbvel_options) :
                if (!isset($iwbvel_available_attributes[$iwbvel_attribute_name])) {
                    continue;
                }
        ?>
                <div class="iwbvel-variations-view-product-variation-attribute">
                    <span><?php echo esc_html(wc_attribute_label($iwbvel_attribute_name)); ?></span>
                    <select class="iwbvel-variations-view-product-variation-attribute-item" data-attribute="<?php echo esc_attr($iwbvel_attribute_name); ?>">
                        <option value="">Choose an option</option>
                        <?php
                        if (!empty($iwbvel_options)) :
                            foreach ($iwbvel_options as $iwbvel_option_slug) :
                                if (!in_array($iwbvel_option_slug, $iwbvel_available_attributes[$iwbvel_attribute_name])) {
                                    continue;
                                }
                                $iwbvel_term = get_term_by('slug', $iwbvel_option_slug, $iwbvel_attribute_name);
                                if (!($iwbvel_term instanceof \WP_Term)) {
                                    continue;
                                }
                        ?>
                                <option value="<?php echo esc_attr($iwbvel_term->slug); ?>"><?php echo esc_html($iwbvel_term->name); ?></option>
                        <?php
                            endforeach;
                        endif;
                        ?>
                    </select>
                </div>
        <?php
            endforeach;
        endif;
        ?>

        <div class="iwbvel-variations-view-product-price">
            <?php echo wp_kses($iwbvel_price_items, Sanitizer::allowed_html()); ?>
        </div>
    </div>
<?php endif; ?>