<?php
/**
 * Checkout Widget - Content Controls
 *
 * Included inside register_controls() — $this refers to the widget instance.
 *
 * @package GGM_Commerce
 */

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

// Content Section
$this->start_controls_section(
    'content_section',
    [
        'label' => __('Checkout Settings', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'show_shipping_fields',
    [
        'label' => __('Show Shipping Fields', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'enable_order_notes',
    [
        'label' => __('Enable Order Notes', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Enable', 'ggm-commerce'),
        'label_off' => __('Disable', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'required_fields',
    [
        'label' => __('Required Fields', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => [
            'phone' => __('Phone Number', 'ggm-commerce'),
            'company' => __('Company', 'ggm-commerce'),
            'address_2' => __('Address Line 2', 'ggm-commerce'),
        ],
        'default' => ['phone'],
    ]
);

$this->end_controls_section();

// Editor Preview Section (only visible in editor)
$this->start_controls_section(
    'editor_preview_section',
    [
        'label' => __('Editor Preview', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'editor_preview_notice',
    [
        'type' => \Elementor\Controls_Manager::RAW_HTML,
        'raw' => __('Enable preview mode to see dummy data for styling. This only affects the editor view.', 'ggm-commerce'),
        'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
    ]
);

$this->add_control(
    'show_editor_preview',
    [
        'label' => __('Show Preview Data', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Display dummy checkout data in the editor for styling purposes.', 'ggm-commerce'),
    ]
);

$this->add_control(
    'preview_item_count',
    [
        'label' => __('Preview Items', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'options' => [
            '1' => '1 ' . __('item', 'ggm-commerce'),
            '2' => '2 ' . __('items', 'ggm-commerce'),
            '3' => '3 ' . __('items', 'ggm-commerce'),
            '4' => '4 ' . __('items', 'ggm-commerce'),
        ],
        'default' => '2',
        'condition' => [
            'show_editor_preview' => 'yes',
        ],
    ]
);

$this->add_control(
    'preview_show_discount',
    [
        'label' => __('Show Discount Applied', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'condition' => [
            'show_editor_preview' => 'yes',
        ],
    ]
);

$this->add_control(
    'preview_show_shipping_options',
    [
        'label' => __('Show Shipping Options', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'condition' => [
            'show_editor_preview' => 'yes',
        ],
    ]
);

$this->end_controls_section();

// Section Headings Content
$this->start_controls_section(
    'section_headings_content',
    [
        'label' => __('Section Headings', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

// Customer Information
$this->add_control(
    'customer_info_heading_section',
    [
        'label' => __('Customer Information', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
    ]
);

$this->add_responsive_control(
    'customer_info_heading',
    [
        'label' => __('Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Customer Information', 'ggm-commerce'),
    ]
);

$this->add_control(
    'customer_info_heading_tag',
    [
        'label' => __('HTML Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ],
    ]
);

// Shipping Address
$this->add_control(
    'shipping_heading_section',
    [
        'label' => __('Shipping Address', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
        'condition' => [
            'show_shipping_fields' => 'yes',
        ],
    ]
);

$this->add_responsive_control(
    'shipping_heading',
    [
        'label' => __('Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Shipping Address', 'ggm-commerce'),
        'condition' => [
            'show_shipping_fields' => 'yes',
        ],
    ]
);

$this->add_control(
    'shipping_heading_tag',
    [
        'label' => __('HTML Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ],
        'condition' => [
            'show_shipping_fields' => 'yes',
        ],
    ]
);

// Billing Address
$this->add_control(
    'billing_heading_section',
    [
        'label' => __('Billing Address', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_responsive_control(
    'billing_heading',
    [
        'label' => __('Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Billing Address', 'ggm-commerce'),
    ]
);

$this->add_control(
    'billing_heading_tag',
    [
        'label' => __('HTML Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ],
    ]
);

// Payment Method
$this->add_control(
    'payment_heading_section',
    [
        'label' => __('Payment Method', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_responsive_control(
    'payment_heading',
    [
        'label' => __('Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Payment Method', 'ggm-commerce'),
    ]
);

$this->add_control(
    'payment_heading_tag',
    [
        'label' => __('HTML Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ],
    ]
);

// Order Summary
$this->add_control(
    'order_summary_heading_section',
    [
        'label' => __('Order Summary', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_responsive_control(
    'order_summary_heading',
    [
        'label' => __('Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Order Summary', 'ggm-commerce'),
    ]
);

$this->add_control(
    'order_summary_heading_tag',
    [
        'label' => __('HTML Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ],
    ]
);

$this->end_controls_section();

// Order Summary Content Settings
$this->start_controls_section(
    'order_summary_content_section',
    [
        'label' => __('Order Summary Settings', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'show_product_images',
    [
        'label' => __('Show Product Images', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Display product images in the order summary.', 'ggm-commerce'),
    ]
);

$this->add_control(
    'link_product_name',
    [
        'label' => __('Link Product Name', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Link product name to the product page.', 'ggm-commerce'),
    ]
);

$this->add_control(
    'order_summary_sticky',
    [
        'label' => __('Sticky Order Summary', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Make the Order Summary stick to the top when scrolling on desktop.', 'ggm-commerce'),
    ]
);

$this->add_responsive_control(
    'order_summary_sticky_offset',
    [
        'label' => __('Sticky Top Offset', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px'],
        'range' => [
            'px' => [
                'min' => 0,
                'max' => 200,
            ],
        ],
        'default' => [
            'size' => 20,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .order-summary.is-sticky' => 'top: {{SIZE}}{{UNIT}};',
        ],
        'condition' => [
            'order_summary_sticky' => 'yes',
        ],
    ]
);

$this->add_control(
    'order_summary_admin_bar_offset',
    [
        'label' => __('Account for Admin Bar', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Automatically add extra offset when WordPress admin bar is visible.', 'ggm-commerce'),
        'condition' => [
            'order_summary_sticky' => 'yes',
        ],
    ]
);

$this->end_controls_section();

// Discount Code Section (conditional on premium feature)
$discounts_activated = class_exists('GGM_Commerce_Discounts') && GGM_Commerce_Discounts::is_activated();

$this->start_controls_section(
    'discount_code_section',
    [
        'label' => __('Discount Code', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

if (!$discounts_activated) {
    $this->add_control(
        'discount_premium_notice',
        [
            'type' => \Elementor\Controls_Manager::RAW_HTML,
            'raw' => '<div style="padding: 10px; background: #f0f0f0; border-left: 3px solid #667eea;">' .
                     '<strong>' . __('Premium Feature', 'ggm-commerce') . '</strong><br>' .
                     __('Activate the Discounts & Vouchers feature in your license to enable discount code input at checkout.', 'ggm-commerce') .
                     '</div>',
        ]
    );
}

$this->add_control(
    'show_discount_input',
    [
        'label' => __('Show Discount Input', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => $discounts_activated ? 'yes' : '',
        'description' => __('Display a discount code input field in the order summary.', 'ggm-commerce'),
    ]
);

$this->add_control(
    'discount_collapsible',
    [
        'label' => __('Collapsible', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Make the discount input collapsible to save space.', 'ggm-commerce'),
        'condition' => [
            'show_discount_input' => 'yes',
        ],
    ]
);

$this->add_control(
    'discount_label',
    [
        'label' => __('Label Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Have a discount code?', 'ggm-commerce'),
        'condition' => [
            'show_discount_input' => 'yes',
        ],
    ]
);

$this->add_control(
    'discount_placeholder',
    [
        'label' => __('Input Placeholder', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Enter code', 'ggm-commerce'),
        'condition' => [
            'show_discount_input' => 'yes',
        ],
    ]
);

$this->add_control(
    'discount_button_text',
    [
        'label' => __('Button Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Apply', 'ggm-commerce'),
        'condition' => [
            'show_discount_input' => 'yes',
        ],
    ]
);

$this->end_controls_section();
