<?php
namespace Unlockafe_addons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Utils;
use function esc_html__;
use function esc_html;
use function esc_attr;
use function esc_url;
use function wp_kses_post;
use function wp_json_encode;

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

class Price_Menu extends Widget_Base {
	public function get_name() {
		return 'unlockafe-price-menu';
	}

	public function get_title() {
		return esc_html__('Price Menu', 'unlock-addons-for-elementor');
	}

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

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

	public function get_keywords() {
		return ['unlock', 'price', 'menu', 'food', 'restaurant', 'card', 'list'];
	}

	public function get_script_depends() {
		return ['swiper', 'unlockafe-price-menu-script'];
	}

	public function get_style_depends() {
		return ['swiper', 'unlockafe-price-menu-style'];
	}

	protected function register_controls() {
		// --- Layout Section ---
		$this->start_controls_section('layout_section', [
			'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
		]);

		$this->add_control('layout_type', [
			'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
			'type' => Controls_Manager::SELECT,
			'default' => 'price_menu_v1',
			'options' => [
				'price_menu_v1' => esc_html__('List Menu', 'unlock-addons-for-elementor'),
				'recipe_card' => esc_html__('Recipe Card Grid', 'unlock-addons-for-elementor'),
				'popular_items' => esc_html__('Popular Items List (Dark)', 'unlock-addons-for-elementor'),
				'our_menu' => esc_html__('Split Menu with Image', 'unlock-addons-for-elementor'),
				'price_menu_carousel' => esc_html__('Carousel Menu', 'unlock-addons-for-elementor'),
			],
		]);

		$this->add_control('use_demo_data', [
			'label' => esc_html__('Use Demo Content', 'unlock-addons-for-elementor'),
			'type' => Controls_Manager::SWITCHER,
			'label_on' => esc_html__('Yes', 'unlock-addons-for-elementor'),
			'label_off' => esc_html__('No', 'unlock-addons-for-elementor'),
			'return_value' => 'yes',
			'default' => 'yes',
			'description' => esc_html__('Turn this OFF to use the content you add in the "Menu Items" list below.', 'unlock-addons-for-elementor'),
			'separator' => 'before',
		]);

		$this->end_controls_section();

		// --- Header Section ---
		$this->start_controls_section('header_content_section', [
			'label' => esc_html__('Header', 'unlock-addons-for-elementor'),
			'condition' => ['layout_type!' => 'price_menu_carousel'],
		]);
		$this->add_control('heading_text', [
			'label' => 'Heading',
			'type' => Controls_Manager::TEXT,
			'default' => 'Our Price Menu',
			'label_block' => true,
			'dynamic' => ['active' => true],
		]);
		$this->add_control('description_text', [
			'label' => 'Description',
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'Crafted by our chefs with love and passion.',
			'condition' => ['layout_type' => ['recipe_card', 'popular_items']],
			'dynamic' => ['active' => true],
		]);
		$this->end_controls_section();

		// --- Split Menu Section ---
		$this->start_controls_section('our_menu_content_section', [
			'label' => esc_html__('Split Menu Settings', 'unlock-addons-for-elementor'),
			'condition' => ['layout_type' => 'our_menu'],
		]);
		$this->add_control('center_image', [
			'label' => 'Center Image',
			'type' => Controls_Manager::MEDIA,
			'default' => ['url' => 'https://images.unsplash.com/photo-1568901346375-23c9450c58cd?auto=format&fit=crop&w=400&q=80'],
			'dynamic' => ['active' => true],
		]);
		$this->end_controls_section();

		//======================================================================
		// S T Y L E   T A B
		//======================================================================

		// --- Section Header Style --- 
		$this->start_controls_section(
			'section_header_style',
			[
				'label' => esc_html__('Section Header', 'unlock-addons-for-elementor'),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'header_title_color',
			[
				'label' => esc_html__('Title Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .section-title h2, {{WRAPPER}} .carousel-header .carousel-title' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'header_title_typography',
				'label' => esc_html__('Title Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .section-title h2, {{WRAPPER}} .carousel-header .carousel-title',
			]
		);

		$this->add_control(
			'header_description_color',
			[
				'label' => esc_html__('Description Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .section-title p' => 'color: {{VALUE}};',
				],
				'condition' => [
					'layout_type' => ['recipe_card', 'popular_items']
				]
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'header_description_typography',
				'label' => esc_html__('Description Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .section-title p',
				'condition' => [
					'layout_type' => ['recipe_card', 'popular_items']
				]
			]
		);

		$this->end_controls_section();

		// --- Menu Item Style --- 
		$this->start_controls_section(
			'section_menu_item_style',
			[
				'label' => esc_html__('Menu Item', 'unlock-addons-for-elementor'),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'item_title_color',
			[
				'label' => esc_html__('Title Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .price-menu-card .info h3, {{WRAPPER}} .recipe-card .content h3, {{WRAPPER}} .popular-item .info h3, {{WRAPPER}} .burger-menu-item .info h3, {{WRAPPER}} .carousel-card .card-content h3' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'item_title_typography',
				'label' => esc_html__('Title Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .price-menu-card .info h3, {{WRAPPER}} .recipe-card .content h3, {{WRAPPER}} .popular-item .info h3, {{WRAPPER}} .burger-menu-item .info h3, {{WRAPPER}} .carousel-card .card-content h3',
			]
		);

		$this->add_control(
			'item_description_color',
			[
				'label' => esc_html__('Description Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .price-menu-card .info p, {{WRAPPER}} .recipe-card .content p, {{WRAPPER}} .popular-item .info p, {{WRAPPER}} .burger-menu-item .info p, {{WRAPPER}} .carousel-card .card-content p' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'item_description_typography',
				'label' => esc_html__('Description Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .price-menu-card .info p, {{WRAPPER}} .recipe-card .content p, {{WRAPPER}} .popular-item .info p, {{WRAPPER}} .burger-menu-item .info p, {{WRAPPER}} .carousel-card .card-content p',
			]
		);

		$this->add_control(
			'item_price_color',
			[
				'label' => esc_html__('Price Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .price-menu-card .price, {{WRAPPER}} .recipe-card .footer .price, {{WRAPPER}} .popular-item .price, {{WRAPPER}} .burger-menu-item .price, {{WRAPPER}} .carousel-card .carousel-footer .price' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'item_price_typography',
				'label' => esc_html__('Price Typography', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .price-menu-card .price, {{WRAPPER}} .recipe-card .footer .price, {{WRAPPER}} .popular-item .price, {{WRAPPER}} .burger-menu-item .price, {{WRAPPER}} .carousel-card .carousel-footer .price',
			]
		);

		$this->end_controls_section();

		// --- Card Style ---
		$this->start_controls_section(
			'section_card_style',
			[
				'label' => esc_html__('Card Style', 'unlock-addons-for-elementor'),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'card_bg_color',
			[
				'label' => esc_html__('Background Color', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .price-menu-card, {{WRAPPER}} .recipe-card, {{WRAPPER}} .popular-item, {{WRAPPER}} .burger-menu-item' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'card_padding',
			[
				'label' => esc_html__('Padding', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => ['px', '%', 'em'],
				'selectors' => [
					'{{WRAPPER}} .price-menu-card, {{WRAPPER}} .recipe-card, {{WRAPPER}} .popular-item, {{WRAPPER}} .burger-menu-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name' => 'card_border',
				'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .price-menu-card, {{WRAPPER}} .recipe-card, {{WRAPPER}} .popular-item, {{WRAPPER}} .burger-menu-item',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'card_box_shadow',
				'label' => esc_html__('Box Shadow', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .price-menu-card, {{WRAPPER}} .recipe-card, {{WRAPPER}} .popular-item, {{WRAPPER}} .burger-menu-item',
			]
		);

		$this->end_controls_section();

		// --- Menu Items Section ---
		$this->start_controls_section('menu_items_section', [
			'label' => esc_html__('Menu Items', 'unlock-addons-for-elementor'),
			'condition' => ['use_demo_data!' => 'yes'],
		]);

		$repeater = new Repeater();

		$repeater->add_control('item_enabled', [
			'label' => esc_html__('Show this item?', 'unlock-addons-for-elementor'),
			'type' => Controls_Manager::SWITCHER,
			'label_on' => esc_html__('Show', 'unlock-addons-for-elementor'),
			'label_off' => esc_html__('Hide', 'unlock-addons-for-elementor'),
			'return_value' => 'yes',
			'default' => 'yes',
		]);

		$repeater->add_control('item_title', [
			'type' => Controls_Manager::TEXT,
			'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
			'label_block' => true,
			'default' => 'Menu Item',
			'dynamic' => ['active' => true],
		]);
		$repeater->add_control('item_description', [
			'type' => Controls_Manager::TEXTAREA,
			'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
			'default' => 'A delicious description of the menu item.',
			'dynamic' => ['active' => true],
		]);
		$repeater->add_control('item_price', [
			'type' => Controls_Manager::TEXT,
			'label' => esc_html__('Price', 'unlock-addons-for-elementor'),
			'default' => '$14.99',
			'dynamic' => ['active' => true],
		]);
		$repeater->add_control('item_image', [
			'type' => Controls_Manager::MEDIA,
			'label' => esc_html__('Image', 'unlock-addons-for-elementor'),
			'default' => ['url' => Utils::get_placeholder_image_src()],
			'dynamic' => ['active' => true],
		]);
		$repeater->add_control('button_text', [
			'type' => Controls_Manager::TEXT,
			'label' => esc_html__('Button Text (For Carousel)', 'unlock-addons-for-elementor'),
			'default' => 'ADD',
		]);

		$this->add_control('menu_items', [
			'label' => esc_html__('Custom Menu Items List', 'unlock-addons-for-elementor'),
			'type' => Controls_Manager::REPEATER,
			'fields' => $repeater->get_controls(),
			'title_field' => '{{{ item_title }}}',
			'prevent_empty' => false,
			'default' => [
				[
					'item_title' => 'Signature BBQ Beef',
					'item_description' => 'Cheese Kunafa (Arabian Dessert)',
					'item_price' => '$10.48',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
				[
					'item_title' => 'Italian Pasta Sauce',
					'item_description' => 'A timeless classic.',
					'item_price' => '$12.48',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
				[
					'item_title' => 'Crispy Skin Chicken',
					'item_description' => 'Served with a side of greens.',
					'item_price' => '$12.48',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
				[
					'item_title' => 'Prickly Pear Tonic',
					'item_description' => 'Refreshing and unique.',
					'item_price' => '$11.82',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
				[
					'item_title' => 'Wild Mushroom Arancini',
					'item_description' => 'Crispy and savory.',
					'item_price' => '$11.87',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
				[
					'item_title' => 'Fresh Oysters Dozen',
					'item_description' => 'Served on ice.',
					'item_price' => '$12.55',
					'item_image' => ['url' => Utils::get_placeholder_image_src()],
					'button_text' => 'ADD',
					'item_enabled' => 'yes',
				],
			],
		]);
		$this->end_controls_section();

		// --- Carousel Settings Section ---
		$this->start_controls_section('carousel_settings_section', [
			'label' => esc_html__('Carousel Settings', 'unlock-addons-for-elementor'),
			'condition' => ['layout_type' => 'price_menu_carousel'],
		]);
		$this->add_control('carousel_heading', [
			'label' => 'Heading',
			'type' => Controls_Manager::TEXT,
			'default' => "Price Menu Carousel",
			'dynamic' => ['active' => true],
		]);
		$this->add_control('carousel_loop', [
			'label' => 'Infinite Loop',
			'type' => Controls_Manager::SWITCHER,
			'default' => 'yes',
		]);
		$this->add_responsive_control('slides_to_show', [
			'label' => esc_html__('Slides To Show', 'unlock-addons-for-elementor'),
			'type' => Controls_Manager::NUMBER,
			'default' => 4,
			'tablet_default' => 3,
			'mobile_default' => 1,
		]);
		$this->add_control('carousel_navigation', [
			'label' => 'Show Navigation Arrows',
			'type' => Controls_Manager::SWITCHER,
			'default' => 'yes',
		]);
		$this->add_control('carousel_pagination', [
			'label' => 'Show Pagination Dots',
			'type' => Controls_Manager::SWITCHER,
			'default' => 'yes',
		]);
		$this->add_control('space_between', [
			'label' => 'Space Between Slides (px)',
			'type' => Controls_Manager::NUMBER,
			'default' => 24,
		]);
		$this->end_controls_section();
	}
	private function get_demo_data($layout) {
		$data = [
			'price_menu_v1' => [
				['item_title' => 'Signature BBQ Beef', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$10.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Italian Pasta Sauce', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$12.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Crispy Skin Chicken', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$12.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Prickly Pear Tonic', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$11.82', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Wild Mushroom Arancini', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$11.87', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Fresh Oysters Dozen', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$12.55', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
			],
			'recipe_card' => [
				['item_title' => 'Chicken Masala Fries', 'item_description' => 'An exciting fusion of crispy fries and aromatic Indian spices.', 'item_price' => '$18.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1551183053-bf91a1d81141?auto=format&fit=crop&w=400&q=80']],
				['item_title' => 'Gourmet Burger', 'item_description' => 'A timeless classic made with sun-ripened tomatoes and fresh basil.', 'item_price' => '$14.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1568901346375-23c9450c58cd?auto=format&fit=crop&w=400&q=80']],
				['item_title' => 'Crispy Skin Salmon', 'item_description' => 'Juicy salmon with an irresistibly crispy, golden-brown skin.', 'item_price' => '$16.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1519708227418-c8fd9a32b7a2?auto=format&fit=crop&w=400&q=80']],
			],
			'popular_items' => [
				['item_title' => 'Signature BBQ Beef', 'item_description' => 'Slow-smoked to perfection.', 'item_price' => '$12.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Gourmet Fries', 'item_description' => 'Tossed in truffle oil and parmesan.', 'item_price' => '$8.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Crispy Skin Chicken', 'item_description' => 'Served with a side of greens.', 'item_price' => '$18.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&w=100&q=80']],
				['item_title' => 'Baja Fish Tacos', 'item_description' => 'With chipotle mayo and slaw.', 'item_price' => '$12.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1552332386-f8dd00dc2f85?auto=format&fit=crop&w=100&q=80']],
			],
			'our_menu' => [
				['item_title' => 'Chicken Masala Fries', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$6.88'],
				['item_title' => 'Beef Belly Tacos', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$12.48'],
				['item_title' => 'Signature BBQ Beef', 'item_description' => 'Cheese Kunafa (Arabian Dessert)', 'item_price' => '$8.48'],
				['item_title' => 'Gourmet Fries', 'item_description' => 'Tossed in truffle oil and parmesan.', 'item_price' => '$10.48'],
				['item_title' => 'Baja Fish Tacos', 'item_description' => 'With chipotle mayo and slaw.', 'item_price' => '$13.48'],
				['item_title' => 'Italian Pasta Sauce', 'item_description' => 'A timeless classic.', 'item_price' => '$12.48'],
			],
			'price_menu_carousel' => [
				['item_title' => 'Chicha Morada', 'item_description' => 'A refreshing and vibrant purple corn drink.', 'item_price' => '$6.64', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1512621776951-a57141f2eefd?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
				['item_title' => 'Flank Steak Salad', 'item_description' => 'Grilled steak over a bed of fresh greens.', 'item_price' => '$9.40', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1540189549336-e6e99c3679fe?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
				['item_title' => 'Italian Pasta', 'item_description' => 'Classic pasta with a rich tomato sauce.', 'item_price' => '$4.48', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1473093295043-cdd812d0e601?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
				['item_title' => 'French Toast', 'item_description' => 'Drizzled with honey and fresh berries.', 'item_price' => '$4.40', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1482049016688-2d3e1b311543?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
				['item_title' => 'Pepperoni Pizza', 'item_description' => 'A classic favorite with a crispy crust.', 'item_price' => '$11.20', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1555939594-58d7cb561ad1?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
				['item_title' => 'Grilled Chicken', 'item_description' => 'Served with fresh lemon and herbs.', 'item_price' => '$12.80', 'item_image' => ['url' => 'https://images.unsplash.com/photo-1546069901-ba9599a7e63c?auto=format&fit=crop&w=300&q=80'], 'button_text' => 'ADD'],
			]
		];
		return $data[$layout] ?? [];
	}

	// --- RENDER METHODS ---
	protected function render() {
		$settings = $this->get_settings_for_display();
		$this->add_render_attribute('wrapper', 'class', 'unlockafe-price-menu-wrapper');
		
		// Check if we're in Elementor editor or preview mode
		$is_editor_mode = \Elementor\Plugin::$instance->editor->is_edit_mode();
		$is_preview_mode = \Elementor\Plugin::$instance->preview->is_preview_mode();

		echo '<div ' . $this->get_render_attribute_string('wrapper') . '>';
		if ('price_menu_carousel' === $settings['layout_type']) {
			$this->render_price_menu_carousel($settings);
		} else {
			$this->render_layout_with_header($settings);
		}
		echo '</div>';
		
		// Add inline script for editor/preview mode initialization
		if ($is_editor_mode || $is_preview_mode) {
			?>
			<script type="text/javascript">
			(function() {
				function initPriceMenu() {
					if (typeof jQuery === 'undefined') {
						setTimeout(initPriceMenu, 100);
						return;
					}
					
					var $ = jQuery;
					var $widget = $('.elementor-widget-unlockafe-price-menu').last();
					var $widgetWrapper = $widget.find('.unlockafe-price-menu-wrapper');
					
					if ($widgetWrapper.length === 0) {
						setTimeout(initPriceMenu, 100);
						return;
					}
					
					// Make all animated elements visible in editor mode
					var $animatedElements = $widgetWrapper.find('.animate-on-scroll');
					$animatedElements.addClass('is-visible');
					
					// Initialize carousel if present
					var $swiperContainer = $widgetWrapper.find('.swiper-container');
					if ($swiperContainer.length > 0 && typeof Swiper !== 'undefined') {
						var options = $swiperContainer.data('swiper-options');
						if (typeof options !== 'undefined') {
							try {
								var swiperOptions = {
									loop: options.loop || false,
									spaceBetween: options.space_between || 24,
									breakpoints: {
										0: { slidesPerView: options.slides_mobile || 1 },
										768: { slidesPerView: options.slides_tablet || 2 },
										992: { slidesPerView: options.slides_desktop || 4 }
									}
								};
								
								if (options.navigation) {
									swiperOptions.navigation = {
										nextEl: $widget.find('.swiper-button-next')[0],
										prevEl: $widget.find('.swiper-button-prev')[0],
									};
								}
								
								if (options.pagination) {
									swiperOptions.pagination = {
										el: $widget.find('.swiper-pagination')[0],
										clickable: true,
									};
								}
								
								new Swiper($swiperContainer[0], swiperOptions);
							} catch(e) {
								console.error('Price Menu: Error initializing carousel', e);
							}
						}
					}
					
					console.log('Price Menu: Initialized', $animatedElements.length, 'elements in editor mode');
				}
				
				// Start initialization
				if (document.readyState === 'loading') {
					document.addEventListener('DOMContentLoaded', function() {
						setTimeout(initPriceMenu, 100);
					});
				} else {
					setTimeout(initPriceMenu, 100);
				}
				
				// Also try after delays
				setTimeout(initPriceMenu, 300);
				setTimeout(initPriceMenu, 600);
			})();
			</script>
			<?php
		}
	}

	private function render_layout_with_header($settings) {
		$layout = $settings['layout_type'];
		$items = ('yes' === $settings['use_demo_data']) ? $this->get_demo_data($layout) : $settings['menu_items'];
		// Only show enabled items
		$items = array_filter($items, function($item) {
			return empty($item['item_enabled']) || $item['item_enabled'] === 'yes';
		});
		$section_classes = ['section-container'];
		if ('popular_items' === $layout) {
			$section_classes[] = 'dark-section';
		}
		?>
		<section class="<?php echo esc_attr(implode(' ', $section_classes)); ?>">
			<?php $this->render_section_header($settings); ?>
			<?php
			switch ($layout) {
				case 'price_menu_v1':
					$this->render_grid_layout($items, 'price-menu-card');
					break;
				case 'recipe_card':
					$this->render_grid_layout($items, 'recipe-card');
					break;
				case 'popular_items':
					$this->render_grid_layout($items, 'popular-item');
					break;
				case 'our_menu':
					$this->render_our_menu_layout($items, $settings);
					break;
			}
			?>
		</section>
		<?php
	}

	private function render_section_header($settings) {
		if (empty($settings['heading_text']) && 'price_menu_carousel' !== $settings['layout_type']) return;
		$heading_text = 'price_menu_carousel' === $settings['layout_type'] ? $settings['carousel_heading'] : $settings['heading_text'];
		?>
		<div class="section-title animate-on-scroll">
			<h2><?php echo esc_html($heading_text); ?></h2>
			<?php if (!empty($settings['description_text']) && in_array($settings['layout_type'], ['recipe_card', 'popular_items'])): ?>
				<p><?php echo wp_kses_post($settings['description_text']); ?></p>
			<?php endif; ?>
		</div>
		<?php
	}

	private function render_grid_layout($items, $item_class) {
		if (empty($items)) return;
		$grid_class = 'grid-layout' . ('recipe-card' === $item_class ? ' recipe-grid' : '');
		echo '<div class="' . esc_attr($grid_class) . '">';
		foreach ($items as $item) {
			echo '<div class="' . esc_attr($item_class) . ' animate-on-scroll">';
			if ('recipe-card' === $item_class) {
				if (!empty($item['item_image']['url'])) echo '<div class="img-wrapper"><img src="' . esc_url($item['item_image']['url']) . '" alt="' . esc_attr($item['item_title']) . '"></div>';
				echo '<div class="content"><h3>' . esc_html($item['item_title']) . '</h3><p>' . wp_kses_post($item['item_description']) . '</p>';
				echo '<div class="footer"><span class="price">' . esc_html($item['item_price']) . '</span><span class="icon"><svg xmlns="http://www.w3.org/2000/svg" height="16" viewBox="0 96 960 960" width="16"><path d="M200 976V336h560v640L480 816 200 976Z"/></svg></span></div></div>';
			} else {
				if (!empty($item['item_image']['url'])) echo '<img src="' . esc_url($item['item_image']['url']) . '" alt="' . esc_attr($item['item_title']) . '">';
				echo '<div class="info"><h3>' . esc_html($item['item_title']) . '</h3><p>' . wp_kses_post($item['item_description']) . '</p></div>';
				echo '<div class="price">' . esc_html($item['item_price']) . '</div>';
			}
			echo '</div>';
		}
		echo '</div>';
	}
	private function render_our_menu_layout($items, $settings) {
		if (empty($items)) return;
		$columns = array_chunk($items, ceil(count($items) / 2));
		?>
		<div class="burger-menu-layout animate-on-scroll">
			<div class="burger-menu-column">
				<?php if (!empty($columns[0])) foreach ($columns[0] as $item): ?>
					<div class="burger-menu-item">
						<div class="info">
							<h3><?php echo esc_html($item['item_title']); ?></h3>
							<p><?php echo wp_kses_post($item['item_description']); ?></p>
						</div>
						<div class="dots"></div>
						<div class="price"><?php echo esc_html($item['item_price']); ?></div>
					</div>
				<?php endforeach; ?>
			</div>
			<div class="burger-image-container">
				<?php if (!empty($settings['center_image']['url'])): ?>
					<img src="<?php echo esc_url($settings['center_image']['url']); ?>" alt="<?php echo esc_attr($settings['heading_text']); ?>">
				<?php endif; ?>
			</div>
			<div class="burger-menu-column">
				<?php if (isset($columns[1])) foreach ($columns[1] as $item): ?>
					<div class="burger-menu-item">
						<div class="info">
							<h3><?php echo esc_html($item['item_title']); ?></h3>
							<p><?php echo wp_kses_post($item['item_description']); ?></p>
						</div>
						<div class="dots"></div>
						<div class="price"><?php echo esc_html($item['item_price']); ?></div>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	private function render_price_menu_carousel($settings) {
		$items = ('yes' === $settings['use_demo_data']) ? $this->get_demo_data('price_menu_carousel') : $settings['menu_items'];
		// Only show enabled items
		$items = array_filter($items, function($item) {
			return empty($item['item_enabled']) || $item['item_enabled'] === 'yes';
		});
		$opts = [
			'loop' => ($settings['carousel_loop'] === 'yes'),
			'slides_desktop' => $settings['slides_to_show'] ?? 4,
			'slides_tablet' => $settings['slides_to_show_tablet'] ?? 3,
			'slides_mobile' => $settings['slides_to_show_mobile'] ?? 1,
			'navigation' => ($settings['carousel_navigation'] === 'yes'),
			'pagination' => ($settings['carousel_pagination'] === 'yes'),
			'space_between' => $settings['space_between'] ?? 24,
		];

		$this->add_render_attribute('swiper', 'class', ['swiper-container', 'animate-on-scroll']);
		$this->add_render_attribute('swiper', 'data-swiper-options', wp_json_encode($opts));
		?>
		<section class="section-container">
			<div class="carousel-header animate-on-scroll">
				<h2 class="carousel-title"><?php echo esc_html($settings['carousel_heading']); ?></h2>
				<?php if ('yes' === $settings['carousel_navigation']): ?>
					<div class="swiper-navigation-wrapper">
						<div class="swiper-button-prev"></div>
						<div class="swiper-button-next"></div>
					</div>
				<?php endif; ?>
			</div>
			<div <?php echo $this->get_render_attribute_string('swiper'); ?>>
				<div class="swiper-wrapper">
					<?php if (!empty($items)): foreach ($items as $item): ?>
						<div class="swiper-slide">
							<div class="carousel-card">
								<div class="img-wrapper">
									<img src="<?php echo esc_url($item['item_image']['url']); ?>" alt="<?php echo esc_attr($item['item_title']); ?>">
								</div>
								<div class="card-content">
									<h3><?php echo esc_html($item['item_title']); ?></h3>
									<p><?php echo wp_kses_post($item['item_description']); ?></p>
								</div>
								<div class="carousel-footer">
									<span class="price"><?php echo esc_html($item['item_price']); ?></span>
									<button class="add-btn"><?php echo esc_html($item['button_text']); ?></button>
								</div>
							</div>
						</div>
					<?php endforeach; endif; ?>
				</div>
				<?php if ('yes' === $settings['carousel_pagination']): ?>
					<div class="swiper-pagination"></div>
				<?php endif; ?>
			</div>
		</section>
		<?php
	}
}


