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

/**
 * Lehká varianta widgetu Obrázek kurzu pro editor bez uloženého widgetu na stránce.
 */
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->add_control(
            'link',
            [
                'label'   => __( 'Odkaz, kam obrázek povede', 'platebni-formular' ),
                'type'    => \Elementor\Controls_Manager::URL,
                'default' => [
                    'url' => '',
                ],
            ]
        );

        $this->add_control(
            'course_role',
            [
                'label'       => __( 'Viditelné pro role', 'platebni-formular' ),
                'type'        => \Elementor\Controls_Manager::SELECT2,
                'label_block' => true,
                'options'     => function_exists( 'elementsell_get_role_select2_options' )
                    ? elementsell_get_role_select2_options()
                    : [],
                'multiple'    => true,
                'tags'        => true,
            ]
        );

        $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 === '' ) {
            echo '<div class="elementsell-course-image elementsell-course-image--notice">Vyberte obrázek kurzu.</div>';
            return;
        }
        ?>
        <div class="elementsell-course-image elementsell-course-image--admin">
            <img src="<?php echo esc_url( $url ); ?>" alt="" style="max-width:100%;height:auto;border-radius:8px;opacity:0.85;">
        </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 : <?php echo wp_json_encode( $placeholder ); ?>;
        #>
        <div class="elementsell-course-image elementsell-course-image--admin">
            <img src="{{{ imageUrl }}}" alt="" style="max-width:100%;height:auto;border-radius:8px;opacity:0.85;">
        </div>
        <?php
    }
}
