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

use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Elementor\Repeater;
use Elementor\Utils;

class Elementor_Images_List extends \Elementor\Widget_Base {

    public function get_name() {
        return 'images-list';
    }

    public function get_title() {
        return esc_html__('Images List', 'elementor-addon');
    }

    public function get_icon() {
        return 'eicon-code';
    }

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

    public function get_keywords() {
        return ['global', 'login'];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'list_section',
            [
                'label' => esc_html__('List', 'elementor-addon'),
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'image',
            [
                'label' => esc_html__('Image', 'elementor-addon'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $repeater->add_control(
            'URL',
            [
                'label' => esc_html__('URL', 'elementor-addon'),
                'type' => Controls_Manager::URL,
                'default' => [
                    'url' => '#',
                ],
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $this->add_control(
            'images',
            [
                'label' => esc_html__('Images', 'elementor-addon'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'image' => [
                            'url' => Utils::get_placeholder_image_src(),
                        ],
                    ],
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();

        if (empty($settings['images'])) {
            return;
        }
        ?>
        <ul>
            <?php foreach ($settings['images'] as $image) : ?>
                <li>
                    <a href="<?php echo $image['URL']['url']; ?>" <?php echo $image['URL']['is_external'] ? 'target="_blank"' : ''; ?>>
                        <img src="<?php echo $image['image']['url']; ?>">
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php
    }
}