<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Image-radio (single-choice with picture-style options).
 *
 * Storage shape — single string (drop-in replacement for `select`):
 *   'BTC'
 *
 * Same option config as image_select:
 *   'BTC' => [ 'label' => 'Bitcoin', 'image' => '<url>/btc.svg' ]
 *
 * Supports the same `locked` flag — locked options are dimmed, disabled,
 * and can never be selected. If the currently saved value points at a now-
 * locked option, the renderer falls back to the next unlocked option.
 *
 * Constraint: exactly one (unlocked) option is always selected.
 *
 * @var array  $field
 * @var mixed  $value
 * @var string $name_base
 */
$options = isset( $field['options'] ) && is_array( $field['options'] ) ? $field['options'] : array();
$current = is_scalar( $value ) ? (string) $value : '';

// Build a list of unlocked keys for fallback selection.
$unlocked = array();
foreach ( $options as $k => $opt ) {
    if ( ! ( is_array( $opt ) && ! empty( $opt['locked'] ) ) ) {
        $unlocked[] = (string) $k;
    }
}

// Resolve which option should be marked as selected.
$current_is_locked = false;
if ( array_key_exists( $current, $options ) ) {
    $opt = $options[ $current ];
    $current_is_locked = is_array( $opt ) && ! empty( $opt['locked'] );
}

if ( $current === '' || ! array_key_exists( $current, $options ) || $current_is_locked ) {
    // Fall back to default (if unlocked) or first unlocked option.
    if ( isset( $field['default'] ) && in_array( (string) $field['default'], $unlocked, true ) ) {
        $current = (string) $field['default'];
    } elseif ( $unlocked ) {
        $current = $unlocked[0];
    }
}
?>
<div class="cn-image-select cn-image-radio">
    <?php foreach ( $options as $key => $opt ) :
        $opt          = is_array( $opt ) ? $opt : array( 'label' => (string) $opt );
        $label        = isset( $opt['label'] ) ? $opt['label'] : (string) $key;
        $image        = isset( $opt['image'] ) ? $opt['image'] : '';
        $badge        = isset( $opt['badge'] ) ? $opt['badge'] : '';
        $locked       = ! empty( $opt['locked'] );
        $tooltip_text = $locked && ! empty( $opt['locked_label'] ) ? $opt['locked_label'] : $label;

        $row_id  = 'cn-field-' . $field['id'] . '-' . $key;
        $checked = ! $locked && ( (string) $key === $current );
        $style   = $image ? 'background-image:url(' . esc_url( $image ) . ')' : '';

        $classes = array( 'cn-image-option' );
        if ( $checked ) $classes[] = 'is-checked';
        if ( $locked )  $classes[] = 'is-locked';
    ?>
        <label class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
               aria-label="<?php echo esc_attr( $tooltip_text ); ?>"
               style="<?php echo esc_attr( $style ); ?>">
            <input type="radio"
                   id="<?php echo esc_attr( $row_id ); ?>"
                   name="<?php echo esc_attr( $name_base ); ?>"
                   value="<?php echo esc_attr( $key ); ?>"
                   <?php checked( $checked ); ?>
                   <?php disabled( $locked ); ?> />
            <?php if ( $badge ) : ?>
                <span class="cn-image-option-badge"
                      style="background-image:url(<?php echo esc_url( $badge ); ?>)"></span>
            <?php endif; ?>
            <?php if ( $locked ) : ?>
                <span class="cn-image-option-lock" aria-hidden="true"><i class="icon ion-md-lock"></i></span>
            <?php endif; ?>
            <span class="cn-image-option-tooltip"><?php echo esc_html( $tooltip_text ); ?></span>
        </label>
    <?php endforeach; ?>
</div>
