<?php
namespace Elementor;

/**
 * Elementor Widget
 * @package Geroz
 * @since 1.0.0
 */ 
 
class Services_Filter extends Widget_Base {

	/**
	 * Get widget name.
	 * Retrieve button widget name.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'geroz-services-filter-widget';
	}

	/**
	 * Get widget title.
	 * Retrieve button widget title.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Services Filter', 'geroz-core' );
	}

	/**
	 * Get widget icon.
	 * Retrieve button widget icon.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-flash';
	}

	/**
	 * Get widget categories.
	 * Retrieve the list of categories the button widget belongs to.
	 * Used to determine where to display the widget in the editor.
	 *
	 * @since  2.0.0
	 * @access public
	 * @return array Widget categories.
	 */
	public function get_categories()
    {
        return ['geroz_widgets'];
    }
	
	/**
	 * Register button widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since  1.0.0
	 * @access protected
	 */
	protected function register_controls() {

        $this->start_controls_section(
            'services_filter_content',
            [
                'label' => __( 'Services Filter Content', 'geroz-core' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'subtitle',
            [
                'label' => __( 'Sub Title', 'geroz-core' ),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__('Get specialist cargo services worldwide', 'geroz-core'),
                'dynamic' => [ 'active' => true ],
            ]
        );

        $this->add_control(
            'title',
            [
                'label' => __( 'Title', 'geroz-core' ),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__( 'Our Services', 'geroz-core' ),
                'dynamic' => [ 'active' => true ],
            ]
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'section_image',
            [
                'label' => __( 'Image', 'geroz-core' ),
                'type' => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'section_title',
            [
                'label' => __( 'Title', 'geroz-core' ),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__( 'Service Title', 'geroz-core' ),
            ]
        );

        $repeater->add_control(
            'section_button_link',
            [
                'label' => __( 'Button Link', 'geroz-core' ),
                'type' => Controls_Manager::URL,
                'default' => [ 'url' => '#' ],
            ]
        );

        $repeater->add_control(
            'section_class',
            [
                'label' => __( 'Section Class', 'geroz-core' ),
                'type' => Controls_Manager::TEXT,
                'default' => 'mt-4 pt-2',
            ]
        );

        $repeater->add_control(
            'section_category',
            [
                'label' => __( 'Category (slug)', 'geroz-core' ),
                'type' => Controls_Manager::TEXT,
                'default' => 'Marketing',
                'description' => 'Use a simple word like Marketing, Business, etc.',
            ]
        );

        $this->add_control(
            'repeat',
            [
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'title_field' => '{{ section_title }}',
            ]
        );

        $this->end_controls_section();
    }

	/**
	 * Render button widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since  1.0.0
	 * @access protected
	 */
	protected function render() {
        $settings = $this->get_settings_for_display();
        $allowed_tags = wp_kses_allowed_html( 'post' );
        $services = $settings['repeat'];

        // Get unique categories
        $categories = ['All-Service'];
        foreach ( $services as $item ) {
            $cat = sanitize_title( $item['section_category'] );
            if ( ! in_array( $cat, $categories ) ) {
                $categories[] = $cat;
            }
        }
        ?>
        <section class="service-wrapper service-5 service-page">
            <div class="container">
                <?php if ( $settings['title'] ) : ?>
                    <div class="section-title section-title-font text-center">
                        <div class="sub-title">
                            <div class="dot"></div> 
                            <span><?php echo $settings['subtitle']; ?></span>
                        </div>
                        <h2 class="tp-char-animation"><?php echo $settings['title']; ?></h2>
                    </div>
                <?php endif; ?>

                <div class="product-header text-center">
                    <ul class="nav">
                        <?php foreach ( $categories as $index => $cat ) : ?>
                            <li class="nav-item wow fadeInUp" data-wow-delay=".<?php echo 2 + ($index * 2); ?>s">
                                <a href="#<?php echo esc_attr( $cat ); ?>" data-bs-toggle="tab" class="nav-link <?php echo $index === 0 ? 'active' : ''; ?>">
                                    <?php echo esc_html( ucfirst( $cat ) ); ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>

                <div class="tab-content wow fadeInUp" data-wow-delay="400ms">
                    <?php foreach ( $categories as $index => $cat ) : ?>
                        <div id="<?php echo esc_attr( $cat ); ?>" class="tab-pane fade <?php echo $index === 0 ? 'show active' : ''; ?>">
                            <div class="row mt-5">
                                <?php foreach ( $services as $item ) :
                                    $item_cat = sanitize_title( $item['section_category'] );
                                    if ( $cat !== 'All-Service' && $item_cat !== $cat ) continue;
                                ?>
                                    <div class="col-xl-3 col-lg-4 col-md-6 col-sm-6 col-12 mb-30">
                                        <a href="<?php echo esc_url( $item['section_button_link']['url'] ); ?>">
                                            <div class="single-service-item fix tp-hover-btn-wrapper <?php echo esc_attr( $item['section_class'] ); ?> wow fadeInLeft"
                                                data-wow-delay="400ms">
                                                <div class="image tp-hover-btn-item">
                                                    <?php if ( ! empty( $item['section_image']['url'] ) ) : ?>
                                                        <img src="<?php echo esc_url( $item['section_image']['url'] ); ?>" alt="">
                                                    <?php endif; ?>
                                                </div>
                                                <h4><?php echo wp_kses( $item['section_title'], $allowed_tags ); ?></h4>
                                            </div>
                                        </a>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
        <?php
    }


}

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