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

class Nikan_Products_Grid_Widget extends \Elementor\Widget_Base {

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

    public function get_title() { return 'محصولات (نیکان)'; }

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

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

    protected function register_controls() {
        $this->start_controls_section('section_content', [
            'label' => 'تنظیمات',
            'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
        ]);

        $this->add_control('count', [
            'label'   => 'تعداد محصولات',
            'type'    => \Elementor\Controls_Manager::NUMBER,
            'min'     => 1,
            'max'     => 50,
            'step'    => 1,
            'default' => 8,
        ]);

        $this->add_control('columns', [
            'label'   => 'تعداد ستون (دسکتاپ)',
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => '4',
            'options' => [
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
            ],
        ]);

        $this->add_control('category', [
            'label'       => 'اسلاگ دسته محصول (اختیاری)',
            'type'        => \Elementor\Controls_Manager::TEXT,
            'placeholder' => 'مثلاً laptops',
        ]);

        $this->add_control('orderby', [
            'label'   => 'مرتب‌سازی',
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'date',
            'options' => [
                'date'       => 'جدیدترین',
                'popularity' => 'محبوب‌ترین',
                'rating'     => 'امتیاز',
                'price'      => 'قیمت (کم به زیاد)',
                'price-desc' => 'قیمت (زیاد به کم)',
                'rand'       => 'تصادفی',
            ],
        ]);

        $this->end_controls_section();
    }

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

        if ( ! class_exists('WooCommerce') ) {
            echo '<div class="p-4 border border-dashed border-slate-300 rounded-xl text-slate-600">ووکامرس فعال نیست.</div>';
            return;
        }

        $count = max(1, absint($settings['count']));
        $cols  = max(2, min(5, absint($settings['columns'])));

        $args = [
            'limit'   => $count,
            'status'  => 'publish',
            'orderby' => $settings['orderby'],
        ];

        $cat = trim((string)($settings['category'] ?? ''));
        if ( $cat !== '' ) {
            $args['category'] = [$cat];
        }

        $products = wc_get_products($args);

        if ( empty($products) ) {
            echo '<p class="text-center text-slate-500">محصولی برای نمایش پیدا نشد.</p>';
            return;
        }

        // Tailwind grid classes for desktop columns
        $grid_class = 'grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-'.$cols.' gap-3 sm:gap-4';

        echo '<div class="'.esc_attr($grid_class).'">';
        foreach ( $products as $product ) {
            $pid = $product->get_id();
            $permalink = get_permalink($pid);
            $img = $product->get_image('woocommerce_thumbnail', ['class' => 'w-full h-auto rounded-2xl']);
            $title = esc_html($product->get_name());

            echo '<div class="nikan-product-card bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">';
            echo '<a href="'.esc_url($permalink).'" class="block p-3">'.$img.'</a>';
            echo '<div class="p-4">';
            echo '<a href="'.esc_url($permalink).'" class="block text-slate-900 font-bold text-sm sm:text-base line-clamp-2">'.$title.'</a>';
            echo '<div class="mt-2 text-slate-900 font-extrabold text-base sm:text-lg">'.$product->get_price_html().'</div>';
            echo '<div class="mt-4">';
            echo '<a class="inline-flex items-center justify-center w-full px-4 py-2 rounded-xl bg-emerald-600 text-white font-bold hover:opacity-90 transition" href="'.esc_url($permalink).'">مشاهده</a>';
            echo '</div>';
            echo '</div></div>';
        }
        echo '</div>';
    }
}
