<?php
/**
 * Blog Over Radio Image Control
 * 
 * Custom radio buttons with image previews for Customizer
 *
 * @package Blog_Over
 */

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

if (!class_exists('WP_Customize_Control')) {
    return;
}

/**
 * Radio Image Control
 */
class blog_over_Radio_Image_Control extends WP_Customize_Control
{
    /**
     * Control type
     *
     * @var string
     */
    public $type = 'blog-over-radio-image';

    /**
     * Enqueue control styles and scripts
     */
    public function enqueue()
    {
        wp_enqueue_style(
            'blog-over-customizer-controls',
            get_template_directory_uri() . '/assets/css/customizer-controls.css',
            array(),
            Blog_Over_VERSION
        );
    }

    /**
     * Render the control content
     */
    public function render_content()
    {
        if (empty($this->choices)) {
            return;
        }

        $input_id = '_customize-input-' . $this->id;
        ?>
        <div class="blog-over-radio-image-wrapper">
            <?php if (!empty($this->label)): ?>
                <span class="customize-control-title"><?php echo esc_html($this->label); ?></span>
            <?php endif; ?>

            <?php if (!empty($this->description)): ?>
                <span class="description customize-control-description"><?php echo esc_html($this->description); ?></span>
            <?php endif; ?>

            <div class="blog-over-radio-image-options" id="<?php echo esc_attr($input_id); ?>">
                <?php foreach ($this->choices as $value => $image): ?>
                    <label class="blog-over-radio-image-item <?php echo ($this->value() === $value) ? 'selected' : ''; ?>">
                        <input type="radio" name="<?php echo esc_attr($input_id); ?>" value="<?php echo esc_attr($value); ?>"
                            class="blog-over-radio-image-input" <?php $this->link(); ?>             <?php checked($this->value(), $value); ?> />
                        <img src="<?php echo esc_url($image); ?>" alt="<?php echo esc_attr($value); ?>" />
                    </label>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
    }
}
