<?php

namespace Lknwp\CrowdfundingExtension\Includes\elementorFields\controls;

use Elementor\Controls_Manager;

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

class LknwpCrowdfundingListingMainContent
{
    public static function getControl($input)
    {
        $current_page_id = get_the_ID();

        if (!function_exists(__NAMESPACE__ . '\\lkn_get_descendants')) {
            function lkn_get_descendants($parent_id, $post_type = 'page')
            {
                $descendants = [];

                // Buscar filhos diretos
                $children = get_posts([
                    'post_type'   => $post_type,
                    'post_status' => 'publish',
                    'numberposts' => -1,
                    'post_parent' => $parent_id,
                ]);

                foreach ($children as $child) {
                    $descendants[] = $child;

                    $descendants = array_merge($descendants, lkn_get_descendants($child->ID, $post_type));
                }

                return $descendants;
            }
        }

        $pages = lkn_get_descendants($current_page_id);

        // Formatar as opções para o campo select
        $page_options = ['none' => __('Default', 'link-nacional-wp-crowdfunding-extension')];
        if (!empty($pages)) {
            foreach ($pages as $page) {
                $page_options[$page->post_name] = $page->post_name;
            }
        }

        $input->add_control(
            'lkn_template_page',
            [
                'label'       => __('Campaign Link', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::SELECT,
                'options'     => $page_options,
                'default'     => key($page_options),
                'description' => __('Customize the campaign details page link.', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        // Campo para categoria
        $input->add_control(
            'category',
            [
                'label'       => __('Category', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::TEXT,
                'description' => __('Add category slugs separated by commas.', 'link-nacional-wp-crowdfunding-extension'),
                'placeholder' => __('e.g. technology, health', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        // Campo para número de posts
        $input->add_control(
            'number',
            [
                'label'       => __('Number of Posts per section', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::NUMBER,
                'default'     => 10,
            ]
        );

        // Campo para ordenação
        $input->add_control(
            'order',
            [
                'label'       => __('Order', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::SELECT,
                'options'     => [
                    'ASC'  => __('Ascending', 'link-nacional-wp-crowdfunding-extension'),
                    'DESC' => __('Descending', 'link-nacional-wp-crowdfunding-extension'),
                    'RAND' => __('Random', 'link-nacional-wp-crowdfunding-extension'),
                ],
                'default'     => 'DESC',
            ]
        );

        $input->add_control(
            'divider',
            [
                'type' => Controls_Manager::DIVIDER,
            ]
        );

        $input->add_control(
            'main_background_color',
            [
                'label'     => __('Background Color', 'link-nacional-wp-crowdfunding-extension'),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#FFFFFF',
            ]
        );

        // Campo para Largura - Responsivo
        $input->add_responsive_control(
            'width',
            [
                'label'       => __('Width', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .lknwp-elementor-width' => 'width: {{SIZE}}{{UNIT}};',
                ],
                'size_units'  => ['px'],
                'range'       => [
                    'px' => [
                        'min' => 80,
                        'max' => 1300,
                        'step' => 1,
                    ]
                ],
                'default'     => [
                    'unit' => 'px',
                    'size' => 300,
                ],
                'devices' => [ 'desktop', 'tablet', 'mobile' ],
                'desktop_default' => [
                    'size' => 300,
                    'unit' => 'px',
                ],
                'tablet_default' => [
                    'size' => 300,
                    'unit' => 'px',
                ],
                'mobile_default' => [
                    'size' => 300,
                    'unit' => 'px',
                ],
                'description' => __('Minimum width is 240px.', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        // Campo para Altura - Responsivo
        $input->add_responsive_control(
            'height',
            [
                'label'       => __('Height', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .lknwp-elementor-height' => 'height: {{SIZE}}{{UNIT}};',
                ],
                'size_units'  => ['px'],
                'range'       => [
                    'px' => [
                        'min' => 80,
                        'max' => 2000,
                        'step' => 1,
                    ]
                ],
                'default'     => [
                    'unit' => 'px',
                    'size' => 640,
                ],
                'devices' => [ 'desktop', 'tablet', 'mobile' ],
                'desktop_default' => [
                    'size' => 600,
                    'unit' => 'px',
                ],
                'tablet_default' => [
                    'size' => 600,
                    'unit' => 'px',
                ],
                'mobile_default' => [
                    'size' => 600,
                    'unit' => 'px',
                ],
                'description' => __('Minimum height is 230px.', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        $input->add_responsive_control(
            'gap',
            [
                'label'       => __('Gap', 'link-nacional-wp-crowdfunding-extension'),
                'type'        => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .lknwp-elementor-gap' => 'gap: {{SIZE}}{{UNIT}};',
                ],
                'size_units'  => ['px'],
                'range'       => [
                    'px' => [
                        'min' => 5,
                        'max' => 50,
                        'step' => 1,
                    ]
                ],
                'default'     => [
                    'unit' => 'px',
                    'size' => 5,
                ],
                'devices' => [ 'desktop', 'tablet', 'mobile' ],
                'desktop_default' => [
                    'size' => 5,
                    'unit' => 'px',
                ],
                'tablet_default' => [
                    'size' => 5,
                    'unit' => 'px',
                ],
                'mobile_default' => [
                    'size' => 5,
                    'unit' => 'px',
                ],
                'description' => __('Minimum gap is 5px.', 'link-nacional-wp-crowdfunding-extension'),
            ]
        );

        return $input;
    }
}
