<?php
/**
 * Tech Repair Tabs Widget for Elementor
 *
 * @package Tech_Repair_Service
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Tech_Repair_Tabs_Widget class
 */
class Tech_Repair_Tabs_Widget extends \Elementor\Widget_Base {

    /**
     * Get widget name
     *
     * @return string Widget name
     */
    public function get_name() {
        return 'tech_repair_tabs';
    }

    /**
     * Get widget title
     *
     * @return string Widget title
     */
    public function get_title() {
        return esc_html__( 'Tech Repair Tabs', 'tech-repair-service' );
    }

    /**
     * Get widget icon
     *
     * @return string Widget icon
     */
    public function get_icon() {
        return 'eicon-tabs';
    }

    /**
     * Get widget categories
     *
     * @return array Widget categories
     */
    public function get_categories() {
        return [ 'tech-repair-service' ];
    }

    /**
     * Get widget keywords
     *
     * @return array Widget keywords
     */
    public function get_keywords() {
        return [ 'tech', 'repair', 'tabs', 'issues', 'devices' ];
    }

    /**
     * Register widget controls
     */
    protected function register_controls() {
        // Content Section
        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__( 'Content', 'tech-repair-service' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        // Get device categories
        $device_categories = get_terms( array(
            'taxonomy'   => 'tech_repair_device_cat',
            'hide_empty' => false,
        ) );

        $device_categories_options = array();
        foreach ( $device_categories as $category ) {
            $device_categories_options[ $category->slug ] = $category->name;
        }

        $this->add_control(
            'device_categories',
            [
                'label'       => esc_html__( 'Device Categories', 'tech-repair-service' ),
                'type'        => \Elementor\Controls_Manager::SELECT2,
                'options'     => $device_categories_options,
                'multiple'    => true,
                'label_block' => true,
                'description' => esc_html__( 'Select device categories to display. Leave empty to show all.', 'tech-repair-service' ),
            ]
        );

        // Get issue types
        $issue_types = get_terms( array(
            'taxonomy'   => 'tech_repair_issue_type',
            'hide_empty' => false,
        ) );

        $issue_types_options = array();
        foreach ( $issue_types as $type ) {
            $issue_types_options[ $type->slug ] = $type->name;
        }

        $this->add_control(
            'issue_types',
            [
                'label'       => esc_html__( 'Issue Types', 'tech-repair-service' ),
                'type'        => \Elementor\Controls_Manager::SELECT2,
                'options'     => $issue_types_options,
                'multiple'    => true,
                'label_block' => true,
                'description' => esc_html__( 'Select issue types to display. Leave empty to show all.', 'tech-repair-service' ),
            ]
        );

        $this->add_control(
            'columns',
            [
                'label'   => esc_html__( 'Columns', 'tech-repair-service' ),
                'type'    => \Elementor\Controls_Manager::SELECT,
                'default' => '3',
                'options' => [
                    '2' => esc_html__( '2 Columns', 'tech-repair-service' ),
                    '3' => esc_html__( '3 Columns', 'tech-repair-service' ),
                    '4' => esc_html__( '4 Columns', 'tech-repair-service' ),
                ],
            ]
        );

        $this->add_control(
            'content_length',
            [
                'label'       => esc_html__( 'Content Length', 'tech-repair-service' ),
                'type'        => \Elementor\Controls_Manager::NUMBER,
                'default'     => 200,
                'min'         => 50,
                'max'         => 1000,
                'step'        => 10,
                'description' => esc_html__( 'Number of characters to show before "Read more" button', 'tech-repair-service' ),
            ]
        );

        $this->add_control(
            'device_title',
            [
                'label'       => esc_html__( 'Device Title', 'tech-repair-service' ),
                'type'        => \Elementor\Controls_Manager::TEXT,
                'default'     => esc_html__( 'Select your device', 'tech-repair-service' ),
                'placeholder' => esc_html__( 'Enter title', 'tech-repair-service' ),
            ]
        );

        $this->add_control(
            'issue_title',
            [
                'label'       => esc_html__( 'Issue Title', 'tech-repair-service' ),
                'type'        => \Elementor\Controls_Manager::TEXT,
                'default'     => esc_html__( 'Select your issue', 'tech-repair-service' ),
                'placeholder' => esc_html__( 'Enter title', 'tech-repair-service' ),
            ]
        );

        $this->add_control(
            'show_pricing',
            [
                'label'        => esc_html__( 'Show Pricing', 'tech-repair-service' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'label_on'     => esc_html__( 'Yes', 'tech-repair-service' ),
                'label_off'    => esc_html__( 'No', 'tech-repair-service' ),
                'return_value' => 'yes',
                'default'      => 'yes',
            ]
        );

        $this->end_controls_section();

        // Style Section
        $this->start_controls_section(
            'section_style',
            [
                'label' => esc_html__( 'Style', 'tech-repair-service' ),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'tab_background_color',
            [
                'label'     => esc_html__( 'Tab Background Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'tab_active_background_color',
            [
                'label'     => esc_html__( 'Active Tab Background Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li.active a' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'tab_text_color',
            [
                'label'     => esc_html__( 'Tab Text Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'tab_active_text_color',
            [
                'label'     => esc_html__( 'Active Tab Text Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li.active a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'content_background_color',
            [
                'label'     => esc_html__( 'Content Background Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'content_text_color',
            [
                'label'     => esc_html__( 'Content Text Color', 'tech-repair-service' ),
                'type'      => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'tab_typography',
                'label'    => esc_html__( 'Tab Typography', 'tech-repair-service' ),
                'selector' => '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name'     => 'content_typography',
                'label'    => esc_html__( 'Content Typography', 'tech-repair-service' ),
                'selector' => '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'tab_border',
                'label'    => esc_html__( 'Tab Border', 'tech-repair-service' ),
                'selector' => '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name'     => 'content_border',
                'label'    => esc_html__( 'Content Border', 'tech-repair-service' ),
                'selector' => '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content',
            ]
        );

        $this->add_responsive_control(
            'tab_border_radius',
            [
                'label'      => esc_html__( 'Tab Border Radius', 'tech-repair-service' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'content_border_radius',
            [
                'label'      => esc_html__( 'Content Border Radius', 'tech-repair-service' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'tab_padding',
            [
                'label'      => esc_html__( 'Tab Padding', 'tech-repair-service' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-nav li a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'content_padding',
            [
                'label'      => esc_html__( 'Content Padding', 'tech-repair-service' ),
                'type'       => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .tech-repair-tabs .tech-repair-tab-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Render widget output on the frontend
     */
    protected function render() {
        $settings = $this->get_settings_for_display();

        // Prepare shortcode attributes
        $atts = array(
            'columns'         => $settings['columns'],
            'device_title'    => $settings['device_title'],
            'issue_title'     => $settings['issue_title'],
            'show_pricing'    => $settings['show_pricing'] === 'yes' ? 'true' : 'false',
            'content_length'  => $settings['content_length'],
        );
        
        // Обработка мультивыбора категорий устройств
        if (!empty($settings['device_categories'])) {
            $atts['device_category'] = implode(',', $settings['device_categories']);
        }
        
        // Обработка мультивыбора типов проблем
        if (!empty($settings['issue_types'])) {
            $atts['issue_type'] = implode(',', $settings['issue_types']);
        }

        // Convert attributes to shortcode format
        $shortcode_atts = '';
        foreach ( $atts as $key => $value ) {
            if ( ! empty( $value ) ) {
                $shortcode_atts .= ' ' . $key . '="' . esc_attr( $value ) . '"';
            }
        }

        // Output shortcode
        echo do_shortcode( '[tech_repair_tabs' . $shortcode_atts . ']' );
    }

    /**
     * Render widget output in the editor
     */
    protected function content_template() {
        ?>
        <div class="elementor-tech-repair-tabs-widget-notice">
            <?php echo esc_html__( 'Tech Repair Tabs will be displayed here. Preview on the frontend to see the actual tabs.', 'tech-repair-service' ); ?>
        </div>
        <?php
    }
}
