<?php
namespace AITheme\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;

if (!defined('ABSPATH')) exit;

class Comparison_Table extends AI_Widget_Base {
    public function get_name() { return 'ai_comparison_table'; }
    public function get_title() { return __('Comparison Table', 'ai-theme'); }
    public function get_icon() { return 'eicon-table'; }
    public function get_categories() { return array('ai-theme-comparison'); }

    protected function register_controls() {
        $this->start_controls_section('content_section', ['label' => __('Content', 'ai-theme'), 'tab' => Controls_Manager::TAB_CONTENT]);
        $this->add_control('title', ['label' => __('Title', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $this->add_control('columns', ['label' => __('Column Headers (comma separated)', 'ai-theme'), 'type' => Controls_Manager::TEXT]);

        $repeater = new Repeater();
        $repeater->add_control('feature', ['label' => __('Feature', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $repeater->add_control('values', ['label' => __('Values (comma separated)', 'ai-theme'), 'type' => Controls_Manager::TEXT]);
        $this->add_control('rows', ['label' => __('Rows', 'ai-theme'), 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls()]);
        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();
        $cols = !empty($s['columns']) ? array_map('trim', explode(',', $s['columns'])) : [];
        ?>
        <section class="ai-comparison ai-comparison-table w-full bg-gray-50 py-16">
            <div class="ai-container">
                <?php if (!empty($s['title'])) : ?>
                <div class="text-center mb-12">
                    <h2 class="text-3xl md:text-4xl font-bold text-gray-900 mb-4"><?php echo esc_html($s['title']); ?></h2>
                </div>
                <?php endif; ?>
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                    <div class="overflow-x-auto">
                        <table class="ai-comparison-table-inner w-full">
                            <?php if (!empty($cols)) : ?>
                            <thead class="sticky top-0 z-10 bg-gradient-to-br from-blue-600 to-blue-700">
                                <tr>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-white"><?php _e('Features', 'ai-theme'); ?></th>
                                    <?php foreach ($cols as $col) : ?>
                                    <th class="px-6 py-4 text-center text-sm font-semibold text-white border-l border-blue-500"><?php echo esc_html($col); ?></th>
                                    <?php endforeach; ?>
                                </tr>
                            </thead>
                            <?php endif; ?>
                            <tbody>
                                <?php foreach ($s['rows'] as $row) : $vals = array_map('trim', explode(',', $row['values'])); ?>
                                <tr class="border-t border-gray-200 hover:bg-gray-50 transition-colors">
                                    <td class="px-6 py-4 text-sm text-gray-700"><?php echo esc_html($row['feature']); ?></td>
                                    <?php foreach ($vals as $v) : ?>
                                    <td class="px-6 py-4 text-sm text-center border-l border-gray-200 text-gray-900 font-medium"><?php echo esc_html($v); ?></td>
                                    <?php endforeach; ?>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>
        <?php
    }
}
