<?php
/**
 * Product Table Widget — TAB_CONTENT Controls
 * Included via require inside register_controls(). Uses $this directly.
 */

// 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' => 20,
        '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(
    '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(
    'filter_by_current_taxonomy',
    [
        'label' => __('Filter by Current Taxonomy', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Yes',
        'label_off' => 'No',
        'return_value' => 'yes',
        'default' => 'no',
        'description' => 'Enable this when using this widget on category or tag archive pages to automatically filter products by the current taxonomy.',
    ]
);

$this->add_control(
    'show_pagination',
    [
        'label' => 'Show Pagination',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Yes',
        'label_off' => 'No',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->end_controls_section();

// -------------------------------------------------------------------------
// Table Columns Section
// -------------------------------------------------------------------------
$this->start_controls_section(
    'columns_section',
    [
        'label' => 'Table Columns',
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'show_image_column',
    [
        'label' => 'Show Image Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'show_badges_column',
    [
        'label' => 'Show Badges Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
        'description' => 'Display product badges (Featured, Sale, New) in a separate column',
    ]
);

$this->add_control(
    'show_name_column',
    [
        'label' => 'Show Name Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'show_rating_column',
    [
        'label' => 'Show Rating Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'show_sku_column',
    [
        'label' => 'Show SKU Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'show_price_column',
    [
        'label' => 'Show Price Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'show_stock_column',
    [
        'label' => 'Show Stock Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        '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_column' => '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_column' => 'yes',
            'show_restock_date' => 'yes',
        ],
    ]
);

$this->add_control(
    'show_description_column',
    [
        'label' => 'Show Description Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'no',
    ]
);

$this->add_control(
    'show_actions_column',
    [
        'label' => 'Show Actions Column',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Show',
        'label_off' => 'Hide',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'add_to_cart_text',
    [
        'label' => __('Add to Cart Button Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Add to Cart', 'ggm-commerce'),
        'condition' => [
            'show_actions_column' => 'yes',
        ],
    ]
);

// Variable Product Settings
$this->add_control(
    'variable_product_text',
    [
        'label' => __('Variable Product Button Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Select Options', 'ggm-commerce'),
        'description' => __('Text shown when product has variations', 'ggm-commerce'),
        'condition' => [
            'show_actions_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'variable_product_behavior',
    [
        'label' => __('Variable Product Behavior', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'link_to_product',
        'options' => [
            'link_to_product' => __('Link to Product Page', 'ggm-commerce'),
            'disabled' => __('Disabled (wait for selection)', 'ggm-commerce'),
        ],
        'description' => __('What happens when button clicked without variation selected', 'ggm-commerce'),
        'condition' => [
            'show_actions_column' => 'yes',
        ],
    ]
);

// Column HTML Tags
$this->add_control(
    'column_html_tags_heading',
    [
        'label' => __('Column HTML Tags', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_control(
    'name_html_tag',
    [
        'label' => __('Product Name Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'p' => 'P',
            'span' => 'Span',
        ],
        'condition' => [
            'show_name_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'price_html_tag',
    [
        'label' => __('Price Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'h3',
        'options' => [
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'p' => 'P',
            'span' => 'Span',
        ],
        'condition' => [
            'show_price_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'stock_html_tag',
    [
        'label' => __('Stock Status Tag', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'p',
        'options' => [
            'p' => 'P',
            'span' => 'Span',
            'div' => 'Div',
        ],
        'condition' => [
            'show_stock_column' => 'yes',
        ],
    ]
);

// Column Header Labels
$this->add_control(
    'column_header_labels_heading',
    [
        'label' => __('Column Header Labels', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$this->add_control(
    'header_image_label',
    [
        'label' => __('Image Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Image', 'ggm-commerce'),
        'condition' => [
            'show_image_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_badges_label',
    [
        'label' => __('Badges Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Badges', 'ggm-commerce'),
        'condition' => [
            'show_badges_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_name_label',
    [
        'label' => __('Name Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Product', 'ggm-commerce'),
        'condition' => [
            'show_name_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_sku_label',
    [
        'label' => __('SKU Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('SKU', 'ggm-commerce'),
        'condition' => [
            'show_sku_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_price_label',
    [
        'label' => __('Price Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Price', 'ggm-commerce'),
        'condition' => [
            'show_price_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_stock_label',
    [
        'label' => __('Stock Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Stock', 'ggm-commerce'),
        'condition' => [
            'show_stock_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_rating_label',
    [
        'label' => __('Rating Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Rating', 'ggm-commerce'),
        'condition' => [
            'show_rating_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_description_label',
    [
        'label' => __('Description Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Description', 'ggm-commerce'),
        'condition' => [
            'show_description_column' => 'yes',
        ],
    ]
);

$this->add_control(
    'header_actions_label',
    [
        'label' => __('Actions Header Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Actions', 'ggm-commerce'),
        'condition' => [
            'show_actions_column' => 'yes',
        ],
    ]
);

// Element Visibility (Within Cells)
$this->add_control(
    'element_visibility_heading',
    [
        'label' => __('Element Visibility', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
    ]
);

$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',
        'description' => __('Display categories under product name', 'ggm-commerce'),
        'condition' => [
            'show_name_column' => 'yes',
        ],
    ]
);

$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_column' => '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',
        'condition' => [
            'show_actions_column' => '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_actions_column' => 'yes',
                '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',
        'condition' => [
            'show_actions_column' => 'yes',
        ],
    ]
);

$this->end_controls_section();

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

$_column_order_inline_styles = '
<style>
/* Column Order Repeater Styling */
.elementor-control-column_order .elementor-repeater-row-controls {
    display: none !important;
}
.elementor-control-column_order .elementor-repeater-fields:hover {
    background: #f5f5f5;
}
.elementor-control-column_order .elementor-repeater-fields .elementor-repeater-row-item-title {
    padding: 10px 12px 10px 36px !important;
    cursor: move;
    position: relative;
}
/* Drag handle icon */
.elementor-control-column_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(
    'column_order_description',
    [
        'type' => \Elementor\Controls_Manager::RAW_HTML,
        'raw' => '<p style="color: #666; font-size: 12px; margin-bottom: 10px;">' . __('Drag and drop to reorder table columns. Use the Column visibility toggles above to show/hide columns.', 'ggm-commerce') . '</p>' . $_column_order_inline_styles,
    ]
);

$_repeater = new \Elementor\Repeater();

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

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

$_default_columns = ggmc_product_table_get_available_columns();

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

$this->end_controls_section();

// -------------------------------------------------------------------------
// Table Features Section
// -------------------------------------------------------------------------
$this->start_controls_section(
    'filters_section',
    [
        'label' => 'Table Features',
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
    ]
);

$this->add_control(
    'enable_sorting',
    [
        'label' => 'Enable Sorting',
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => 'Yes',
        'label_off' => 'No',
        'return_value' => 'yes',
        'default' => 'yes',
    ]
);

$this->add_control(
    'sorting_type',
    [
        'label' => 'Sorting Type',
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'both',
        'options' => [
            'select' => 'Select Box Only',
            'headers' => 'Column Headers Only',
            'both' => 'Both',
        ],
        'condition' => [
            'enable_sorting' => 'yes',
        ],
    ]
);

$this->add_control(
    'sorting_options',
    [
        'label' => 'Available Sorting Options',
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'default' => ['name', 'price', 'stock'],
        'options' => [
            'name' => 'Name (A-Z / Z-A)',
            'price' => 'Price (Low/High)',
            'stock' => 'Stock (Low/High)',
        ],
        'description' => 'Select which fields can be sorted. Dropdown will show both directions for each field.',
        'condition' => [
            'enable_sorting' => 'yes',
            'sorting_type' => ['select', 'both'],
        ],
    ]
);

$this->add_control(
    'sorting_show_label',
    [
        'label' => __('Show Label', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Yes', 'ggm-commerce'),
        'label_off' => __('No', 'ggm-commerce'),
        'return_value' => 'yes',
        'default' => 'yes',
        'condition' => [
            'enable_sorting' => 'yes',
            'sorting_type' => ['select', 'both'],
        ],
    ]
);

$this->add_control(
    'sorting_label_text',
    [
        'label' => __('Label Text', 'ggm-commerce'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => __('Sort By:', 'ggm-commerce'),
        'condition' => [
            'enable_sorting' => 'yes',
            'sorting_type' => ['select', 'both'],
            'sorting_show_label' => 'yes',
        ],
    ]
);

$this->add_control(
    'sorting_notice',
    [
        'type' => \Elementor\Controls_Manager::RAW_HTML,
        'raw' => '<div style="padding: 10px; background: #e3f2fd; border-left: 3px solid #2196f3; margin: 10px 0;"><strong>Note:</strong> Filtering is now handled by the separate Product Filter widget. Sorting shows the last selected option only (non-cumulative).</div>',
        'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
    ]
);

$this->add_control(
    'no_products_message',
    [
        'label' => 'No Products Message',
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => 'No products found.',
        'placeholder' => 'Enter message when no products are found',
    ]
);

$this->end_controls_section();
