<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Controls_Manager;
use \Elementor\Repeater;
trait Sifency_Clients_Controls {
	protected function add_sifency_clients_controls( $args = [] ) {
        $this->start_controls_section( 'section_content', [ 'label' => 'Clients' ] );
        $this->add_control(
            'source',
            [
                'label' => 'Source', 'type' => Controls_Manager::SELECT,
                'options' => [ 'custom' => 'Custom', 'posts' => 'Posts (Requires "client" CPT)' ],
                'default' => 'custom',
            ]
        );

        // --- Custom Content (Repeater) ---
        $repeater = new Repeater();
        $repeater->add_control( 'client_logo', [ 'label' => 'Logo', 'type' => Controls_Manager::MEDIA, 'default' => [ 'url' => \Elementor\Utils::get_placeholder_image_src() ] ]);
        $repeater->add_control( 'client_name', [ 'label' => 'Name', 'type' => Controls_Manager::TEXT, 'default' => 'Client Name' ] );
        $repeater->add_control( 'link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ] );
        $this->add_control(
            'client_list',
            [
                'label' => 'Client Items', 'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [ [ 'client_name' => 'Client #1' ], [ 'client_name' => 'Client #2' ], [ 'client_name' => 'Client #3' ] ],
                'title_field' => '{{{ client_name }}}',
                'condition' => [ 'source' => 'custom' ],
            ]
        );
        $this->end_controls_section();

        // --- Query Section ---
        $this->add_sifency_arg_controls();
        $this->update_control('query_post_type', ['default' => 'client']);
        $this->update_control('section_query', [ 'condition' => [ 'source' => 'posts' ] ]);
        
        // --- Layout Section ---
        $this->start_controls_section( 'section_layout', [ 'label' => 'Layout' ] );
        $this->add_control( 'layout_style', [ 'label' => 'Layout', 'type' => Controls_Manager::SELECT, 'options' => [ 'grid' => 'Grid', 'carousel' => 'Carousel' ], 'default' => 'grid' ]);
        
        $this->add_responsive_control( 'columns', [ 
            'label' => 'Columns', 
            'type' => Controls_Manager::SELECT, 
            'default' => '4', 
            'tablet_default' => '3', 
            'mobile_default' => '2', 
            'options' => [ '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6' ], 
            'selectors' => [ 
                '{{WRAPPER}} .sifency-clients--grid' => 'display: grid; grid-template-columns: repeat({{VALUE}}, 1fr);' 
            ], 
            'condition' => [ 'layout_style' => 'grid' ] 
        ]);
        
        $this->add_responsive_control( 'gap', [ 
            'label' => 'Gap', 
            'type' => Controls_Manager::SLIDER, 
            'default' => [ 'size' => 30 ], 
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [ 
                '{{WRAPPER}} .sifency-clients--grid' => 'gap: {{SIZE}}{{UNIT}};' 
            ],
            'condition' => [ 'layout_style' => 'grid' ] 
        ]);
        $this->end_controls_section();

	}
}