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

trait Sifency_Projects_Controls {
	protected function add_sifency_projects_controls( $args = [] ) {
        $this->start_controls_section( 'section_content', [ 'label' => 'Portfolio' ] );
        $this->add_control(
            'source',
            [
                'label' => 'Source', 'type' => Controls_Manager::SELECT,
                'options' => [ 'custom' => 'Custom', 'posts' => 'Posts (Requires "portfolio" CPT)' ],
                'default' => 'custom',
            ]
        );
        
        $repeater = new Repeater();
        $repeater->add_control( 'image', [ 'label' => 'Image', 'type' => Controls_Manager::MEDIA, 'default' => [ 'url' => \Elementor\Utils::get_placeholder_image_src() ] ] );
        $repeater->add_control( 'title', [ 'label' => 'Title', 'type' => Controls_Manager::TEXT, 'default' => 'Project Title' ] );
        $repeater->add_control( 'category', [ 'label' => 'Category', 'type' => Controls_Manager::TEXT, 'default' => 'Web Design' ] );
        $repeater->add_control( 'link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ] );
        $this->add_control(
            'project_list',
            [
                'label' => 'Portfolio Items', 'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [ [ 'title' => 'Project #1' ], [ 'title' => 'Project #2' ] ],
                'title_field' => '{{{ title }}}', 'condition' => [ 'source' => 'custom' ],
            ]
        );
        $this->add_control( 'show_read_more', [ 'label' => esc_html__( 'Show Read More', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'separator' => 'before' ] );
        $this->add_control( 'read_more_text', [ 'label' => esc_html__( 'Read More Text', 'sifency-addons' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'Read More', 'sifency-addons' ), 'condition' => [ 'show_read_more' => 'yes' ] ] );
        $this->end_controls_section();

        // --- Query Section ---
        $this->add_sifency_arg_controls();
        $this->update_control('query_post_type', ['default' => 'portfolio']);
        $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' => '3', 'tablet_default' => '2', 'mobile_default' => '1', 'options' => [ '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4' ], 'selectors' => [ '{{WRAPPER}} .sifency-portfolio--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 ], 'selectors' => [ '{{WRAPPER}} .sifency-portfolio--grid' => 'gap: {{SIZE}}px;' ] ]);
        $this->add_group_control( Group_Control_Image_Size::get_type(), [ 'name' => 'thumbnail', 'default' => 'medium_large' ]);
        $this->end_controls_section();
	}
}