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

class Nikan_ImageBannerCard_Widget extends \Elementor\Widget_Base {

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

    public function get_title() { return 'بنر تصویر (Nikan)'; }

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

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

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

    public function get_description() {
        return 'یک بنر/کارت تصویری با استایل گرد و hover مشابه قالب.';
    }

    protected function register_controls() {
        $this->start_controls_section('content', [
            'label' => 'محتوا',
            'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
        ]);

        $this->add_control('image', [
            'label'   => 'تصویر',
            'type'    => \Elementor\Controls_Manager::MEDIA,
            'default' => [ 'url' => '' ],
        ]);

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

        $this->add_control('min_height', [
            'label'       => 'حداقل ارتفاع (px)',
            'type'        => \Elementor\Controls_Manager::NUMBER,
            'default'     => 180,
            'min'         => 80,
            'max'         => 800,
        ]);

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();
        $img = isset($s['image']['url']) ? $s['image']['url'] : '';
        $url = isset($s['link']['url']) && $s['link']['url'] ? $s['link']['url'] : '#';
        $is_external = !empty($s['link']['is_external']);
        $nofollow    = !empty($s['link']['nofollow']);
        $attrs = '';
        if ( $is_external ) $attrs .= ' target="_blank"';
        if ( $nofollow ) $attrs .= ' rel="nofollow"';
        $min_h = isset($s['min_height']) ? (int)$s['min_height'] : 180;

        if ( ! $img ) {
            echo '<div style="padding:12px;border:1px dashed #ddd;">تصویر بنر را انتخاب کنید.</div>';
            return;
        }
        ?>
        <a href="<?php echo esc_url($url); ?>" <?php echo $attrs; ?>
           class="block overflow-hidden rounded-[28px] ring-1 ring-slate-200 bg-white hover:ring-emerald-200 hover:shadow-lg transition-all duration-300 no-underline">
            <div class="w-full" style="min-height: <?php echo esc_attr($min_h); ?>px;">
                <img src="<?php echo esc_url($img); ?>" alt="" class="w-full h-full object-cover" loading="lazy" />
            </div>
        </a>
        <?php
    }
}
