<?php

use iwbvel\classes\helpers\Sanitizer;

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

<?php
if (!empty($iwbvel_variation) && $iwbvel_variation instanceof \WC_Product_Variation) {
    $iwbvel_image_id = $iwbvel_variation->get_image_id();
    $iwbvel_full_size = wp_get_attachment_image_src($iwbvel_image_id, 'full');
    $iwbvel_full_size = (!empty($iwbvel_full_size[0])) ? $iwbvel_full_size[0] : esc_url(IWBVEL_IMAGES_URL . "/woocommerce-placeholder.png");
    $iwbvel_image_url = wp_get_attachment_image_src($iwbvel_variation->get_image_id(), [40, 40]);
    $iwbvel_image_url = !empty($iwbvel_image_url[0]) ? $iwbvel_image_url[0] : esc_url(IWBVEL_IMAGES_URL . "/woocommerce-placeholder-150x150.png");

    $iwbvel_attribute_input = '';
    $iwbvel_variation_attributes = $iwbvel_variation->get_variation_attributes();
    $iwbvel_attributes = '<div class="iwbvel-variations-table-name">';
    if (!empty($iwbvel_variation_attributes)) {
        $iwbvel_is_default = true;
        $iwbvel_i = 0;
        foreach ($iwbvel_variation_attributes as $iwbvel_attribute_key => $iwbvel_attribute_slug) {
            $iwbvel_attribute_key = str_replace('attribute_', '', urldecode($iwbvel_attribute_key));
            $iwbvel_attribute_slug = urldecode($iwbvel_attribute_slug);
            $iwbvel_attribute_input .= '<input type="hidden" class="iwbvel-variation-attributes-inputs" data-attribute="' . esc_attr($iwbvel_attribute_key) . '" data-term="' . esc_attr($iwbvel_attribute_slug) . '" name="variations[' . intval($iwbvel_variation->get_id()) . '][attributes][' . esc_attr($iwbvel_attribute_key) . ']" value="' . esc_attr($iwbvel_attribute_slug) . '">';
            if (empty($default_attributes) || !isset($default_attributes[$iwbvel_attribute_key]) || $default_attributes[$iwbvel_attribute_key] != $iwbvel_attribute_slug) {
                $iwbvel_is_default = false;
            }

            if ($iwbvel_i > 0) {
                $iwbvel_attributes .= '<br>';
            }

            $iwbvel_attribute_label = wc_attribute_label($iwbvel_attribute_key);
            $iwbvel_attribute_label = (!empty($iwbvel_attribute_label)) ? $iwbvel_attribute_label : ucfirst(str_replace('pa_', '', $iwbvel_attribute_key));
            if ($iwbvel_attribute_slug === '') {
                $iwbvel_attributes .= '<strong>' . $iwbvel_attribute_label . '</strong>: ' . wp_kses(sprintf(strtolower('Any %s'), $iwbvel_attribute_label), Sanitizer::allowed_html());
            } else {
                $iwbvel_term = get_term_by('slug', $iwbvel_attribute_slug, $iwbvel_attribute_key);
                if ($iwbvel_term instanceof \WP_Term) {
                    $iwbvel_attributes .= '<strong>' . $iwbvel_attribute_label . '</strong>: <span>' .  urldecode($iwbvel_term->name) . '</span>';
                }
            }

            $iwbvel_i++;
        }
    }
    $iwbvel_attributes .= '</div>';

    $iwbvel_row = '';
    ob_start();
    include IWBVEL_VIEWS_DIR . "variations/add_variations/variations-raw-table-row.php";
    $iwbvel_row = ob_get_clean();
    $iwbvel_row = str_replace('{id}', esc_attr($iwbvel_variation->get_id()), $iwbvel_row);
    $iwbvel_row = str_replace('{attributes}', $iwbvel_attribute_input, $iwbvel_row);
    $iwbvel_row = str_replace('{name}', $iwbvel_attributes, $iwbvel_row);
    $iwbvel_row = str_replace('{enable_checked}', ($iwbvel_variation->get_status() == 'publish') ? 'checked="checked"' : '', $iwbvel_row);
    $iwbvel_row = str_replace('{default_checked}', (isset($iwbvel_is_default) && $iwbvel_is_default) ? 'checked="checked"' : '', $iwbvel_row);
    $iwbvel_row = str_replace('{regular_price}', esc_html($iwbvel_variation->get_regular_price()), $iwbvel_row);
    $iwbvel_row = str_replace('{sale_price}', esc_html($iwbvel_variation->get_sale_price()), $iwbvel_row);
    $iwbvel_row = str_replace('{stock_quantity}', esc_html($iwbvel_variation->get_stock_quantity()), $iwbvel_row);
    $iwbvel_row = str_replace('{thumbnail}', esc_url($iwbvel_image_url), $iwbvel_row);
    $iwbvel_row = str_replace('{thumbnail_full_size}', esc_url($iwbvel_full_size), $iwbvel_row);
    echo wp_kses($iwbvel_row, Sanitizer::allowed_html());
}
