<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
/**
 * @var array $data
 *
 */

use DEVELUXSCT\HTMLGenerators\SVG_Generator;

$post = $data['post'] ?? null;
if( !$post ) {
    return;
}

$develuxsct_svg = SVG_Generator::generate_button_icons();
$develuxsct_allowed = SVG_Generator::get_allowed_svg_elements();
$develuxsct_info_svg = $develuxsct_svg['info_purple'];

$develuxsct_options = $data['options'] ?? [];
$develuxsct_key = $data['key'] ?? '';
$develuxsct_global_key = $develuxsct_key.'_global';
$develuxsct_global_settings = wp_load_alloptions();
$develuxsct_global_value = $develuxsct_global_settings[$develuxsct_global_key] ?? '';
$develuxsct_ignore_global_settings = $data['ignore_global_settings'] ?? 1;

$develuxsct_legend = $data['legend'] ?? '';
$develuxsct_tooltip = $data['tooltip'] ?? '';
$develuxsct_data_tooltip = '';
if ($develuxsct_tooltip) {
    $develuxsct_data_tooltip =  $develuxsct_tooltip;
}
$develuxsct_class_name = $data['class_name'] ?? '';
$develuxsct_flex_direction = $data['flex_direction'] ?? 'col';
$develuxsct_flex_inner_direction = $data['flex_inner_direction'] ?? 'col';
$develuxsct_checkbox_val = get_post_meta($post->ID, $develuxsct_key, true);
$develuxsct_data_property = $data['data_property'] ?? '';
$develuxsct_data_dimension_unit_type = $data['data_dimension_unit_type'] ?? '';
$develuxsct_data_target = $data['data_target'] ?? '';
$develuxsct_items = $data['items'] ?? 'items-center';
$develuxsct_default_value = $data['default_value'] ?? '';
if( $develuxsct_ignore_global_settings ){
    $develuxsct_value = get_post_meta($post->ID, $develuxsct_key, true);
    if (empty($develuxsct_value) && $develuxsct_default_value) {
        $develuxsct_value = $develuxsct_default_value;
    }
}else{
    $develuxsct_value = $develuxsct_global_value;
}

?>
<div class="label-style develux-radiobutton flex flex-<?php echo esc_attr($develuxsct_flex_direction); ?> flex-wrap <?php echo esc_attr($develuxsct_items); ?> gap-5">
        <label class="flex justify-between items-center">
            <?php echo esc_html($develuxsct_legend); ?>
            <?php if($develuxsct_data_tooltip): ?>
                <span data-develux-tooltip="<?php echo esc_attr($develuxsct_data_tooltip); ?>"><i class="woocommerce-help-tip"></i></span>
        <?php endif; ?>
        </label>
    <?php
    if( $develuxsct_options ):  ?>
    <div class="flex flex-<?php echo esc_attr($develuxsct_flex_inner_direction); ?> <?php echo esc_attr($develuxsct_items); ?> gap">
  <?php  foreach ($develuxsct_options as $develuxsct_option): ?>

    <div class="flex flex-row gap">
        <input
                data-default-value="<?php echo esc_attr($develuxsct_default_value); ?>"
                data-target="<?php echo esc_attr($develuxsct_data_target); ?>"
                data-property="<?php echo esc_attr($develuxsct_data_property); ?>"
                data-unit="<?php echo esc_attr($develuxsct_data_dimension_unit_type); ?>"
                id="<?php echo esc_attr($develuxsct_option['id']) ?? ''; ?>"
                type="radio"
                class="<?php echo esc_attr($develuxsct_class_name); ?>"
                name="<?php echo esc_attr($develuxsct_key); ?>"
                value="<?php echo esc_attr($develuxsct_option['value']) ?? ''; ?>"
            <?php checked( esc_attr($develuxsct_option['value']), esc_attr($develuxsct_value) ); ?>>

        <?php
        $develuxsct_description = $develuxsct_option['description'] ?? '';
        $develuxsct_error_message = $develuxsct_option['error_msg'] ?? '';
        $develuxsct_data_tooltip = '';
        $develuxsct_tooltip_icon = '';
        if($develuxsct_description){
            $develuxsct_data_tooltip = $develuxsct_description;
            $develuxsct_tooltip_icon = $develuxsct_info_svg;
        }
        ?>
        <?php if($develuxsct_data_tooltip): ?>
        <label class="develux-radiobutton-label" for="<?php echo esc_attr($develuxsct_option['id']) ?? ''; ?>" >
            <?php
            echo esc_html($develuxsct_option['label']) ?? '';
            echo '<span data-develux-tooltip="'. esc_attr($develuxsct_data_tooltip) . '"><i class="woocommerce-help-tip"></i></span>';
            echo wp_kses('&nbsp; '. $develuxsct_error_message,$develuxsct_allowed);
            ?>
            <?php else: ?>
        <label class="develux-radiobutton-label" for="<?php echo esc_attr($develuxsct_option['id']) ?? ''; ?>" >
            <?php
            echo esc_html($develuxsct_option['label']) ?? '';
            ?>
            <?php endif; ?>
        </label>
    </div>
    <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>
