<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;

class Custom_Partners_Slider extends Widget_Base {

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

    public function get_title() {
        return 'Partners Slider';
    }

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

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

    public function get_script_depends() {
        return [ 'elementor-addon-swiper-js', 'elementor-addon-main-js' ];
    }

    protected function _register_controls() {

        $this->start_controls_section(
            'partners_section',
            [
                'label' => esc_html__( 'Partners', 'elementor-addon' ),
            ]
        );

        $repeater = new Repeater();

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

        $repeater->add_control(
            'partner_link',
            [
                'label' => esc_html__( 'Partner Link', 'elementor-addon' ),
                'type' => Controls_Manager::URL,
                'placeholder' => 'https://example.com',
                'show_external' => true,
                'default' => [
                    'url' => '',
                    'is_external' => false,
                    'nofollow' => false,
                ],
            ]
        );

        $this->add_control(
            'partners_list',
            [
                'label' => esc_html__( 'Partners', 'elementor-addon' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [],
                'title_field' => '{{{ partner_image.url ? "Partner" : "New" }}}',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        if (empty($settings['partners_list'])) {
            return;
        }
        ?>
        <div class="partners-slider swiper">
            <div class="swiper-wrapper">
                <?php foreach ($settings['partners_list'] as $item) :
                    $link = $item['partner_link']['url'] ?? '';
                    $target = $item['partner_link']['is_external'] ? ' target="_blank"' : '';
                    $nofollow = $item['partner_link']['nofollow'] ? ' rel="nofollow"' : '';
                ?>
                    <div class="swiper-slide">
                        <?php if ($link): ?>
                            <a href="<?php echo esc_url($link); ?>"<?php echo $target . $nofollow; ?>></a>
                        <?php else: ?>
                            <img src="<?php echo esc_url($item['partner_image']['url']); ?>" alt="Partner">
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>

            <!-- <div class="swiper-button-prev"></div> -->
            <!-- <div class="swiper-button-next"></div> -->
        </div>
        <?php
    }
}
