<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Controls_Manager;
use \Elementor\Repeater;
trait Sifency_Event_Controls {

	protected function add_sifency_event_controls( $args = [] ) {
        $this->start_controls_section( 'section_content', [ 'label' => 'Events' ] );
        $this->add_control( 'source', [ 'label' => 'Source', 'type' => Controls_Manager::SELECT, 'options' => [ 'custom' => 'Custom', 'posts' => 'Posts (Requires "event" CPT)' ], 'default' => 'custom' ] );
        
        $repeater = new Repeater();
        $repeater->add_control( 'title', [ 'label' => 'Title', 'type' => Controls_Manager::TEXT, 'default' => 'Event Title' ] );
        $repeater->add_control( 'date', [ 'label' => 'Date', 'type' => Controls_Manager::TEXT, 'default' => 'Jan 01, 2024' ] );
        $repeater->add_control( 'location', [ 'label' => 'Location', 'type' => Controls_Manager::TEXT, 'default' => 'New York, USA' ] );
        $repeater->add_control( 'excerpt', [ 'label' => 'Description', 'type' => Controls_Manager::TEXTAREA ] );
        $repeater->add_control( 'link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ] );
        $this->add_control(
            'event_list',
            [
                'label' => 'Event Items', 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(),
                'default' => [
                    [ 'title' => 'Community Meetup', 'date' => 'May 25, 2024', 'location' => 'Online' ],
                    [ 'title' => 'Design Conference', 'date' => 'June 10, 2024', 'location' => 'San Francisco, CA' ],
                ],
                'title_field' => '{{{ title }}}', 'condition' => [ 'source' => 'custom' ],
            ]
        );
        $this->end_controls_section();

        // --- Query Section ---
        $this->add_sifency_arg_controls();
        $this->update_control( 'section_query', [ 'condition' => [ 'source' => 'posts' ] ] );
        $this->update_control('query_post_type', ['default' => 'event']);
        
        // --- Layout Section ---
        $this->start_controls_section( 'section_layout', [ 'label' => 'Layout' ] );
        $this->add_responsive_control(
            'columns',
            [ 'label' => 'Columns', 'type' => Controls_Manager::SELECT, 'default' => '1', 'options' => [ '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4' ], 'selectors' => [ '{{WRAPPER}} .sifency-event-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ] ]
        );
        $this->add_responsive_control( 'gap', [ 'label' => 'Gap', 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 30 ], 'selectors' => [ '{{WRAPPER}} .sifency-event-grid' => 'gap: {{SIZE}}px;' ] ]);
        $this->add_control( 'read_more_text', [ 'label' => 'Button Text', 'type' => Controls_Manager::TEXT, 'default' => 'View Event' ]);
        $this->end_controls_section();
	}
}