<?php
namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Image_Size;

trait Sifency_Career_Controls {

	protected function add_sifency_career_controls( $args = array() ) {

		// --- Content Section ---
		$this->start_controls_section( 'section_content', array( 'label' => 'Career Content' ) );

		$this->add_control(
			'source',
			array(
				'label'   => 'Source',
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'custom' => 'Custom',
					'posts'  => 'Posts',
				),
				'default' => 'custom',
			)
		);

		$this->add_control(
			'layout_style',
			array(
				'label'   => 'Layout Style',
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => array(
					'style-1' => 'Style 1: Horizontal List (Row)',
					'style-2' => 'Style 2: Grid Card (Vertical)',
					'style-3' => 'Style 3: Minimal Border (Clean)',
					'style-4' => 'Style 4: Compact Box (Filled)',
					'style-5' => 'Style 5: Left Icon/Image (Modern)',
				),
			)
		);

		// --- Post Specific Controls ---
		$this->add_control(
			'show_excerpt',
			array(
				'label'     => esc_html__( 'Show Excerpt', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'source' => 'posts' ),
			)
		);

		$this->add_control(
			'excerpt_length',
			array(
				'label'     => esc_html__( 'Excerpt Length', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 5,
						'max' => 100,
					),
				),
				'default'   => array( 'size' => 20 ),
				'condition' => array( 'show_excerpt' => 'yes' ),
			)
		);

		// --- Custom Repeater ---
		$repeater = new Repeater();

		$repeater->add_control(
			'career_image',
			array(
				'label'   => 'Image/Logo',
				'type'    => Controls_Manager::MEDIA,
				'default' => array( 'url' => \Elementor\Utils::get_placeholder_image_src() ),
			)
		);

		$repeater->add_control(
			'title',
			array(
				'label'   => 'Job Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Senior Developer',
			)
		);
		$repeater->add_control(
			'location',
			array(
				'label'   => 'Location',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Remote',
			)
		);
		$repeater->add_control(
			'job_type',
			array(
				'label'   => 'Job Type',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Full-time',
			)
		);
		$repeater->add_control(
			'salary',
			array(
				'label' => 'Salary',
				'type'  => Controls_Manager::TEXT,
			)
		);
		$repeater->add_control(
			'excerpt',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Job description goes here.',
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'   => 'Apply Link',
				'type'    => Controls_Manager::URL,
				'default' => array( 'is_external' => 'true' ),
			)
		);

		$this->add_control(
			'career_list',
			array(
				'label'       => 'Career Items',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'    => 'Senior Product Designer',
						'location' => 'Remote',
						'job_type' => 'Full-time',
					),
					array(
						'title'    => 'Lead Developer',
						'location' => 'New York',
						'job_type' => 'Full-time',
					),
				),
				'title_field' => '{{{ title }}}',
				'condition'   => array( 'source' => 'custom' ),
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'thumbnail',
				'default'   => 'thumbnail',
				'separator' => 'before',
			)
		);
		$this->end_controls_section();

		// --- Icons Section ---
		$this->start_controls_section( 'section_icons', array( 'label' => 'Icons Settings' ) );

		$this->add_control(
			'location_icon',
			array(
				'label'   => 'Location Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-location',
					'library' => 'sifency-icons',
				),
			)
		);

		$this->add_control(
			'type_icon',
			array(
				'label'   => 'Job Type Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-briefcase',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->add_control(
			'salary_icon',
			array(
				'label'   => 'Salary Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-dollar',
					'library' => 'sifency-icons',
				),
			)
		);

		$this->add_control(
			'button_icon',
			array(
				'label'   => 'Button Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-right-open-big',
					'library' => 'sifency-icons',
				),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_arg_controls(
			array(
				'default_post' => 'career',
				'condition'    => array( 'source' => 'posts' ),
			)
		);

		$this->start_controls_section( 'section_layout', array( 'label' => 'Grid Settings' ) );
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => 'Columns',
				'type'      => Controls_Manager::SELECT,
				'default'   => '1',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'selectors' => array( '{{WRAPPER}} .sifency-career-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 30 ),
				'selectors' => array( '{{WRAPPER}} .sifency-career-grid' => 'gap: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'read_more_text',
			array(
				'label'   => 'Button Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Apply Now',
			)
		);
		$this->end_controls_section();
	}
}
