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

class IDSC_Elementor_Accordion extends \Elementor\Widget_Base {

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

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

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

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

    public function get_keywords() {
        return array( 'accordion', 'toggle', 'collapse', 'faq' );
    }

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

        $repeater = new \Elementor\Repeater();

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

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

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

        $this->end_controls_section();

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

        $this->add_control( 'open_first', array(
            'label'        => __( 'Open First Item', 'idsc' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'idsc' ),
            'label_off'    => __( 'No', 'idsc' ),
            'return_value' => 'yes',
            'default'      => '',
        ) );

        $this->add_control( 'icon', array(
            'label'   => __( 'Icon Style', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'chevron',
            'options' => array(
                'chevron'    => __( 'Chevron', 'idsc' ),
                'plus-minus' => __( 'Plus / Minus', 'idsc' ),
                'none'       => __( 'None', 'idsc' ),
            ),
        ) );

        $this->add_control( 'border', array(
            'label'   => __( 'Border Style', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'bordered',
            'options' => array(
                'bordered'  => __( 'Bordered', 'idsc' ),
                'separated' => __( 'Separated', 'idsc' ),
                'none'      => __( 'None', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

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

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

        $atts  = $s['open_first'] === 'yes' ? ' open_first="yes"' : '';
        $atts .= ' icon="' . esc_attr( $s['icon'] ) . '"';
        $atts .= ' border="' . esc_attr( $s['border'] ) . '"';

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

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