<?php
/**
 * Restaurant Food Menu Widget
 *
 * Displays categorized food/drink items with price, image, dietary badges.
 * Uses unlockaddons_restaurant_food_menu_* CSS/ID prefix.
 *
 * @package Unlock_Addons_For_Elementor
 */

namespace Unlockafe_addons\Widgets;

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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Utils;

class Restaurant_Food_Menu extends Widget_Base {

	public function get_name() {
		return 'unlockafe-restaurant-food-menu';
	}

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

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

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

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

	public function get_style_depends() {
		return ['unlockafe-global', 'unlockafe-restaurant-food-menu-style'];
	}

	protected function register_controls() {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	protected function register_content_controls() {
		$this->start_controls_section(
			'section_layout',
			[
				'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'layout',
			[
				'label'   => esc_html__('Layout', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'list'  => esc_html__('List', 'unlock-addons-for-elementor'),
					'grid'  => esc_html__('Grid', 'unlock-addons-for-elementor'),
					'tabs'  => esc_html__('Tabs by Category', 'unlock-addons-for-elementor'),
				],
				'default' => 'list',
			]
		);

		$this->add_responsive_control(
			'columns',
			[
				'label'          => esc_html__('Columns', 'unlock-addons-for-elementor'),
				'type'           => Controls_Manager::SELECT,
				'default'        => '2',
				'tablet_default'  => '2',
				'mobile_default' => '1',
				'options'        => ['1' => '1', '2' => '2', '3' => '3', '4' => '4'],
				'condition'      => ['layout' => 'grid'],
			]
		);

		$this->add_control(
			'show_filter_tabs',
			[
				'label'        => esc_html__('Filter Tabs', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['layout' => 'tabs'],
			]
		);

		$this->add_control(
			'show_all_tab',
			[
				'label'        => esc_html__('Show "All" Tab', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['layout' => 'tabs', 'show_filter_tabs' => 'yes'],
			]
		);

		$this->add_control(
			'show_image',
			[
				'label'        => esc_html__('Show Image', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_price',
			[
				'label'        => esc_html__('Show Price', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'currency_symbol',
			[
				'label'   => esc_html__('Currency Symbol', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'default' => '$',
				'condition' => ['show_price' => 'yes'],
			]
		);

		$this->add_control(
			'show_description',
			[
				'label'        => esc_html__('Show Description', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'description_length',
			[
				'label'     => esc_html__('Description Length', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 120,
				'min'       => 50,
				'max'       => 500,
				'condition' => ['show_description' => 'yes'],
			]
		);

		$this->add_control(
			'show_dietary_badges',
			[
				'label'        => esc_html__('Show Dietary Badges', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_separator',
			[
				'label'        => esc_html__('Show Separator (name–price)', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_sold_out_badge',
			[
				'label'        => esc_html__('Show Sold Out Badge', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_categories',
			[
				'label'     => esc_html__('Categories', 'unlock-addons-for-elementor'),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => ['layout' => 'tabs'],
			]
		);

		$cat_repeater = new Repeater();
		$cat_repeater->add_control('category_name', [
			'label' => esc_html__('Category Name', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXT,
			'default' => esc_html__('Starters', 'unlock-addons-for-elementor'),
		]);
		$cat_repeater->add_control('category_slug', [
			'label' => esc_html__('Slug (unique)', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXT,
			'default' => 'starters',
			'description' => esc_html__('Used to assign menu items to this category.', 'unlock-addons-for-elementor'),
		]);

		$this->add_control(
			'categories',
			[
				'label'       => esc_html__('Categories', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $cat_repeater->get_controls(),
				'title_field' => '{{{ category_name }}}',
				'default'     => [
					['category_name' => esc_html__('Starters', 'unlock-addons-for-elementor'), 'category_slug' => 'starters'],
					['category_name' => esc_html__('Main Course', 'unlock-addons-for-elementor'), 'category_slug' => 'main'],
					['category_name' => esc_html__('Desserts', 'unlock-addons-for-elementor'), 'category_slug' => 'desserts'],
					['category_name' => esc_html__('Drinks', 'unlock-addons-for-elementor'), 'category_slug' => 'drinks'],
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_menu_items',
			[
				'label' => esc_html__('Menu Items', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$item_repeater = new Repeater();
		$item_repeater->add_control('item_name', [
			'label' => esc_html__('Item Name', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXT,
			'default' => esc_html__('Dish Name', 'unlock-addons-for-elementor'),
		]);
		$item_repeater->add_control('item_description', [
			'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXTAREA,
			'default' => '',
		]);
		$item_repeater->add_control('item_price', [
			'label' => esc_html__('Price', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXT,
			'default' => '12.99',
		]);
		$item_repeater->add_control('item_original_price', [
			'label' => esc_html__('Original Price (strikethrough)', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::TEXT,
			'default' => '',
		]);
		$item_repeater->add_control('item_category', [
			'label'   => esc_html__('Category', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::SELECT,
			'default' => 'starters',
			'options' => [
				'starters'  => esc_html__('Starters', 'unlock-addons-for-elementor'),
				'main'      => esc_html__('Main Course', 'unlock-addons-for-elementor'),
				'desserts'  => esc_html__('Desserts', 'unlock-addons-for-elementor'),
				'drinks'    => esc_html__('Drinks', 'unlock-addons-for-elementor'),
			],
		]);
		$item_repeater->add_control('item_image', [
			'label' => esc_html__('Image', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::MEDIA,
			'default' => ['url' => Utils::get_placeholder_image_src()],
		]);
		$item_repeater->add_control('item_dietary', [
			'label'   => esc_html__('Dietary Badges', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::SELECT2,
			'multiple' => true,
			'options' => [
				'vegan' => esc_html__('Vegan', 'unlock-addons-for-elementor'),
				'vegetarian' => esc_html__('Vegetarian', 'unlock-addons-for-elementor'),
				'gluten_free' => esc_html__('Gluten-Free', 'unlock-addons-for-elementor'),
				'halal' => esc_html__('Halal', 'unlock-addons-for-elementor'),
				'spicy' => esc_html__('Spicy', 'unlock-addons-for-elementor'),
				'chefs_special' => esc_html__("Chef's Special", 'unlock-addons-for-elementor'),
				'new' => esc_html__('New', 'unlock-addons-for-elementor'),
				'bestseller' => esc_html__('Bestseller', 'unlock-addons-for-elementor'),
			],
			'default' => [],
		]);
		$item_repeater->add_control('item_available', [
			'label'        => esc_html__('Available', 'unlock-addons-for-elementor'),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
		]);
		$item_repeater->add_control('item_link', [
			'label' => esc_html__('Link', 'unlock-addons-for-elementor'),
			'type'  => Controls_Manager::URL,
		]);

		$this->add_control(
			'menu_items',
			[
				'label'       => esc_html__('Menu Items', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $item_repeater->get_controls(),
				'title_field' => '{{{ item_name }}}',
				'default'     => [
					['item_name' => esc_html__('Caesar Salad', 'unlock-addons-for-elementor'), 'item_description' => 'Crisp romaine, parmesan, croutons.', 'item_price' => '$9.99', 'item_category' => 'starters', 'item_available' => 'yes'],
					['item_name' => esc_html__('Grilled Salmon', 'unlock-addons-for-elementor'), 'item_description' => 'With seasonal vegetables.', 'item_price' => '$18.99', 'item_category' => 'main', 'item_available' => 'yes'],
					['item_name' => esc_html__('Chocolate Cake', 'unlock-addons-for-elementor'), 'item_description' => 'Rich and decadent.', 'item_price' => '$8.99', 'item_category' => 'desserts', 'item_available' => 'yes'],
				],
			]
		);

		$this->end_controls_section();
	}

	protected function register_style_controls() {
		$this->start_controls_section(
			'section_style_container',
			[
				'label' => esc_html__('Section', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'section_bg',
			[
				'label'     => esc_html__('Background', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_container' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'section_padding',
			[
				'label'      => esc_html__('Padding', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors'  => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_item',
			[
				'label' => esc_html__('Item', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'item_bg',
			[
				'label'     => esc_html__('Background', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#FFFFFF',
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_item' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'item_padding',
			[
				'label'      => esc_html__('Padding', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'default'    => ['top' => 16, 'right' => 16, 'bottom' => 16, 'left' => 16],
				'selectors'  => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'item_spacing',
			[
				'label'      => esc_html__('Gap Between Items', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range'      => ['px' => ['min' => 0, 'max' => 60]],
				'default'    => ['unit' => 'px', 'size' => 16],
				'selectors'  => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_list' => 'gap: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_grid' => 'gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'item_border',
				'selector' => '{{WRAPPER}} .unlockaddons_restaurant_food_menu_item',
			]
		);

		$this->add_responsive_control(
			'item_border_radius',
			[
				'label'      => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', '%'],
				'default'    => ['top' => 8, 'right' => 8, 'bottom' => 8, 'left' => 8],
				'selectors'  => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_name',
			[
				'label' => esc_html__('Item Name', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'name_color',
			[
				'label'     => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#1A1A2E',
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_name' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'name_typography',
				'selector' => '{{WRAPPER}} .unlockaddons_restaurant_food_menu_name',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_price',
			[
				'label' => esc_html__('Price', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'price_color',
			[
				'label'     => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#C8102E',
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_restaurant_food_menu_price' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'price_typography',
				'selector' => '{{WRAPPER}} .unlockaddons_restaurant_food_menu_price',
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$s = $this->get_settings_for_display();
		$layout = !empty($s['layout']) ? $s['layout'] : 'list';
		$items = !empty($s['menu_items']) ? $s['menu_items'] : [];
		$categories = !empty($s['categories']) ? $s['categories'] : [];
		$currency = !empty($s['currency_symbol']) ? $s['currency_symbol'] : '$';
		$desc_len = isset($s['description_length']) ? (int) $s['description_length'] : 120;
		$show_image = !empty($s['show_image']) && $s['show_image'] === 'yes';
		$show_price = !empty($s['show_price']) && $s['show_price'] === 'yes';
		$show_desc = !empty($s['show_description']) && $s['show_description'] === 'yes';
		$show_badges = !empty($s['show_dietary_badges']) && $s['show_dietary_badges'] === 'yes';
		$show_sep = !empty($s['show_separator']) && $s['show_separator'] === 'yes';
		$show_sold_out = !empty($s['show_sold_out_badge']) && $s['show_sold_out_badge'] === 'yes';
		$show_tabs = $layout === 'tabs' && !empty($s['show_filter_tabs']) && $s['show_filter_tabs'] === 'yes';
		$show_all_tab = !empty($s['show_all_tab']) && $s['show_all_tab'] === 'yes';

		$cat_slugs = [];
		foreach ($categories as $c) {
			if (!empty($c['category_slug'])) {
				$cat_slugs[$c['category_slug']] = $c['category_name'];
			}
		}

		$container_class = 'unlockaddons_restaurant_food_menu_container';
		$list_class = $layout === 'grid' ? 'unlockaddons_restaurant_food_menu_grid' : 'unlockaddons_restaurant_food_menu_list';
		$cols = $layout === 'grid' ? ($s['columns'] ?? '2') : '1';
		$grid_style = $layout === 'grid' ? ' style="--unlockaddons-rfm-cols: ' . esc_attr($cols) . ';"' : '';
		?>
		<div id="unlockaddons_restaurant_food_menu_container" class="<?php echo esc_attr($container_class); ?>">
			<?php if ($show_tabs && $layout === 'tabs' && !empty($cat_slugs)) : ?>
				<div class="unlockaddons_restaurant_food_menu_tabs">
					<?php if ($show_all_tab) : ?>
						<button type="button" class="unlockaddons_restaurant_food_menu_tab active" data-category=""><?php esc_html_e('All', 'unlock-addons-for-elementor'); ?></button>
					<?php endif; ?>
					<?php foreach ($cat_slugs as $slug => $name) : ?>
						<button type="button" class="unlockaddons_restaurant_food_menu_tab" data-category="<?php echo esc_attr($slug); ?>"><?php echo esc_html($name); ?></button>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>

			<div class="<?php echo esc_attr($list_class); ?>" data-columns="<?php echo esc_attr($cols); ?>"<?php echo $grid_style; ?>>
				<?php foreach ($items as $item) :
					$available = !empty($item['item_available']) && $item['item_available'] === 'yes';
					$cat = !empty($item['item_category']) ? $item['item_category'] : '';
					$name = !empty($item['item_name']) ? $item['item_name'] : '';
					$desc = !empty($item['item_description']) ? $item['item_description'] : '';
					if ($show_desc && $desc_len > 0 && mb_strlen($desc) > $desc_len) {
						$desc = mb_substr($desc, 0, $desc_len) . '…';
					}
					$price = !empty($item['item_price']) ? $item['item_price'] : '';
					$original = !empty($item['item_original_price']) ? $item['item_original_price'] : '';
					$img = !empty($item['item_image']['url']) ? $item['item_image']['url'] : '';
					$dietary = !empty($item['item_dietary']) && is_array($item['item_dietary']) ? $item['item_dietary'] : [];
					$link = !empty($item['item_link']['url']) ? $item['item_link']['url'] : '';
					$item_class = 'unlockaddons_restaurant_food_menu_item';
					if (!$available) $item_class .= ' unlockaddons_restaurant_food_menu_item_sold_out';
					$item_class .= ' unlockaddons_restaurant_food_menu_cat_' . esc_attr($cat);
				?>
					<div class="<?php echo esc_attr($item_class); ?>" data-category="<?php echo esc_attr($cat); ?>">
						<?php if ($show_image && $img) : ?>
							<div class="unlockaddons_restaurant_food_menu_image">
								<?php if ($link) : ?><a href="<?php echo esc_url($link); ?>"><?php endif; ?>
								<img src="<?php echo esc_url($img); ?>" alt="<?php echo esc_attr($name); ?>">
								<?php if ($link) : ?></a><?php endif; ?>
							</div>
						<?php endif; ?>
						<div class="unlockaddons_restaurant_food_menu_content">
							<div class="unlockaddons_restaurant_food_menu_header">
								<?php if ($link) : ?><a href="<?php echo esc_url($link); ?>" class="unlockaddons_restaurant_food_menu_name"><?php echo esc_html($name); ?></a>
								<?php else : ?><span class="unlockaddons_restaurant_food_menu_name"><?php echo esc_html($name); ?></span><?php endif; ?>
								<?php if ($show_sep) : ?><span class="unlockaddons_restaurant_food_menu_sep"></span><?php endif; ?>
								<?php if ($show_price) : ?>
									<span class="unlockaddons_restaurant_food_menu_price_wrap">
										<?php if ($original) : ?><span class="unlockaddons_restaurant_food_menu_original_price"><?php echo esc_html(strpos($original, $currency) === 0 ? $original : $currency . $original); ?></span><?php endif; ?>
										<span class="unlockaddons_restaurant_food_menu_price"><?php echo esc_html(strpos($price, $currency) === 0 ? $price : $currency . $price); ?></span>
									</span>
								<?php endif; ?>
							</div>
							<?php if ($show_desc && $desc) : ?>
								<p class="unlockaddons_restaurant_food_menu_description"><?php echo esc_html($desc); ?></p>
							<?php endif; ?>
							<?php if ($show_badges && !empty($dietary)) : ?>
								<div class="unlockaddons_restaurant_food_menu_badges">
									<?php foreach ($dietary as $badge) : ?>
										<span class="unlockaddons_restaurant_food_menu_badge unlockaddons_restaurant_food_menu_badge_<?php echo esc_attr($badge); ?>"><?php echo esc_html(ucfirst(str_replace('_', ' ', $badge))); ?></span>
									<?php endforeach; ?>
								</div>
							<?php endif; ?>
							<?php if ($show_sold_out && !$available) : ?>
								<span class="unlockaddons_restaurant_food_menu_sold_out"><?php esc_html_e('Sold Out', 'unlock-addons-for-elementor'); ?></span>
							<?php endif; ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}
}
