<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Panelová varianta — jen ikona v knihovně Elementoru. Plný widget na frontendu.
 */
class ElementSell_Course_Image_Widget extends \Elementor\Widget_Base {

    public function get_name() {
        return 'elementsell_course_image';
    }

    public function get_title() {
        return __( 'Obrázek kurzu', 'platebni-formular' );
    }

    public function get_icon() {
        return function_exists( 'elementsell_get_course_image_widget_icon' )
            ? elementsell_get_course_image_widget_icon()
            : 'eicon-eye';
    }

    public function get_categories() {
        return [ 'basic' ];
    }

    public function get_keywords() {
        return [ 'kurz', 'role', 'obrázek', 'elementsell', 'přístup' ];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Kurz', 'platebni-formular' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'image',
            [
                'label'   => __( 'Obrázek kurzu', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $image    = $settings['image'] ?? [];
        $url      = isset( $image['url'] ) ? (string) $image['url'] : '';

        if ( $url !== '' ) {
            ?>
            <div class="elementsell-course-image elementsell-course-image--panel">
                <img src="<?php echo esc_url( $url ); ?>" alt="" style="max-width:100%;height:auto;border-radius:8px;opacity:0.9;">
            </div>
            <?php
            return;
        }
        ?>
        <div class="elementsell-course-image elementsell-course-image--panel-placeholder" style="text-align:center;padding:20px 12px;border:1px dashed #c3c4c7;border-radius:8px;background:#f6f7f7;">
            <span class="eicon-image" style="font-size:28px;color:#646970;display:block;margin-bottom:8px;" aria-hidden="true"></span>
            <strong style="display:block;font-size:14px;color:#1d2327;"><?php esc_html_e( 'Obrázek kurzu', 'platebni-formular' ); ?></strong>
        </div>
        <?php
    }

    protected function content_template() {
        $placeholder = esc_url( \Elementor\Utils::get_placeholder_image_src() );
        ?>
        <#
        var image = settings.image || {};
        var imageUrl = image.url ? image.url : '';
        #>
        <# if ( imageUrl ) { #>
            <div class="elementsell-course-image elementsell-course-image--panel">
                <img src="{{{ imageUrl }}}" alt="" style="max-width:100%;height:auto;border-radius:8px;opacity:0.9;">
            </div>
        <# } else { #>
            <div class="elementsell-course-image elementsell-course-image--panel-placeholder" style="text-align:center;padding:20px 12px;border:1px dashed #c3c4c7;border-radius:8px;background:#f6f7f7;">
                <span class="eicon-image" style="font-size:28px;color:#646970;display:block;margin-bottom:8px;" aria-hidden="true"></span>
                <strong style="display:block;font-size:14px;color:#1d2327;"><?php esc_html_e( 'Obrázek kurzu', 'platebni-formular' ); ?></strong>
            </div>
        <# } #>
        <?php
    }
}
