<?php
/**
 * Table Reservation Widget
 *
 * Restaurant table booking form with date, time, party size, and optional table selection.
 * Uses unlockaddons_table_reservation_* 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;

class Table_Reservation extends Widget_Base {

	public function get_name() {
		return 'unlockafe-table-reservation';
	}

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

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

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

	public function get_keywords() {
		return ['restaurant', 'reservation', 'booking', 'table', 'form'];
	}

	public function get_style_depends() {
		return ['unlockafe-global', 'unlockafe-table-reservation-style'];
	}

	public function get_script_depends() {
		return ['unlockafe-table-reservation-script'];
	}

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

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

		$this->add_control(
			'show_book_table_btn',
			[
				'label'        => esc_html__('Show "Book A Table" Button', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'book_table_btn_icon',
			[
				'label'     => esc_html__('Book A Table Button Icon', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::ICONS,
				'default'   => [
					'value'   => 'eicon-calendar',
					'library' => 'eicons',
				],
				'condition' => ['show_book_table_btn' => 'yes'],
			]
		);

		$this->add_control(
			'book_table_btn_text',
			[
				'label'     => esc_html__('Book A Table Button Text', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Book A Table', 'unlock-addons-for-elementor'),
				'condition' => ['show_book_table_btn' => 'yes'],
			]
		);

		$this->add_control(
			'form_title',
			[
				'label'   => esc_html__('Form Title', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__('TABLE RESERVATION MADE EASY TODAY', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'form_description',
			[
				'label'   => esc_html__('Form Description', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Book your table in advance and skip the wait.', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'form_layout',
			[
				'label'   => esc_html__('Form Layout', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'one_column'   => esc_html__('One Column', 'unlock-addons-for-elementor'),
					'two_columns'  => esc_html__('Two Columns', 'unlock-addons-for-elementor'),
				],
				'default' => 'two_columns',
			]
		);

		$this->add_control(
			'title_tag',
			[
				'label'   => esc_html__('Title HTML Tag', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'h2' => 'H2',
					'h3' => 'H3',
					'h4' => 'H4',
					'h5' => 'H5',
					'div' => 'div',
				],
				'default' => 'h3',
			]
		);

		$this->end_controls_section();

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

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

		$this->add_control(
			'name_required',
			[
				'label'        => esc_html__('Name Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['show_name' => 'yes'],
			]
		);

		$this->add_control(
			'name_placeholder',
			[
				'label'     => esc_html__('Name Placeholder', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Dianne Russell',
				'condition' => ['show_name' => 'yes'],
			]
		);

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

		$this->add_control(
			'email_required',
			[
				'label'        => esc_html__('Email Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['show_email' => 'yes'],
			]
		);

		$this->add_control(
			'email_placeholder',
			[
				'label'     => esc_html__('Email Placeholder', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'info@dinevo.com',
				'condition' => ['show_email' => 'yes'],
			]
		);

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

		$this->add_control(
			'phone_required',
			[
				'label'        => esc_html__('Phone Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'     => 'yes',
				'condition'    => ['show_phone' => 'yes'],
			]
		);

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

		$this->add_control(
			'min_party_size',
			[
				'label'     => esc_html__('Min Party Size', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 50,
				'default'   => 1,
				'condition' => ['show_party_size' => 'yes'],
			]
		);

		$this->add_control(
			'max_party_size',
			[
				'label'     => esc_html__('Max Party Size', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 50,
				'default'   => 20,
				'condition' => ['show_party_size' => 'yes'],
			]
		);

		$this->add_control(
			'party_size_required',
			[
				'label'        => esc_html__('Party Size Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['show_party_size' => 'yes'],
			]
		);

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

		$this->add_control(
			'min_guest_label',
			[
				'label'     => esc_html__('Minimum Guest Label', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Minimum Guest', 'unlock-addons-for-elementor'),
				'condition' => ['show_min_guest' => 'yes'],
			]
		);

		$this->add_control(
			'min_guest_required',
			[
				'label'        => esc_html__('Minimum Guest Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
				'condition'    => ['show_min_guest' => 'yes'],
			]
		);

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

		$this->add_control(
			'max_guest_label',
			[
				'label'     => esc_html__('Maximum Guest Label', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Maximum Guest', 'unlock-addons-for-elementor'),
				'condition' => ['show_max_guest' => 'yes'],
			]
		);

		$this->add_control(
			'max_guest_required',
			[
				'label'        => esc_html__('Maximum Guest Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
				'condition'    => ['show_max_guest' => 'yes'],
			]
		);

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

		$this->add_control(
			'message_label',
			[
				'label'     => esc_html__('Message Field Label', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Message', 'unlock-addons-for-elementor'),
				'condition' => ['show_special_requests' => 'yes'],
			]
		);

		$this->add_control(
			'special_requests_placeholder',
			[
				'label'     => esc_html__('Message Placeholder', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Write your text...',
				'condition' => ['show_special_requests' => 'yes'],
			]
		);

		$this->add_control(
			'message_required',
			[
				'label'        => esc_html__('Message Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
				'condition'    => ['show_special_requests' => 'yes'],
			]
		);

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

		$this->add_control(
			'location_label',
			[
				'label'     => esc_html__('Location Label', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Location', 'unlock-addons-for-elementor'),
				'condition' => ['show_location' => 'yes'],
			]
		);

		$this->add_control(
			'location_placeholder',
			[
				'label'     => esc_html__('Location Placeholder', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'London, UK',
				'condition' => ['show_location' => 'yes'],
			]
		);

		$this->add_control(
			'location_required',
			[
				'label'        => esc_html__('Location Required', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => ['show_location' => 'yes'],
			]
		);

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

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

		$this->end_controls_section();

		$this->start_controls_section(
			'section_required_indicator',
			[
				'label' => esc_html__('Required Indicator (Star)', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'show_required_star',
			[
				'label'        => esc_html__('Show Required Star / Symbol', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'required_star_char',
			[
				'label'     => esc_html__('Required Symbol', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::TEXT,
				'default'   => '*',
				'condition'  => ['show_required_star' => 'yes'],
			]
		);

		$this->add_control(
			'required_star_color',
			[
				'label'     => esc_html__('Required Symbol Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#e74c3c',
				'condition' => ['show_required_star' => 'yes'],
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_table_reservation_required' => 'color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_custom_selection',
			[
				'label' => esc_html__('Custom Selection Fields (e.g. Type of Coffee / How Many Cup)', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'custom_selection_description',
			[
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => esc_html__('Add pairs of "Type" dropdown + "Quantity" dropdown. Example: "Type Of Coffee" with options Espresso, Latte + "How Many Cup" with options 1, 2, 3. Use Add Item to add more (e.g. Type Of Tea, How Many Cup).', 'unlock-addons-for-elementor'),
				'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
			]
		);

		$repeater = new Repeater();

		$repeater->add_control('custom_enabled', [
			'label'        => esc_html__('Enable', 'unlock-addons-for-elementor'),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
		]);

		$repeater->add_control('custom_type_label', [
			'label'   => esc_html__('Type Field Label', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::TEXT,
			'default' => esc_html__('Type Of Coffee', 'unlock-addons-for-elementor'),
		]);

		$repeater->add_control('custom_type_options', [
			'label'       => esc_html__('Type Options (one per line)', 'unlock-addons-for-elementor'),
			'type'        => Controls_Manager::TEXTAREA,
			'default'     => "Espresso\nAmericano\nLatte\nCappuccino",
			'description' => esc_html__('Enter one option per line.', 'unlock-addons-for-elementor'),
		]);

		$repeater->add_control('custom_quantity_label', [
			'label'   => esc_html__('Quantity Field Label', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::TEXT,
			'default' => esc_html__('How Many Cup', 'unlock-addons-for-elementor'),
		]);

		$repeater->add_control('custom_quantity_min', [
			'label'   => esc_html__('Quantity Min', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::NUMBER,
			'min'     => 1,
			'max'     => 50,
			'default' => 1,
		]);

		$repeater->add_control('custom_quantity_max', [
			'label'   => esc_html__('Quantity Max', 'unlock-addons-for-elementor'),
			'type'    => Controls_Manager::NUMBER,
			'min'     => 1,
			'max'     => 50,
			'default' => 10,
		]);

		$repeater->add_control('custom_required', [
			'label'        => esc_html__('Required', 'unlock-addons-for-elementor'),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
		]);

		$this->add_control(
			'custom_selection_fields',
			[
				'label'       => esc_html__('Selection Pairs', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ custom_type_label }}} / {{{ custom_quantity_label }}}',
				'default'     => [
					[
						'custom_enabled'       => 'yes',
						'custom_type_label'   => esc_html__('Type Of Coffee', 'unlock-addons-for-elementor'),
						'custom_type_options' => "Espresso\nAmericano\nLatte\nCappuccino",
						'custom_quantity_label' => esc_html__('How Many Cup', 'unlock-addons-for-elementor'),
						'custom_quantity_min' => 1,
						'custom_quantity_max' => 10,
						'custom_required'     => 'yes',
					],
				],
			]
		);

		$this->end_controls_section();

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

		$this->add_control(
			'opening_time',
			[
				'label'   => esc_html__('Opening Time', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'default' => '11:00',
			]
		);

		$this->add_control(
			'closing_time',
			[
				'label'   => esc_html__('Closing Time', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'default' => '22:00',
			]
		);

		$this->add_control(
			'time_slot_interval',
			[
				'label'   => esc_html__('Time Slot Interval (minutes)', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 15,
				'max'     => 120,
				'default' => 30,
			]
		);

		$this->add_control(
			'advance_booking_max_days',
			[
				'label'   => esc_html__('Advance Booking (max days)', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 90,
				'default' => 30,
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_post_submit',
			[
				'label' => esc_html__('Post-Submission', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'success_message',
			[
				'label'   => esc_html__('Success Message', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__("Thank you! Your table has been reserved. You'll receive a confirmation email shortly.", 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'redirect_url',
			[
				'label' => esc_html__('Redirect After Booking', 'unlock-addons-for-elementor'),
				'type'  => Controls_Manager::URL,
			]
		);

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

		$this->add_control(
			'admin_email',
			[
				'label'   => esc_html__('Admin Email', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'placeholder' => get_option('admin_email'),
			]
		);

		$this->add_control(
			'send_customer_email',
			[
				'label'        => esc_html__('Send Customer Confirmation Email', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'submit_button_text',
			[
				'label'   => esc_html__('Submit Button Text', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__('MAKE A ORDER DONE', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'submit_show_icon',
			[
				'label'        => esc_html__('Show Button Arrow / Icon', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'submit_button_icon',
			[
				'label'     => esc_html__('Submit Button Icon (Arrow)', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::ICONS,
				'skin'      => 'inline',
				'default'   => [
					'value'   => 'eicon-arrow-right',
					'library' => 'eicons',
				],
				'condition' => ['submit_show_icon' => 'yes'],
			]
		);

		$this->add_control(
			'submit_icon_align',
			[
				'label'     => esc_html__('Icon Position', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'right',
				'options'   => [
					'left'  => esc_html__('Before', 'unlock-addons-for-elementor'),
					'right' => esc_html__('After', 'unlock-addons-for-elementor'),
				],
				'condition' => [
					'submit_show_icon' => 'yes',
					'submit_button_icon[value]!' => '',
				],
			]
		);

		$this->end_controls_section();
	}

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

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

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

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

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'form_shadow',
				'selector' => '{{WRAPPER}} .unlockaddons_table_reservation_container',
			]
		);

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

		$this->end_controls_section();

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

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

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

		$this->add_control(
			'title_align',
			[
				'label'     => esc_html__('Alignment', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => ['title' => esc_html__('Left', 'unlock-addons-for-elementor'), 'icon' => 'eicon-text-align-left'],
					'center' => ['title' => esc_html__('Center', 'unlock-addons-for-elementor'), 'icon' => 'eicon-text-align-center'],
					'right'  => ['title' => esc_html__('Right', 'unlock-addons-for-elementor'), 'icon' => 'eicon-text-align-right'],
				],
				'default'   => 'center',
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_table_reservation_title' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_book_btn',
			[
				'label' => esc_html__('Book A Table Button', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
				'condition' => ['show_book_table_btn' => 'yes'],
			]
		);

		$this->add_control(
			'book_btn_color',
			[
				'label'     => esc_html__('Text Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#1a1a2e',
				'selectors' => [
					'{{WRAPPER}} .unlockaddons_table_reservation_book_btn' => 'color: {{VALUE}};',
					'{{WRAPPER}} .unlockaddons_table_reservation_book_btn_icon svg' => 'fill: {{VALUE}};',
				],
			]
		);

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

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

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

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

		$this->end_controls_section();

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

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

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

		$this->end_controls_section();

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

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

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

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

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

		$this->add_responsive_control(
			'submit_border_radius',
			[
				'label'      => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range'      => ['px' => ['min' => 0, 'max' => 50]],
				'default'    => ['unit' => 'px', 'size' => 8],
				'selectors'  => [
					'{{WRAPPER}} .unlockaddons_table_reservation_submit' => 'border-radius: {{SIZE}}{{UNIT}};',
				],
			]
		);

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

		$this->end_controls_section();
	}

	protected function render_required_star($s, $is_required) {
		if (!$is_required) return '';
		if (empty($s['show_required_star']) || $s['show_required_star'] !== 'yes') return '';
		$char = isset($s['required_star_char']) && $s['required_star_char'] !== '' ? $s['required_star_char'] : '*';
		$color = !empty($s['required_star_color']) ? $s['required_star_color'] : '#e74c3c';
		return '<span class="unlockaddons_table_reservation_required" style="color:' . esc_attr($color) . ';">' . esc_html($char) . '</span>';
	}

	protected function render() {
		$s = $this->get_settings_for_display();
		$title = !empty($s['form_title']) ? $s['form_title'] : '';
		$desc  = !empty($s['form_description']) ? $s['form_description'] : '';
		$tag   = !empty($s['title_tag']) ? $s['title_tag'] : 'h3';
		$form_layout = !empty($s['form_layout']) && $s['form_layout'] === 'two_columns' ? 'unlockaddons_table_reservation_fields_two_col' : '';
		$submit_text = !empty($s['submit_button_text']) ? $s['submit_button_text'] : esc_html__('Reserve Table', 'unlock-addons-for-elementor');
		$submit_show_icon = !empty($s['submit_show_icon']) && $s['submit_show_icon'] === 'yes';
		$submit_icon = $submit_show_icon && !empty($s['submit_button_icon']['value']) ? $s['submit_button_icon'] : null;
		$icon_align = !empty($s['submit_icon_align']) ? $s['submit_icon_align'] : 'right';

		$container_id = 'unlockaddons_table_reservation_container';
		$container_class = 'unlockaddons_table_reservation_container';
		?>
		<div id="<?php echo esc_attr($container_id); ?>" class="<?php echo esc_attr($container_class); ?>">
			<?php if (!empty($s['show_book_table_btn']) && $s['show_book_table_btn'] === 'yes') :
				$book_icon = !empty($s['book_table_btn_icon']['value']) ? $s['book_table_btn_icon'] : null;
			?>
				<button type="button" class="unlockaddons_table_reservation_book_btn">
					<?php if ($book_icon) : ?><span class="unlockaddons_table_reservation_book_btn_icon"><?php \Elementor\Icons_Manager::render_icon($book_icon, ['aria-hidden' => 'true']); ?></span><?php endif; ?>
					<span class="unlockaddons_table_reservation_book_btn_text"><?php echo esc_html(!empty($s['book_table_btn_text']) ? $s['book_table_btn_text'] : esc_html__('Book A Table', 'unlock-addons-for-elementor')); ?></span>
				</button>
			<?php endif; ?>
			<?php if ($title) : ?>
				<<?php echo esc_html($tag); ?> class="unlockaddons_table_reservation_title"><?php echo esc_html($title); ?></<?php echo esc_html($tag); ?>>
			<?php endif; ?>
			<?php if ($desc) : ?>
				<p class="unlockaddons_table_reservation_description"><?php echo esc_html($desc); ?></p>
			<?php endif; ?>

			<form class="unlockaddons_table_reservation_form" method="post" action="" data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" data-redirect="<?php echo esc_attr(!empty($s['redirect_url']['url']) ? $s['redirect_url']['url'] : ''); ?>" data-show-summary="<?php echo esc_attr(!empty($s['show_booking_summary']) && $s['show_booking_summary'] === 'yes' ? '1' : '0'); ?>" data-admin-email="<?php echo esc_attr(!empty($s['admin_email']) ? $s['admin_email'] : get_option('admin_email')); ?>" data-send-customer-email="<?php echo esc_attr(!empty($s['send_customer_email']) && $s['send_customer_email'] === 'yes' ? 'yes' : 'no'); ?>" data-success-message="<?php echo esc_attr(!empty($s['success_message']) ? $s['success_message'] : ''); ?>">
				<?php wp_nonce_field('unlockafe_table_reservation_submit', 'unlockafe_reservation_nonce'); ?>

				<div class="unlockaddons_table_reservation_fields <?php echo esc_attr($form_layout); ?>">
					<?php if (!empty($s['show_name']) && $s['show_name'] === 'yes') :
						$name_req = !empty($s['name_required']) && $s['name_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_name" class="unlockaddons_table_reservation_label"><?php esc_html_e('Full Name', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $name_req); ?></label>
							<input type="text" id="unlockaddons_reservation_name" name="guest_name" class="unlockaddons_table_reservation_input" placeholder="<?php echo esc_attr(!empty($s['name_placeholder']) ? $s['name_placeholder'] : ''); ?>" <?php if ($name_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php if (!empty($s['show_email']) && $s['show_email'] === 'yes') :
						$email_req = !empty($s['email_required']) && $s['email_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_email" class="unlockaddons_table_reservation_label"><?php esc_html_e('Email Address', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $email_req); ?></label>
							<input type="email" id="unlockaddons_reservation_email" name="guest_email" class="unlockaddons_table_reservation_input" placeholder="<?php echo esc_attr(!empty($s['email_placeholder']) ? $s['email_placeholder'] : ''); ?>" <?php if ($email_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php
					$custom_fields = !empty($s['custom_selection_fields']) && is_array($s['custom_selection_fields']) ? $s['custom_selection_fields'] : [];
					foreach ($custom_fields as $idx => $row) :
						if (empty($row['custom_enabled']) || $row['custom_enabled'] !== 'yes') continue;
						$type_label = !empty($row['custom_type_label']) ? $row['custom_type_label'] : esc_html__('Type', 'unlock-addons-for-elementor');
						$type_options_raw = !empty($row['custom_type_options']) ? $row['custom_type_options'] : '';
						$type_options = array_filter(array_map('trim', explode("\n", str_replace("\r", "\n", $type_options_raw))));
						if (empty($type_options)) $type_options = [esc_html__('Option 1', 'unlock-addons-for-elementor')];
						$qty_label = !empty($row['custom_quantity_label']) ? $row['custom_quantity_label'] : esc_html__('Quantity', 'unlock-addons-for-elementor');
						$qty_min = isset($row['custom_quantity_min']) ? max(1, (int) $row['custom_quantity_min']) : 1;
						$qty_max = isset($row['custom_quantity_max']) ? min(50, (int) $row['custom_quantity_max']) : 10;
						if ($qty_max < $qty_min) $qty_max = $qty_min;
						$custom_req = !empty($row['custom_required']) && $row['custom_required'] === 'yes';
						$name_type = 'custom_type_' . $idx;
						$name_qty = 'custom_qty_' . $idx;
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_<?php echo esc_attr($name_type); ?>" class="unlockaddons_table_reservation_label"><?php echo esc_html($type_label); ?> <?php echo $this->render_required_star($s, $custom_req); ?></label>
							<select id="unlockaddons_reservation_<?php echo esc_attr($name_type); ?>" name="<?php echo esc_attr($name_type); ?>" class="unlockaddons_table_reservation_input unlockaddons_table_reservation_select" <?php if ($custom_req) echo 'required'; ?>>
								<option value=""><?php esc_html_e('Select...', 'unlock-addons-for-elementor'); ?></option>
								<?php foreach ($type_options as $opt) : ?>
									<option value="<?php echo esc_attr($opt); ?>"><?php echo esc_html($opt); ?></option>
								<?php endforeach; ?>
							</select>
						</div>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_<?php echo esc_attr($name_qty); ?>" class="unlockaddons_table_reservation_label"><?php echo esc_html($qty_label); ?> <?php echo $this->render_required_star($s, $custom_req); ?></label>
							<select id="unlockaddons_reservation_<?php echo esc_attr($name_qty); ?>" name="<?php echo esc_attr($name_qty); ?>" class="unlockaddons_table_reservation_input unlockaddons_table_reservation_select" <?php if ($custom_req) echo 'required'; ?>>
								<?php for ($n = $qty_min; $n <= $qty_max; $n++) : ?>
									<option value="<?php echo esc_attr($n); ?>"><?php echo esc_html((string) $n); ?></option>
								<?php endfor; ?>
							</select>
						</div>
					<?php endforeach; ?>

					<?php if (!empty($s['show_location']) && $s['show_location'] === 'yes') :
						$loc_req = !empty($s['location_required']) && $s['location_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field unlockaddons_table_reservation_field_full">
							<label for="unlockaddons_reservation_location" class="unlockaddons_table_reservation_label"><?php echo esc_html(!empty($s['location_label']) ? $s['location_label'] : esc_html__('Location', 'unlock-addons-for-elementor')); ?> <?php echo $this->render_required_star($s, $loc_req); ?></label>
							<input type="text" id="unlockaddons_reservation_location" name="guest_location" class="unlockaddons_table_reservation_input" placeholder="<?php echo esc_attr(!empty($s['location_placeholder']) ? $s['location_placeholder'] : ''); ?>" <?php if ($loc_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php if (!empty($s['show_phone']) && $s['show_phone'] === 'yes') :
						$phone_req = !empty($s['phone_required']) && $s['phone_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_phone" class="unlockaddons_table_reservation_label"><?php esc_html_e('Phone', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $phone_req); ?></label>
							<input type="tel" id="unlockaddons_reservation_phone" name="guest_phone" class="unlockaddons_table_reservation_input" <?php if ($phone_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php if (!empty($s['show_min_guest']) && $s['show_min_guest'] === 'yes') :
						$min_guest_req = !empty($s['min_guest_required']) && $s['min_guest_required'] === 'yes';
						$min_guest_lbl = !empty($s['min_guest_label']) ? $s['min_guest_label'] : esc_html__('Minimum Guest', 'unlock-addons-for-elementor');
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_min_guest" class="unlockaddons_table_reservation_label"><?php echo esc_html($min_guest_lbl); ?> <?php echo $this->render_required_star($s, $min_guest_req); ?></label>
							<input type="number" id="unlockaddons_reservation_min_guest" name="min_guest" class="unlockaddons_table_reservation_input" min="1" max="50" <?php if ($min_guest_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php if (!empty($s['show_max_guest']) && $s['show_max_guest'] === 'yes') :
						$max_guest_req = !empty($s['max_guest_required']) && $s['max_guest_required'] === 'yes';
						$max_guest_lbl = !empty($s['max_guest_label']) ? $s['max_guest_label'] : esc_html__('Maximum Guest', 'unlock-addons-for-elementor');
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_max_guest" class="unlockaddons_table_reservation_label"><?php echo esc_html($max_guest_lbl); ?> <?php echo $this->render_required_star($s, $max_guest_req); ?></label>
							<input type="number" id="unlockaddons_reservation_max_guest" name="max_guest" class="unlockaddons_table_reservation_input" min="1" max="50" <?php if ($max_guest_req) echo 'required'; ?>>
						</div>
					<?php endif; ?>

					<?php if (!empty($s['show_party_size']) && $s['show_party_size'] === 'yes') :
						$min = isset($s['min_party_size']) ? max(1, (int) $s['min_party_size']) : 1;
						$max = isset($s['max_party_size']) ? min(50, (int) $s['max_party_size']) : 20;
						if ($max < $min) $max = $min;
						$party_req = !empty($s['party_size_required']) && $s['party_size_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field">
							<label for="unlockaddons_reservation_party_size" class="unlockaddons_table_reservation_label"><?php esc_html_e('Party Size', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $party_req); ?></label>
							<select id="unlockaddons_reservation_party_size" name="party_size" class="unlockaddons_table_reservation_input unlockaddons_table_reservation_select" <?php if ($party_req) echo 'required'; ?>>
								<?php for ($i = $min; $i <= $max; $i++) : ?>
									<option value="<?php echo esc_attr($i); ?>"><?php echo esc_html($i); ?></option>
								<?php endfor; ?>
							</select>
						</div>
					<?php endif; ?>

					<?php
					$date_req = !empty($s['date_required']) && $s['date_required'] === 'yes';
					$time_req = !empty($s['time_required']) && $s['time_required'] === 'yes';
					?>
					<div class="unlockaddons_table_reservation_field">
						<label for="unlockaddons_reservation_date" class="unlockaddons_table_reservation_label"><?php esc_html_e('Date', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $date_req); ?></label>
						<input type="date" id="unlockaddons_reservation_date" name="reservation_date" class="unlockaddons_table_reservation_input" <?php if ($date_req) echo 'required'; ?>>
					</div>

					<div class="unlockaddons_table_reservation_field">
						<label for="unlockaddons_reservation_time" class="unlockaddons_table_reservation_label"><?php esc_html_e('Time', 'unlock-addons-for-elementor'); ?> <?php echo $this->render_required_star($s, $time_req); ?></label>
						<input type="time" id="unlockaddons_reservation_time" name="reservation_time" class="unlockaddons_table_reservation_input" <?php if ($time_req) echo 'required'; ?>>
					</div>

					<?php if (!empty($s['show_special_requests']) && $s['show_special_requests'] === 'yes') :
						$msg_req = !empty($s['message_required']) && $s['message_required'] === 'yes';
					?>
						<div class="unlockaddons_table_reservation_field unlockaddons_table_reservation_field_full">
							<label for="unlockaddons_reservation_notes" class="unlockaddons_table_reservation_label"><?php echo esc_html(!empty($s['message_label']) ? $s['message_label'] : esc_html__('Message', 'unlock-addons-for-elementor')); ?> <?php echo $this->render_required_star($s, $msg_req); ?></label>
							<textarea id="unlockaddons_reservation_notes" name="special_requests" class="unlockaddons_table_reservation_input unlockaddons_table_reservation_textarea" placeholder="<?php echo esc_attr(!empty($s['special_requests_placeholder']) ? $s['special_requests_placeholder'] : ''); ?>" <?php if ($msg_req) echo 'required'; ?>></textarea>
						</div>
					<?php endif; ?>
				</div>

				<div class="unlockaddons_table_reservation_actions">
					<button type="submit" class="unlockaddons_table_reservation_submit">
						<?php if ($submit_icon && $icon_align === 'left') : ?>
							<span class="unlockaddons_table_reservation_submit_icon unlockaddons_table_reservation_submit_icon_left"><?php \Elementor\Icons_Manager::render_icon($submit_icon, ['aria-hidden' => 'true']); ?></span>
						<?php endif; ?>
						<span class="unlockaddons_table_reservation_submit_text"><?php echo esc_html($submit_text); ?></span>
						<?php if ($submit_icon && $icon_align === 'right') : ?>
							<span class="unlockaddons_table_reservation_submit_icon unlockaddons_table_reservation_submit_icon_right"><?php \Elementor\Icons_Manager::render_icon($submit_icon, ['aria-hidden' => 'true']); ?></span>
						<?php endif; ?>
					</button>
				</div>

				<div class="unlockaddons_table_reservation_message" style="display:none;" role="status"></div>
				<div class="unlockaddons_table_reservation_summary" style="display:none;"></div>
			</form>
		</div>
		<?php
	}
}
