<?php

namespace Elementor;

include get_template_directory() . '/elementor/' . 'contracts.php';


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


class BantaFaq extends Widget_Base
{

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

    public function get_title()
    {
        return __('Jiawaz FAQ', TEXT_DOMAIN);
    }

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

    public function get_categories()
    {
        return ['jiawaz_theme'];
    }

    public function get_keywords()
    {
        return ['card', 'box', 'list', 'carousel', 'faq', 'question','accordion'];
    }

    protected function _register_controls()
    {
        $this->register_content_tab_content();
        $this->register_style_tab_controls();
    }

    protected function register_content_tab_content()
    {
        $this->start_controls_section(
            'section_items',
            [
                'label' => esc_html__('Content', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'title',
            [
                'label' => esc_html__('Title', TEXT_DOMAIN),
                'default' => esc_html__('Lorem ipsum dolor sit amet', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::TEXT,
                'placeholder' => esc_html__('enter your text', TEXT_DOMAIN),
            ]
        );
//        $repeater->add_control(
//            'text',
//            [
//                'label' => esc_html__('Text', TEXT_DOMAIN),
//                'default' => esc_html__('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', TEXT_DOMAIN),
//                'type' => \Elementor\Controls_Manager::TEXTAREA,
//                'placeholder' => esc_html__('enter your text', TEXT_DOMAIN),
//            ]
//        );

        $repeater->add_control(
            'text',
            [
                'label' => esc_html__('Text', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::WYSIWYG,
                'default' => esc_html__(
                    'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
                    TEXT_DOMAIN
                ),
                'placeholder' => esc_html__('Enter your text', TEXT_DOMAIN),
            ]
        );



        $this->add_control(
            'items',
            [
                'label' => esc_html__('Items', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'title' => esc_html__('Lorem ipsum dolor sit amet', TEXT_DOMAIN),
                        'text' => esc_html__('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', TEXT_DOMAIN),
                    ],
                    [
                        'title' => esc_html__('Lorem ipsum dolor sit amet', TEXT_DOMAIN),
                        'text' => esc_html__('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', TEXT_DOMAIN),
                    ],
                ],
            ]
        );

        $this->end_controls_section();

    }

    protected function register_style_tab_controls()
    {
        $this->start_controls_section(
            'section_style',
            [
                'label' => esc_html__('Style', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );


        $this->add_responsive_control(
            'border_radius',
            [
                'label' => __('border radius', TEXT_DOMAIN),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', 'rem'],
                'selectors' => [
                    '{{WRAPPER}} .accordion-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'border_type',
                'label' => esc_html__('border', TEXT_DOMAIN),
                'selector' => '{{WRAPPER}} .accordion-item',
            ]
        );

        $this->add_control(
            'box_color',
            [
                'label' => esc_html__( 'background color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-item' => 'background-color: {{VALUE}}!important',
                ],
            ]
        );

        // title and text style

        $this->start_controls_tabs(
            'content_style_tabs'
        );
        $this->start_controls_tab(
            'content_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );

        $this->add_control(
            'icon_color',
            [
                'label' => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button::after' => 'color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'icon_bg_color',
            [
                'label' => esc_html__( 'icon background color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button::after' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => esc_html__( 'title color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button-text' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'label' => esc_html__( 'title typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .accordion-button-text',
            ]
        );
        $this->add_control(
            'body_bg_color',
            [
                'label' => esc_html__( 'body background color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-body' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'text_color',
            [
                'label' => esc_html__( 'text color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-body' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'text_typography',
                'label' => esc_html__( 'text typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .accordion-body',
            ]
        );
        $this->end_controls_tab();
        $this->start_controls_tab(
            'content_style_active_tab',
            [
                'label' => esc_html__( 'active', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'icon_color_active',
            [
                'label' => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button:not(.collapsed)::after' => 'color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'icon_bg_color_active',
            [
                'label' => esc_html__( 'icon background color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button:not(.collapsed)::after' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'title_color_hoover',
            [
                'label' => esc_html__( 'title color', TEXT_DOMAIN ),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .accordion-button:not(.collapsed) .accordion-button-text' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography_hover',
                'label' => esc_html__( 'title typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .accordion-button:not(.collapsed) .accordion-button-text',
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();


        $this->end_controls_section();
    }

    protected function render()
    {

        $settings = $this->get_settings_for_display();

        $randomId = substr(str_shuffle(str_repeat($x='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(4/strlen($x)) )),1,4);

        echo "<div class='faq-box elementor-faq'><div class='accordion' id='".$randomId."'>";
        foreach ($settings['items'] as $index => $item) : ?>

            <div class="accordion-item">
                <div class="accordion-header" id="headingOne">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#<?php echo 'bantaCollapse'.$index?>" aria-expanded="false" aria-controls="<?php echo 'bantaCollapse'.$index?>">
                        <span class="accordion-button-text"><?php echo $item['title'];?></span>
                    </button>
                </div>
                <div id="<?php echo 'bantaCollapse'.$index?>" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#<?php echo $randomId?>">
                    <div class="accordion-body">
                        <?php echo wp_kses_post($item['text']);?>
                    </div>
                </div>
            </div>

        <?php endforeach; ?>
        <?php
        echo "</div></div>";
    }


}

Plugin::instance()->widgets_manager->register_widget_type(new BantaFaq());