<?php
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_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Elementor\Repeater;
use Sifency\Traits\Sifency_Button_Style_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Pricing_List extends Widget_Base {
    use Sifency_Button_Style_Controls;
    use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency_pricing_list';
	}

	public function get_title() {
		return esc_html__( 'Sifency Pricing List', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-bullet-list';
	}

	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_style_depends() {
		return [ 'sifency-pricing-list-style' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_header',
			[ 'label' => esc_html__( 'Header', 'sifency-addons' ) ]
		);

		$this->add_control( 'header_title', [
			'label' => esc_html__( 'Title', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Basic',
			'label_block' => true,
		]);

		$this->add_control( 'show_divider', [
			'label' => esc_html__( 'Show Divider', 'sifency-addons' ),
			'type' => Controls_Manager::SWITCHER,
			'default' => 'yes',
		]);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_list',
			[ 'label' => esc_html__( 'Menu Items', 'sifency-addons' ) ]
		);

		$repeater = new Repeater();

		$repeater->add_control( 'title', [
			'label' => esc_html__( 'Item Name', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Menu Item Name',
			'label_block' => true,
		]);

		$repeater->add_control( 'description', [
			'label' => esc_html__( 'Description', 'sifency-addons' ),
			'type' => Controls_Manager::TEXTAREA,
			'rows' => 2,
			'default' => 'Lorem ipsum dolor sit amet',
		]);

		$repeater->add_control( 'price', [
			'label' => esc_html__( 'Price', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => '$18',
		]);

		$repeater->add_control( 'original_price', [
			'label' => esc_html__( 'Original Price (For Sale)', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'placeholder' => '$25',
			'description' => 'Leave empty if not on sale.',
		]);

		$repeater->add_control( 'item_icon', [
			'label' => esc_html__( 'Icon / Badge', 'sifency-addons' ),
			'type' => Controls_Manager::ICONS,
		]);

		$repeater->add_control( 'link', [
			'label' => esc_html__( 'Item Link', 'sifency-addons' ),
			'type' => Controls_Manager::URL,
			'placeholder' => 'https://your-link.com',
		]);

		$this->add_control(
			'list_items',
			[
				'label' => esc_html__( 'Items', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'title' => 'Hello lorem', 'price' => '$18' ],
					[ 'title' => 'Hello world', 'price' => '$23', 'original_price' => '$33', 'item_icon' => [ 'value' => 'fas fa-leaf', 'library' => 'fa-solid' ] ],
					[ 'title' => 'Hello universe!', 'price' => '$13' ],
				],
				'title_field' => '{{{ title }}}',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_button',
			[ 'label' => esc_html__( 'Footer Button', 'sifency-addons' ) ]
		);

		$this->add_control( 'button_text', [
			'label' => esc_html__( 'Button Text', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'See All',
		]);

		$this->add_control( 'button_link', [
			'label' => esc_html__( 'Link', 'sifency-addons' ),
			'type' => Controls_Manager::URL,
			'default' => [ 'url' => '#' ],
		]);

		$this->end_controls_section();

		$this->start_controls_section( 'style_header', [ 'label' => 'Header', 'tab' => Controls_Manager::TAB_STYLE ] );
			
		$this->add_control( 'divider_color', [ 'label' => 'Divider Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-pl-divider' => 'background-color: {{VALUE}};' ], 'condition' => [ 'show_divider' => 'yes' ] ] );
		$this->add_responsive_control( 'header_spacing', [ 'label' => 'Bottom Spacing', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-pl-header' => 'margin-bottom: {{SIZE}}px;' ] ] );

		$this->end_controls_section();
		$this->start_controls_section( 'style_list', [ 'label' => 'List Items', 'tab' => Controls_Manager::TAB_STYLE ] );

		$this->add_responsive_control( 'item_gap', [ 'label' => 'Space Between Items', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-pl-item' => 'margin-bottom: {{SIZE}}px;' ] ] );
		// Leader Line
		$this->add_control( 'line_heading', [ 'label' => 'Leader Line', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'line_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'default' => '#e5e5e5', 'selectors' => [ '{{WRAPPER}} .sifency-pl-line' => 'border-bottom-color: {{VALUE}};' ] ] );
		$this->add_control( 'line_style', [
			'label' => 'Style',
			'type' => Controls_Manager::SELECT,
			'options' => [ 'dotted' => 'Dotted', 'dashed' => 'Dashed', 'solid' => 'Solid' ],
			'default' => 'solid',
			'selectors' => [ '{{WRAPPER}} .sifency-pl-line' => 'border-bottom-style: {{VALUE}};' ],
		]);

		$this->end_controls_section();
		$this->start_controls_section( 'style_icon', [ 'label' => 'Icons', 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_control( 'icon_color', [ 'label' => 'Icon Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-pl-icon' => 'color: {{VALUE}};' ] ] );
		$this->add_responsive_control( 'icon_size', [ 'label' => 'Size', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-pl-icon' => 'font-size: {{SIZE}}px; width: {{SIZE}}px;' ] ] );
		$this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Main Title',
            'selector' => '.sifency-label-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'List Item Title',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'List Item Subtitle',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'number_style',
            'label'    => 'Price Sale',
            'selector' => '.sifency-number-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Price Regular',
            'selector' => '.sifency-meta-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls([
            'prefix'   => 'button_style',
            'label'    => 'Button',
            'selector' => '.sifency-button-style',
            'target_children' => false,
        ]);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		?>
		<div class="sifency-pricing-list-wrapper">
			
			<!-- Header -->
			<div class="sifency-pl-header">
				<h3 class="sifency-label-style"><?php echo esc_html( $settings['header_title'] ); ?></h3>
				<?php if ( 'yes' === $settings['show_divider'] ) : ?>
					<div class="sifency-pl-divider"></div>
				<?php endif; ?>
			</div>

			<!-- List -->
			<ul class="sifency-pl-items">
				<?php foreach ( $settings['list_items'] as $item ) : 
					$has_sale = ! empty( $item['original_price'] );
					$item_class = $has_sale ? 'has-sale' : '';
					$link_url = $item['link']['url'];
				?>
					<li class="sifency-pl-item <?php echo esc_attr( $item_class ); ?>">
						
						<!-- Top Row: Name --- Line --- Price -->
						<div class="sifency-pl-top-row">
							<div class="sifency-pl-name-wrap">
								
								<?php if ( ! empty( $link_url ) ) : ?>
									<a href="<?php echo esc_url( $link_url ); ?>" class="sifency-pl-link">
								<?php endif; ?>

								<span class="sifency-pl-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></span>
								
								<?php if ( ! empty( $item['item_icon']['value'] ) ) : ?>
									<span class="sifency-pl-icon">
										<?php \Elementor\Icons_Manager::render_icon( $item['item_icon'], [ 'aria-hidden' => 'true' ] ); ?>
									</span>
								<?php endif; ?>

								<?php if ( ! empty( $link_url ) ) : ?>
									</a>
								<?php endif; ?>

							</div>

							<div class="sifency-pl-line"></div>

							<div class="sifency-pl-price-wrap">
								<?php if ( $has_sale ) : ?>
									<span class="sifency-pl-price-old sifency-meta-style"><?php echo esc_html( $item['original_price'] ); ?></span>
								<?php endif; ?>
								<span class="sifency-pl-price-current sifency-number-style"><?php echo esc_html( $item['price'] ); ?></span>
							</div>
						</div>

						<!-- Bottom Row: Description -->
						<?php if ( ! empty( $item['description'] ) ) : ?>
							<div class="sifency-pl-desc sifency-content-style">
								<?php echo esc_html( $item['description'] ); ?>
							</div>
						<?php endif; ?>

					</li>
				<?php endforeach; ?>
			</ul>

			<!-- Footer Button -->
			<?php if ( ! empty( $settings['button_text'] ) ) : ?>
				<div class="sifency-pl-btn-wrap">
					<a href="<?php echo esc_url( $settings['button_link']['url'] ); ?>" class="sifency-pl-btn sifency-button-style">
						<?php echo esc_html( $settings['button_text'] ); ?>
					</a>
				</div>
			<?php endif; ?>

		</div>
		<?php
	}
}