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

class IDSC_Elementor_Tabs extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'Tabs', 'idsc' );
    }

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

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'tabs', 'tabbed', 'sections' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_items', array(
            'label' => __( 'Tab Items', 'idsc' ),
        ) );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control( 'title', array(
            'label'   => __( 'Tab Title', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::TEXT,
            'default' => __( 'Tab', 'idsc' ),
        ) );

        $repeater->add_control( 'content', array(
            'label'   => __( 'Content', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::WYSIWYG,
            'default' => __( 'Tab content goes here.', 'idsc' ),
        ) );

        $this->add_control( 'items', array(
            'label'       => __( 'Tabs', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::REPEATER,
            'fields'      => $repeater->get_controls(),
            'default'     => array(
                array( 'title' => __( 'Tab 1', 'idsc' ), 'content' => __( 'Content for tab 1.', 'idsc' ) ),
                array( 'title' => __( 'Tab 2', 'idsc' ), 'content' => __( 'Content for tab 2.', 'idsc' ) ),
            ),
            'title_field' => '{{{ title }}}',
        ) );

        $this->end_controls_section();

        $this->start_controls_section( 'section_settings', array(
            'label' => __( 'Settings', 'idsc' ),
        ) );

        $this->add_control( 'layout', array(
            'label'   => __( 'Layout', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'horizontal',
            'options' => array(
                'horizontal' => __( 'Horizontal', 'idsc' ),
                'vertical'   => __( 'Vertical', 'idsc' ),
            ),
        ) );

        $this->add_control( 'alignment', array(
            'label'   => __( 'Tab Alignment', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'left',
            'options' => array(
                'left'   => __( 'Left', 'idsc' ),
                'center' => __( 'Center', 'idsc' ),
                'right'  => __( 'Right', 'idsc' ),
            ),
        ) );

        $this->add_control( 'active', array(
            'label'   => __( 'Default Active Tab', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::NUMBER,
            'default' => 1,
            'min'     => 1,
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s     = $this->get_settings_for_display();
        $items = $s['items'] ?? array();

        if ( empty( $items ) ) {
            return;
        }

        $atts  = ' layout="' . esc_attr( $s['layout'] ) . '"';
        $atts .= ' alignment="' . esc_attr( $s['alignment'] ) . '"';
        $atts .= ' active="' . esc_attr( $s['active'] ) . '"';

        $inner = '';
        foreach ( $items as $item ) {
            $inner .= '[idsc_tab title="' . esc_attr( $item['title'] ) . '"]' . wp_kses_post( $item['content'] ) . '[/idsc_tab]';
        }

        echo do_shortcode( '[idsc_tabs' . $atts . ']' . $inner . '[/idsc_tabs]' );
    }
}
