<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }


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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Divider_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Pricing_List extends Widget_Base {

	use Sifency_Addons_Combine_Button_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Divider_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency_pricing_list',
			'title'         => 'Sifency Pricing List',
			'icon'          => 'eicon-bullet-list',
			'category'      => 'sifency-business-widgets',
			'keywords'      => array( 'pricing', 'list', 'menu', 'price', 'table', 'sifency' ),
			'style_depends' => array( 'sifency-pricing-list-style', 'sifency-icon', 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_header',
			array( 'label' => esc_html__( 'Header', 'sifency-addons' ) )
		);
		$this->add_control(
			'header_title',
			array(
				'label'       => esc_html__( 'Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Basic',
				'label_block' => true,
			)
		);

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

		$this->end_controls_section();

		$this->start_controls_section(
			'section_list',
			array( 'label' => esc_html__( 'Menu Items', 'sifency-addons' ) )
		);
		$repeater = new Repeater();

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

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

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

		$repeater->add_control(
			'original_price',
			array(
				'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',
			array(
				'label' => esc_html__( 'Icon / Badge', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);

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

		$this->add_control(
			'list_items',
			array(
				'label'       => esc_html__( 'Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title' => 'Hello lorem',
						'price' => '$18',
					),
					array(
						'title'          => 'Hello world',
						'price'          => '$23',
						'original_price' => '$33',
						'item_icon'      => array(
							'value'   => 'fas fa-leaf',
							'library' => 'fa-solid',
						),
					),
					array(
						'title' => 'Hello universe!',
						'price' => '$13',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'primary_btn',
				'label'  => 'Primary Button',
			)
		);
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_header',
			array(
				'label' => 'Header',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

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

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

		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => 'Main Title',
				'selector'  => '.sifency-label-style',
				'condition' => array( 'header_title!' => '' ),
				'exclude'   => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_style',
				'label'    => 'Repeted Items',
				'selector' => '.sifency-pl-item',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'List Item Title',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'List Item Subtitle',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'number_style',
				'label'    => 'Price Sale',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'icon_style',
				'label'           => 'Icon Style',
				'hover_selector'  => '.sifency-pl-item:hover .sifency-icon-style, 
								.sifency-pl-item:hover .sifency-icon-style',
				'show_dimensions' => true,
				'exclude'         => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'meta_style',
				'label'    => 'Price Regular',
				'selector' => '.sifency-meta-style',
				'exclude'  => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_Sifency_Addons_Combine_Button_Controls(
			array(
				'prefix'   => 'primary_btn',
				'selector' => '.sifency-addons-primary-button',
			)
		);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<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 ); ?>">

						<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 sifency-icon-style">
										<?php \Elementor\Icons_Manager::render_icon( $item['item_icon'], array( '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>

						<?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>

			<?php $this->add_sifency_addons_combine_button_render( $settings, 'primary_btn', 'sifency-addons-primary-button' ); ?>

		</div>
		<?php
	}
}
