<?php
/**
 * Product Grid Widget — TAB_CONTENT controls
 * Included via require inside register_controls(). $this = widget instance.
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

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

$this->add_control(
    'products_per_page',
    [
        'label' => __('Products Per Page', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'default' => 12,
        'min' => 1,
        'max' => 100,
    ]
);

$this->add_control(
    'orderby',
    [
        'label' => __('Order By', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'created_at',
        'options' => [
            'created_at' => __('Date Created', 'ggm-commerce'),
            'updated_at' => __('Date Modified', 'ggm-commerce'),
            'name' => __('Name', 'ggm-commerce'),
            'price' => __('Price', 'ggm-commerce'),
            'stock' => __('Stock', 'ggm-commerce'),
            'featured' => __('Featured First', 'ggm-commerce'),
        ],
    ]
);

$this->add_control(
    'order',
    [
        'label' => __('Order', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'DESC',
        'options' => [
            'ASC' => __('Ascending', 'ggm-commerce'),
            'DESC' => __('Descending', 'ggm-commerce'),
        ],
    ]
);

$this->add_control(
    'filter_by_current_taxonomy',
    [
        'label' => __('Filter by Current Taxonomy', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'no',
        'description' => __('On archive pages, filter to show only products in the current category/tag', 'ggm-commerce'),
    ]
);

$this->add_control(
    'filter_by_category',
    [
        'label' => __('Filter by Category', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => GGM_Commerce_Product_Data_Handler::get_categories_options(),
        'description' => __('Leave empty to show all products', 'ggm-commerce'),
        'condition' => [
            'filter_by_current_taxonomy!' => 'yes',
        ],
    ]
);

$this->add_control(
    'filter_by_tag',
    [
        'label' => __('Filter by Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => GGM_Commerce_Product_Data_Handler::get_tags_options(),
        'description' => __('Leave empty to show all products', 'ggm-commerce'),
        'condition' => [
            'filter_by_current_taxonomy!' => 'yes',
        ],
    ]
);

$this->add_control(
    'show_featured_only',
    [
        'label' => __('Featured Products Only', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'no',
    ]
);

$this->add_control(
    'stock_filter',
    [
        'label'   => __('Stock Status', 'ggm-commerce'),
        'type'    => \Elementor\Controls_Manager::SELECT,
        'default' => 'all',
        'options' => [
            'all'        => __('All', 'ggm-commerce'),
            'instock'    => __('In Stock', 'ggm-commerce'),
            'outofstock' => __('Out of Stock', 'ggm-commerce'),
            'lowstock'   => __('Low Stock', 'ggm-commerce'),
        ],
    ]
);

// Attribute filter (conditional on custom-attributes premium feature)
$attr_filter_active = function_exists('ggm_commerce_license') && ggm_commerce_license()->is_widget_activated('custom-attributes');
if ($attr_filter_active) {
    $attr_filter_options = array('' => __('-- No Filter --', 'ggm-commerce'));
    if (class_exists('GGM_Commerce_Attributes')) {
        $all_attrs = GGM_Commerce_Attributes::get_instance()->get_all_attributes();
        foreach ($all_attrs as $attr) {
            $label = $attr->name;
            if (!empty($attr->group_name)) {
                $label .= ' (' . $attr->group_name . ')';
            }
            $attr_filter_options[$attr->id] = $label;
        }
    }

    $this->add_control(
        'filter_by_attribute',
        [
            'label' => __('Filter by Attribute', 'ggm-commerce'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => '',
            'options' => $attr_filter_options,
            'description' => __('Show only products with a specific attribute value.', 'ggm-commerce'),
        ]
    );

    $this->add_control(
        'filter_by_attribute_value',
        [
            'label' => __('Attribute Value', 'ggm-commerce'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'default' => '',
            'description' => __('Enter the exact attribute value to filter by.', 'ggm-commerce'),
            'condition' => [
                'filter_by_attribute!' => '',
            ],
        ]
    );
}

$this->add_control(
    'show_pagination',
    [
        'label' => __('Show Pagination', '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_sorting',
    [
        'label' => __('Enable Sorting', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'no',
        'description' => __('Show a dropdown to allow users to sort products', 'ggm-commerce'),
    ]
);

$this->end_controls_section();

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

$this->add_control(
    'grid_style',
    [
        'label' => __('Grid Style', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'standard',
        'options' => [
            'standard' => __('Standard Grid', 'ggm-commerce'),
            'masonry' => __('Masonry Grid', 'ggm-commerce'),
            'justified' => __('Justified Grid', 'ggm-commerce'),
        ],
    ]
);

$this->add_responsive_control(
    'columns',
    [
        'label' => __('Columns', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => '3',
        'options' => [
            '1' => __('1 Column', 'ggm-commerce'),
            '2' => __('2 Columns', 'ggm-commerce'),
            '3' => __('3 Columns', 'ggm-commerce'),
            '4' => __('4 Columns', 'ggm-commerce'),
            '5' => __('5 Columns', 'ggm-commerce'),
            '6' => __('6 Columns', 'ggm-commerce'),
        ],
        'tablet_default' => '2',
        'mobile_default' => '1',
    ]
);

$this->add_responsive_control(
    'column_gap',
    [
        'label' => __('Column Gap', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px'],
        'range' => [
            'px' => [
                'min' => 0,
                'max' => 100,
            ],
        ],
        'default' => [
            'size' => 20,
            'unit' => 'px',
        ],
    ]
);

$this->add_responsive_control(
    'row_gap',
    [
        'label' => __('Row Gap', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px'],
        'range' => [
            'px' => [
                'min' => 0,
                'max' => 100,
            ],
        ],
        'default' => [
            'size' => 30,
            'unit' => 'px',
        ],
        'selectors' => [
            '{{WRAPPER}} .ggmc-products-grid' => 'row-gap: {{SIZE}}{{UNIT}};',
        ],
    ]
);

// Single Column Layout Options (like Product List)
$this->add_control(
    'single_column_heading',
    [
        'label' => __('Single Column Layout Options', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
        'condition' => [
            'columns' => '1',
        ],
    ]
);

$this->add_control(
    'layout_style',
    [
        'label' => __('Layout Style', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'horizontal',
        'options' => [
            'horizontal' => __('Horizontal (Image + Content)', 'ggm-commerce'),
            'vertical' => __('Vertical (Stacked)', 'ggm-commerce'),
            'compact' => __('Compact (Minimal)', 'ggm-commerce'),
        ],
        'description' => __('Choose how products are displayed in single column mode', 'ggm-commerce'),
        'condition' => [
            'columns' => '1',
        ],
    ]
);

$this->add_responsive_control(
    'image_position',
    [
        'label' => __('Image Position', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::CHOOSE,
        'options' => [
            'left' => [
                'title' => __('Left', 'ggm-commerce'),
                'icon' => 'eicon-h-align-left',
            ],
            'right' => [
                'title' => __('Right', 'ggm-commerce'),
                'icon' => 'eicon-h-align-right',
            ],
            'top' => [
                'title' => __('Top', 'ggm-commerce'),
                'icon' => 'eicon-v-align-top',
            ],
        ],
        'default' => 'left',
        'condition' => [
            'columns' => '1',
            'layout_style' => 'horizontal',
        ],
    ]
);

$this->add_responsive_control(
    'image_width',
    [
        'label' => __('Image Width', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SLIDER,
        'size_units' => ['px', '%'],
        'range' => [
            'px' => ['min' => 50, 'max' => 300],
            '%' => ['min' => 10, 'max' => 50],
        ],
        'default' => ['size' => 120, 'unit' => 'px'],
        'condition' => [
            'columns' => '1',
            'layout_style!' => 'compact',
        ],
    ]
);

$this->end_controls_section();

// -----------------------------------------------------------------------
// Show/Hide Components Section
// -----------------------------------------------------------------------
$this->start_controls_section(
    'elements_section',
    [
        'label' => __('Show/Hide Components', 'ggm-commerce'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'show_image',
    [
        'label' => __('Show Image', '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(
    'show_badge',
    [
        'label' => __('Show Badges', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Show featured, sale, new, and stock badges', 'ggm-commerce'),
    ]
);

$this->add_control(
    'show_quick_view',
    [
        'label' => __('Show Quick View', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Premium feature - requires Quick View activation in Premium Components.', 'ggm-commerce'),
        'condition' => [
            'show_image' => 'yes',
        ],
    ]
);

$this->add_control(
    'show_categories',
    [
        'label' => __('Show Categories', '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(
    'link_categories',
    [
        'label' => __('Link Categories', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'condition' => [
            'show_categories' => 'yes',
        ],
    ]
);

$this->add_control(
    'show_title',
    [
        'label' => __('Show Title', '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(
    'show_rating',
    [
        'label' => __('Show Rating', '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(
    'show_price',
    [
        'label' => __('Show Price', '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(
    'show_description',
    [
        'label' => __('Show Description', '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(
    'show_stock',
    [
        'label' => __('Show Stock Status', '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(
    'show_restock_date',
    [
        'label' => __('Show Restock Date', 'goose-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'goose-commerce'),
        'label_off' => __('No', 'goose-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => __('Replaces "Out of Stock" with the expected restock message if a date is set.', 'goose-commerce'),
        'condition' => [
            'show_stock' => 'yes',
        ],
    ]
);

$this->add_control(
    'restock_display_format',
    [
        'label' => __('Restock Display Format', 'goose-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'descriptive',
        'options' => [
            'exact_date' => __('Exact Date (Expected 04/Aug)', 'goose-commerce'),
            'weeks' => __('Weeks (Expected in 2 to 3 weeks)', 'goose-commerce'),
            'descriptive' => __('Descriptive (Expected Soon / Imminent)', 'goose-commerce'),
        ],
        'condition' => [
            'show_stock' => 'yes',
            'show_restock_date' => 'yes',
        ],
    ]
);

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

$this->add_control(
    'show_add_to_cart',
    [
        'label' => __('Show Add to Cart Button', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'ggm-commerce'),
        'label_off' => __('Hide', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

// Check if wishlist feature is activated
$wishlist_active = \GGM_Commerce_License::instance()->is_widget_activated('wishlist');

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

if (!$wishlist_active) {
    $this->add_control(
        'wishlist_premium_notice',
        [
            'type' => \Elementor\Controls_Manager::RAW_HTML,
            'raw' => '<div style="background: #fff3cd; border-left: 4px solid #ffc107; padding: 10px; margin: 10px 0;"><strong>' . __('Premium Feature', 'ggm-commerce') . '</strong><br>' . __('Activate the Wishlist feature in Goose Commerce Settings → Premium Components to enable this button.', 'ggm-commerce') . '</div>',
            'condition' => [
                'show_wishlist' => 'yes',
            ],
        ]
    );
}

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

$this->end_controls_section();

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

$_component_order_styles = '
<style>
/* Component Order Repeater Styling */
.elementor-control-component_order .elementor-repeater-row-controls {
    display: none !important;
}
.elementor-control-component_order .elementor-repeater-fields:hover {
    background: #f5f5f5;
}
.elementor-control-component_order .elementor-repeater-fields .elementor-repeater-row-item-title {
    padding: 10px 12px 10px 36px !important;
    cursor: move;
    position: relative;
}
/* Drag handle icon */
.elementor-control-component_order .elementor-repeater-fields .elementor-repeater-row-item-title::before {
    content: "\e816";
    font-family: "eicons";
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #a4afb7;
    font-size: 12px;
}
</style>';

$this->add_control(
    'component_order_description',
    [
        'type' => \Elementor\Controls_Manager::RAW_HTML,
        'raw' => '<p style="color: #666; font-size: 12px; margin-bottom: 10px;">' . __('Drag and drop to reorder how product components appear.', 'ggm-commerce') . '</p>' . $_component_order_styles,
    ]
);

$_repeater = new \Elementor\Repeater();

$_repeater->add_control(
    'component_id',
    [
        'label' => __('Component ID', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HIDDEN,
    ]
);

$_repeater->add_control(
    'component_label',
    [
        'label' => __('Component', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HIDDEN,
        'default' => '',
    ]
);

// Build component list dynamically based on activated premium features
$_default_components = ggmc_product_grid_get_available_components();

$this->add_control(
    'component_order',
    [
        'label' => __('Component Order', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::REPEATER,
        'fields' => $_repeater->get_controls(),
        'default' => $_default_components,
        'title_field' => '{{{ component_label }}}',
        'prevent_empty' => true,
        'item_actions' => [
            'add' => false,
            'duplicate' => false,
            'remove' => false,
            'sort' => true,
        ],
    ]
);

$this->end_controls_section();
