<?php
$html_id = ct_get_element_id($settings);
$wg_title = $widget->get_setting('wg_title');
$active_tab = $widget->get_setting('active_tab', 1);
$tabs = $widget->get_setting('tabs', '');
$tabs_title = [];
$tabs_content = [];
$is_new = \Elementor\Icons_Manager::is_migration_allowed();
foreach ($tabs as $key => $tab){
    $title_key = $widget->get_repeater_setting_key( 'tab_title', 'tabs', $key );
    $content_key = $widget->get_repeater_setting_key( 'tab_content', 'tabs', $key );

    $tabs_title[$title_key] = $tab['tab_title'];
    $tabs_content[$content_key] = '';
    if($tab['content_type'] == 'template'){
        if(!empty($tab['tab_content_template'])){
            $content = \Elementor\Plugin::$instance->frontend->get_builder_content( $tab['tab_content_template'] );
            $tabs_content[$content_key] = $content;
        }
    }
    $form_class = '';
    if($tab['content_type'] == 'form') {
        $form_class = 'ct-contact-form-layout1 style8';
        if(!empty($tab['form_id'])) {
            $tabs_content[$content_key] = do_shortcode('[contact-form-7 id="'.esc_attr( $tab['form_id'] ).'"]');
        }
    }
    elseif($tab['content_type'] == 'text_editor'){
        $tabs_content[$content_key] = $tab['tab_content'];
    }

    $widget->add_render_attribute( $title_key, [
        'class' => [ 'ct-tab-title' ],
        'data-target' => '#' . $html_id.'-'.$tab['_id'],
    ] );

    $widget->add_inline_editing_attributes( $title_key, 'basic' );
    $widget->add_render_attribute( $content_key, [
        'class' => [ 'ct-tab-content', $form_class ],
        'id' => $html_id.'-'.$tab['_id'],
    ] );
    if($tab['content_type'] == 'text_editor'){
        $widget->add_inline_editing_attributes( $content_key, 'advanced' );
    }

    if($active_tab == $key + 1){
        $widget->add_render_attribute( $content_key, 'style', 'display:block;');
        $widget->add_render_attribute( $title_key, 'class', 'active');
    }
} ?>
<div class="ct-tabs ct-tabs1 <?php echo esc_attr($settings['ct_animate']); ?> content-type-<?php echo esc_attr($tab['content_type']); ?>" data-wow-delay="<?php echo esc_attr($settings['ct_animate_delay']); ?>ms">
    <div class="ct-tabs--main">
        <div class="ct-tabs-content">
            <?php foreach ($tabs_content as $content_key => $tab_content): ?>
                <div <?php ct_print_html($widget->get_render_attribute_string( $content_key )); ?>><?php ct_print_html($tab_content); ?></div>
            <?php endforeach; ?>
        </div>
        <div class="ct-tabs-title">
            <?php foreach ($tabs as $key => $tab) : 
                $icon_key = $widget->get_repeater_setting_key( 'ct_icon', 'icons', $key );
                $has_icon = ! empty( $tab['ct_icon'] );
                $widget->add_render_attribute( $icon_key, [
                    'class' => $tab['ct_icon'],
                    'aria-hidden' => 'true',
                ] );

                $link_key = $widget->get_repeater_setting_key( 'title', 'value', $key );
                if ( ! empty( $tab['btn_link']['url'] ) ) {
                    $widget->add_render_attribute( $link_key, 'href', $tab['btn_link']['url'] );

                    if ( $tab['btn_link']['is_external'] ) {
                        $widget->add_render_attribute( $link_key, 'target', '_blank' );
                    }

                    if ( $tab['btn_link']['nofollow'] ) {
                        $widget->add_render_attribute( $link_key, 'rel', 'nofollow' );
                    }
                }
                $link_attributes = $widget->get_render_attribute_string( $link_key );

                ?>
                <div class="ct-tab-title <?php if($active_tab == $key + 1) { echo 'active'; } ?>" data-target="#<?php echo esc_attr($html_id.'-'.$tab['_id']); ?>">
                    <div class="tb-item--icon">
                        <?php
                            if($is_new):
                                \Elementor\Icons_Manager::render_icon( $tab['ct_icon'], [ 'aria-hidden' => 'true' ] );
                        ?>
                        <?php else: ?>
                            <i <?php ct_print_html($widget->get_render_attribute_string( $icon_key )); ?>></i>
                        <?php endif; ?>
                    </div>
                    <div class="tb-item--meta">
                        <h4 class="tb-item--title"><?php echo ct_print_html($tab['tab_title']); ?></h4>
                        <?php if(!empty($tab['btn_text'])) : ?>
                            <div class="tb-item--button">
                                <a <?php echo implode( ' ', [ $link_attributes ] ); ?>><?php echo esc_attr($tab['btn_text']); ?><i class="flaticon flaticon-arrow"></i></a>
                            </div>
                        <?php endif; ?>
                    </div> 
                    <div class="tb-item--number"><?php if($key < 10) { echo '0'; } ?><?php echo esc_attr($key+1); ?></div>   
                </div>
            <?php endforeach; ?>
            <?php if(!empty($settings['wg_btn_text'])) : 
                if ( ! empty( $settings['wg_btn_link']['url'] ) ) {
                    $widget->add_render_attribute( 'wg_btn_link', 'href', $settings['wg_btn_link']['url'] );

                    if ( $settings['wg_btn_link']['is_external'] ) {
                        $widget->add_render_attribute( 'wg_btn_link', 'target', '_blank' );
                    }

                    if ( $settings['wg_btn_link']['nofollow'] ) {
                        $widget->add_render_attribute( 'wg_btn_link', 'rel', 'nofollow' );
                    }
                }
                ?>
                <div class="wg-item--button">
                    <a class="btn ct-icon-rotate btn-gradient " <?php ct_print_html($widget->get_render_attribute_string( 'wg_btn_link' )); ?>><?php echo esc_attr($settings['wg_btn_text']); ?><i class="flaticon flaticon-arrow"></i></a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>