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

class SeekMake_Client_Dashboard_Elementor extends Widget_Base {

    public function get_name() { return 'seekmake_client_dashboard'; }
    public function get_title() { return __('SeekMake Client Dashboard', 'seekmake'); }
    public function get_icon() { return 'eicon-dashboard'; }
    public function get_categories() { return ['general']; }
    public function get_keywords() { return ['seekmake', 'dashboard', 'client', 'portal']; }

    protected function register_controls() {
        $this->start_controls_section('content_section', [
            'label' => __('Content', 'seekmake'),
            'tab' => Controls_Manager::TAB_CONTENT,
        ]);

        $this->add_control('manufacturer_id', [
            'label' => __('Manufacturer ID', 'seekmake'),
            'type' => Controls_Manager::TEXT,
            'default' => get_option('seekmake_manufacturer_id', ''),
        ]);

        $this->add_control('display_mode', [
            'label' => __('Display Mode', 'seekmake'),
            'type' => Controls_Manager::SELECT,
            'default' => 'inline',
            'options' => [
                'modal' => __('Modal', 'seekmake'),
                'inline' => __('Inline', 'seekmake'),
            ],
        ]);

        $this->add_control('button_text', [
            'label' => __('Button Text', 'seekmake'),
            'type' => Controls_Manager::TEXT,
            'default' => __('Client Dashboard', 'seekmake'),
            'condition' => ['display_mode!' => 'inline'],
        ]);

        $this->add_control('language', [
            'label' => __('Language', 'seekmake'),
            'type' => Controls_Manager::SELECT,
            'default' => 'en',
            'options' => [
                'en' => __('English', 'seekmake'),
                'es' => __('Spanish', 'seekmake'),
                'fr' => __('French', 'seekmake'),
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('style_section', [
            'label' => __('Style', 'seekmake'),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition' => ['display_mode!' => 'inline'],
        ]);

        $this->add_control('button_style', [
            'label' => __('Button Style', 'seekmake'),
            'type' => Controls_Manager::SELECT,
            'default' => 'primary',
            'options' => [
                'primary' => __('Primary', 'seekmake'),
                'secondary' => __('Secondary', 'seekmake'),
                'outline' => __('Outline', 'seekmake'),
            ],
        ]);

        $this->add_responsive_control('button_align', [
            'label' => __('Alignment', 'seekmake'),
            'type' => Controls_Manager::CHOOSE,
            'options' => [
                'left' => ['title' => __('Left', 'seekmake'), 'icon' => 'eicon-text-align-left'],
                'center' => ['title' => __('Center', 'seekmake'), 'icon' => 'eicon-text-align-center'],
                'right' => ['title' => __('Right', 'seekmake'), 'icon' => 'eicon-text-align-right'],
            ],
            'default' => 'left',
        ]);

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $manufacturer_id = !empty($settings['manufacturer_id']) ? $settings['manufacturer_id'] : get_option('seekmake_manufacturer_id', '');
        $manufacturer_id = SeekMake_Security::sanitize_manufacturer_id($manufacturer_id);

        if (empty($manufacturer_id)) {
            echo '<p class="seekmake-error">' . esc_html__('Manufacturer ID not configured.', 'seekmake') . '</p>';
            return;
        }

        if ($settings['display_mode'] === 'inline') {
            $iframe_url = SEEKMAKE_API_URL . '/dashboard/client?isEmbed=true&manufacturer=' . esc_attr($manufacturer_id) . '&lang=' . esc_attr($settings['language']);
            ?>
            <section class="seekmake-dashboard-container">
                <iframe sandbox="allow-downloads allow-scripts allow-same-origin allow-forms allow-popups" class="seekmake-embed-dashboard" src="<?php echo esc_url($iframe_url); ?>" style="width: 100%; height: 100vh; border: 0px" scrolling="no"></iframe>
            </section>
            <?php
        } else {
            ?>
            <div class="seekmake-widget-wrapper seekmake-widget-<?php echo esc_attr($settings['button_align']); ?>">
                <button class="seekmake-widget-button seekmake-button-<?php echo esc_attr($settings['button_style']); ?> seekmake-modal-trigger" data-widget-type="client-dashboard" data-manufacturer-id="<?php echo esc_attr($manufacturer_id); ?>" data-lang="<?php echo esc_attr($settings['language']); ?>" type="button">
                    <?php echo esc_html($settings['button_text']); ?>
                </button>
            </div>
            <?php
        }
    }
}
