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

class Nikan_BannerFullHomePage_Widget extends \Elementor\Widget_Base {

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

    public function get_title() {
        return 'بنرهای دو‌تایی';
    }

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

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

    public function get_keywords() {
        return [ 'nikan', 'home', 'banner' ];
    }

    protected function register_controls() {
        $theme_uri = defined('THEME_DIR') ? THEME_DIR : get_template_directory_uri();

        $this->start_controls_section(
            'section_banners',
            [
                'label' => 'بنرها',
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'image',
            [
                'label'   => 'تصویر',
                'type'    => \Elementor\Controls_Manager::MEDIA,
                'default' => [ 'url' => $theme_uri . '/src/img/p5.jpg' ],
            ]
        );

        $repeater->add_control(
            'alt',
            [
                'label'       => 'alt',
                'type'        => \Elementor\Controls_Manager::TEXT,
                'default'     => 'banner',
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'url',
            [
                'label'       => 'لینک',
                'type'        => \Elementor\Controls_Manager::URL,
                'placeholder' => '#',
                'default'     => [ 'url' => '#' ],
                'label_block' => true,
            ]
        );

        $this->add_control(
            'banners',
            [
                'type'        => \Elementor\Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'title_field' => '{{{ alt }}}',
                'default'     => [
                    [ 'image' => [ 'url' => $theme_uri . '/src/img/p5.jpg' ], 'alt' => 'banner 1', 'url' => [ 'url' => '#' ] ],
                    [ 'image' => [ 'url' => $theme_uri . '/src/img/p4.jpg' ], 'alt' => 'banner 2', 'url' => [ 'url' => '#' ] ],
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $banners  = isset($settings['banners']) && is_array($settings['banners']) ? $settings['banners'] : [];
        if ( empty($banners) ) return;

        // فقط دو آیتم اول را رندر می‌کنیم تا ساختار دقیق حفظ شود.
        $banners = array_slice($banners, 0, 2);
        ?>
        <section class="mt-6">
            <div class="grid md:grid-cols-2 items-center justify-between gap-4">
                <?php foreach ( $banners as $banner ):
                    $url = isset($banner['url']['url']) && $banner['url']['url'] ? $banner['url']['url'] : '#';
                    $img = isset($banner['image']['url']) ? $banner['image']['url'] : '';
                    $alt = isset($banner['alt']) ? $banner['alt'] : '';
                    if ( ! $img ) continue;
                    $is_external = !empty($banner['url']['is_external']);
                    $nofollow    = !empty($banner['url']['nofollow']);
                    $attrs = '';
                    if ( $is_external ) $attrs .= ' target="_blank"';
                    if ( $nofollow ) $attrs .= ' rel="nofollow"';
                ?>
                    <a href="<?php echo esc_url($url); ?>" <?php echo $attrs; ?>><img class="rounded-2xl" src="<?php echo esc_url($img); ?>" alt="<?php echo esc_attr($alt); ?>"></a>
                <?php endforeach; ?>
            </div>
        </section>
        <?php
    }
}
