<?php
/**
 * Step 5 - Field Mapping (final, advanced editor enabled).
 *
 * @package PrimeLink
 */

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

$import_id = isset( $_GET['import_id'] ) ? absint( $_GET['import_id'] ) : 0;

use PrimeLink\Helpers\TemplateHelper;
?>

<div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Step 5 · Mapping', 'primelink' ); ?></div>
<h2><?php esc_html_e( 'Field Mapping Studio', 'primelink' ); ?></h2>
<input type="hidden" id="mt-primelink-step" value="5">

<!-- Templates -->
            <section class="mt_primelink-card mt_primelink-card--glass mt_primelink-templates-card">
                <div class="mt_primelink-card-header">
                    <h3><?php esc_html_e( 'Load & Save Settings', 'primelink' ); ?></h3>
                </div>
                <div class="mt_primelink-card-body mt_primelink-templates-body">
                    <div class="mt_primelink-template-row">
                        <input type="text" id="mt_primelink-template-name" placeholder="<?php esc_attr_e( 'Template name', 'primelink' ); ?>" />
                        <button type="button" class="button button-primary" id="mt_primelink-save-template"><?php esc_html_e( 'Save', 'primelink' ); ?></button>
                    </div>
                </div>
            </section>

<div class="mt_primelink-step mt_primelink-import-step-5" data-import-id="<?php echo esc_attr( $import_id ); ?>">

    <div class="mt_primelink-mapping-shell">

        <!-- Left panel: file fields -->
        <aside class="mt_primelink-fields-panel">
            <div class="mt_primelink-fields-header">
                <h3><?php esc_html_e( 'File Fields', 'primelink' ); ?></h3>
                <div style="display:flex;gap:8px;align-items:center;">
                    <input type="search" id="mt_primelink-field-search" placeholder="<?php esc_attr_e( 'Search field...', 'primelink' ); ?>" style="flex:1;" />
                    <button type="button" class="button button-small" id="mt_primelink-refresh-fields" title="<?php esc_attr_e( 'Reload fields from CSV file', 'primelink' ); ?>" style="white-space:nowrap;">
                        <span class="dashicons dashicons-update" style="font-size:16px;width:16px;height:16px;line-height:1.2;"></span>
                    </button>
                </div>
            </div>

            <div class="mt_primelink-field-list" id="mt-primelink-fields" data-import-id="<?php echo esc_attr( $import_id ); ?>">
                <div class="mt_primelink-loading"><?php esc_html_e( 'Loading fields...', 'primelink' ); ?></div>
            </div>
        </aside>

        <!-- Right panel: mapping first, preview after -->
        <main class="mt_primelink-mapping-main">
            <div class="mt_primelink-template-row mt_primelink-template-row--actions">
                <input type="search" id="mt_primelink-target-search" placeholder="<?php esc_attr_e( 'Search target field...', 'primelink' ); ?>" aria-label="<?php esc_attr_e( 'Search target field', 'primelink' ); ?>">
                <select id="mt_primelink-load-template">
                    <option value=""><?php esc_html_e( 'Load existing template...', 'primelink' ); ?></option>
                </select>
                <button type="button" class="button" id="mt_primelink-load-template-btn"><?php esc_html_e( 'Load', 'primelink' ); ?></button>
                <button type="button" class="button mt_primelink-duplicate-btn" id="mt_primelink-duplicate-template"><?php esc_html_e( 'Duplicate', 'primelink' ); ?></button>
            </div>

            <!-- Custom auto-map aliases -->
            <section class="mt_primelink-card mt_primelink-card--glass mt_primelink-alias-card">
                <div class="mt_primelink-card-header">
                    <div>
                        <h3><?php esc_html_e( 'Auto-map Aliases', 'primelink' ); ?></h3>
                        <div class="mt_primelink-groups-hint"><?php esc_html_e( 'Add custom synonyms for targets (e.g. Title: sk_title, name).', 'primelink' ); ?></div>
                    </div>
                    <button type="button" class="button" id="mt_primelink-add-alias"><?php esc_html_e( 'Add alias', 'primelink' ); ?></button>
                </div>
                <div class="mt_primelink-card-body" id="mt_primelink-alias-wrap">
                    <div class="mt_primelink-empty"><?php esc_html_e( 'No custom aliases yet.', 'primelink' ); ?></div>
                </div>
            </section>

            <!-- Mapping groups -->
            <section class="mt_primelink-card mt_primelink-card--glass mt_primelink-groups-card">
                    <div class="mt_primelink-card-header">
                    <div>
                        <h3><?php esc_html_e( 'Field Mapping', 'primelink' ); ?></h3>
                        <div class="mt_primelink-groups-hint"><?php esc_html_e( 'Drag fields from left into slots.', 'primelink' ); ?></div>
                    </div>
                    <div class="mt_primelink-map-actions">
                        <button type="button" class="button" id="mt_primelink-auto-map"><?php esc_html_e( 'Auto-map', 'primelink' ); ?></button>
                        <button type="button" class="button" id="mt_primelink-clear-mapping"><?php esc_html_e( 'Clear all', 'primelink' ); ?></button>
                        <span class="mt_primelink-unmapped-pill" id="mt_primelink-unmapped-count" style="display:none;"></span>
                    </div>
                </div>

                <div class="mt_primelink-card-body">

                    <div class="mt_primelink-helper-row">
                        <div class="mt_primelink-helper-card glass">
                            <div class="mt_primelink-helper-head"><?php esc_html_e( 'Required fields', 'primelink' ); ?></div>
                            <div id="mt_primelink-required-status" class="mt_primelink-helper-status"><?php esc_html_e( 'Checking...', 'primelink' ); ?></div>
                            <ul id="mt_primelink-required-list" class="mt_primelink-helper-list"></ul>
                        </div>
                        <div class="mt_primelink-helper-card glass">
                            <div class="mt_primelink-helper-head">
                                <span><?php esc_html_e( 'Suggestions from sample row', 'primelink' ); ?></span>
                            </div>
                            <div id="mt_primelink-suggestion-list" class="mt_primelink-helper-list"></div>
                        </div>
                    </div>

                    <!-- Title & Content -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Title & Content', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_import_slots( array(
                                'post_title'   => __( 'Post Title', 'primelink' ),
                                'post_content' => __( 'Post Content', 'primelink' ),
                                'post_excerpt' => __( 'Post Excerpt', 'primelink' ),
                                'post_name'    => __( 'Slug', 'primelink' ),
                                'post_status'  => __( 'Status', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Product Data -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Product Data', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <div class="mt_primelink-pd-layout">
                                <div class="mt_primelink-pd-tabs" role="tablist" aria-label="<?php esc_attr_e( 'Product data tabs', 'primelink' ); ?>">
                                    <button type="button" class="mt_primelink-pd-tab is-active" data-tab="general"><?php esc_html_e( 'General', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="inventory"><?php esc_html_e( 'Inventory', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="shipping"><?php esc_html_e( 'Shipping', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="attributes"><?php esc_html_e( 'Attributes', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="downloads"><?php esc_html_e( 'Downloads', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="variations"><?php esc_html_e( 'Variation Overrides', 'primelink' ); ?></button>
                                    <button type="button" class="mt_primelink-pd-tab" data-tab="linked"><?php esc_html_e( 'Linked Products', 'primelink' ); ?></button>
                                </div>
                                    <div class="mt_primelink-pd-panels">
                                        <div class="mt_primelink-pd-panel is-active" data-panel="general">
                                            <div class="mt_primelink-product-type-row">
                                                <div class="mt_primelink-product-mapped-slot-inline">
                                                    <?php TemplateHelper::render_import_slots( array(
                                                        'product_type'   => __( 'Product Type (mapped)', 'primelink' ),
                                                    ) ); ?>
                                                </div>
                                                <div class="mt_primelink-product-type-wrap">
                                                    <label for="mt_primelink-product-type-select"><strong><?php esc_html_e( 'Product Type', 'primelink' ); ?></strong></label>
                                                    <select id="mt_primelink-product-type-select">
                                                        <option value="mapped"><?php esc_html_e( 'Mapped from file', 'primelink' ); ?></option>
                                                        <option value="simple" selected><?php esc_html_e( 'Simple product', 'primelink' ); ?></option>
                                                        <option value="grouped"><?php esc_html_e( 'Grouped product', 'primelink' ); ?></option>
                                                        <option value="external"><?php esc_html_e( 'External/Affiliate', 'primelink' ); ?></option>
                                                        <option value="variable"><?php esc_html_e( 'Variable product', 'primelink' ); ?></option>
                                                    </select>
                                                    <p class="description"><?php esc_html_e( 'Choose a fixed type or leave “Mapped from file” to map it from the feed.', 'primelink' ); ?></p>
                                                </div>
                                            </div>
                                            <?php TemplateHelper::render_import_slots( array(
                                                'regular_price'  => 'Regular Price',
                                                'sale_price'     => 'Sale Price',
                                                'tax_status'     => 'Tax Status',
                                                'tax_class'      => 'Tax Class',
                                                'purchase_note'  => 'Purchase Note',
                                                'featured'       => 'Featured Product',
                                                'menu_order'     => 'Menu Order',
                                                'post_date'      => 'Post Date',
                                            ) ); ?>
                                        </div>

                                    <div class="mt_primelink-pd-panel" data-panel="inventory">
                                        <?php TemplateHelper::render_import_slots( array(
                                            'sku'            => 'SKU',
                                            'stock'          => 'Stock Quantity',
                                            'stock_status'   => 'Stock Status',
                                            'manage_stock'   => 'Manage Stock',
                                            'catalog_visibility' => 'Catalog Visibility',
                                            'backorders'     => 'Backorders',
                                            'sold_individually' => 'Sold Individually',
                                            'low_stock_amount'  => 'Low Stock Threshold',
                                            'reviews_allowed'   => 'Allow Reviews',
                                        ) ); ?>
                                    </div>

                                    <div class="mt_primelink-pd-panel" data-panel="shipping">
                                        <?php TemplateHelper::render_import_slots( array(
                                            'weight'         => 'Weight',
                                            'length'         => 'Length',
                                            'width'          => 'Width',
                                            'height'         => 'Height',
                                            'shipping_class' => 'Shipping Class',
                                            'requires_shipping' => 'Requires Shipping',
                                        ) ); ?>
                                    </div>

                                    <div class="mt_primelink-pd-panel" data-panel="downloads">
                                        <?php TemplateHelper::render_import_slots( array(
                                            'downloadable'    => 'Downloadable',
                                            'download_name'   => 'Download Name',
                                            'download_file'   => 'Download File URL',
                                            'download_limit'  => 'Download Limit',
                                            'download_expiry' => 'Download Expiry (days)',
                                        ) ); ?>
                                    </div>

                                    <div class="mt_primelink-pd-panel" data-panel="attributes">
                                        <div class="mt_primelink-attr-actions">
                                            <button type="button" class="button" id="mt_primelink-add-attribute"><?php esc_html_e( 'Add Attribute', 'primelink' ); ?></button>
                                            <label class="mt_primelink-create-variations">
                                                <input type="checkbox" id="mt_primelink-create-variations" />
                                                <?php esc_html_e( 'Create variations from attributes', 'primelink' ); ?>
                                            </label>
                                        </div>
                                        <div id="mt_primelink-attributes-wrap"></div>
                                        <p class="description"><?php esc_html_e( 'Map product attributes (e.g. Color, Size) and optionally generate variations.', 'primelink' ); ?></p>
                                    </div>

                                    <div class="mt_primelink-pd-panel" data-panel="variations">
                                        <?php TemplateHelper::render_import_slots( array(
                                            'variation_regular_price' => 'Variation Regular Price',
                                            'variation_sale_price'    => 'Variation Sale Price',
                                            'variation_content'       => 'Variation Description',
                                            'variation_excerpt'       => 'Variation Short Description',
                                            'variation_stock'         => 'Variation Stock Quantity',
                                            'variation_stock_status'  => 'Variation Stock Status',
                                            'variation_manage_stock'  => 'Variation Manage Stock',
                                            'variation_sku'           => 'Variation SKU',
                                            'variation_tax_class'     => 'Variation Tax Class',
                                            'variation_backorders'    => 'Variation Backorders',
                                            'variation_weight'        => 'Variation Weight',
                                            'variation_length'        => 'Variation Length',
                                            'variation_width'         => 'Variation Width',
                                            'variation_height'        => 'Variation Height',
                                            'variation_image'         => 'Variation Image',
                                            'variation_gallery'       => 'Variation Gallery',
                                        ) ); ?>
                                        <p class="description"><?php esc_html_e( 'Values here apply to generated variations (override parent data per variation).', 'primelink' ); ?></p>
                                    </div>

                                    <div class="mt_primelink-pd-panel" data-panel="linked">
                                        <?php TemplateHelper::render_import_slots( array(
                                            'upsell_refs'        => 'Upsells (IDs/SKUs list)',
                                            'cross_sell_refs'    => 'Cross-sells (IDs/SKUs list)',
                                            'grouped_children'   => 'Grouped children (IDs/SKUs list)',
                                        ) ); ?>
                                        <p class="description"><?php esc_html_e( 'Provide comma/semicolon separated IDs or SKUs to link products.', 'primelink' ); ?></p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </details>

                    <!-- Images -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title">Images</summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_import_slots( array(
                                'featured_image' => 'Featured Image',
                            ) ); ?>
                            
                            <div style="margin-top:16px;">
                                <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
                                    <label><strong><?php esc_html_e( 'Gallery Images', 'primelink' ); ?></strong></label>
                                    <button type="button" class="button button-small" id="mt_primelink-add-gallery-slot">
                                        <?php esc_html_e( '+ Add Gallery Field', 'primelink' ); ?>
                                    </button>
                                </div>
                                <p class="description" style="margin-bottom:12px;">
                                    <?php esc_html_e( 'Map multiple image fields (e.g., img1, img2, img3) to combine them into the product gallery.', 'primelink' ); ?>
                                </p>
                                
                                <div id="mt_primelink-gallery-slots">
                                    <?php TemplateHelper::render_import_slots( array(
                                        'images' => 'Images (Gallery)',
                                    ) ); ?>
                                </div>
                            </div>
                        </div>
                    </details>

                    <?php
                    // Shared integrations mapping (Yoast, etc.).
                    include MT_PRIMELINK_PATH . 'templates/admin/imports/partials/integrations-mapping.php';
                    ?>

                    <!-- Custom Fields -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title">Custom Fields</summary>
                        <div class="mt_primelink-accordion-body">
                            <div id="mt_primelink-custom-fields-wrap"></div>
                            <button type="button" class="button" id="mt_primelink-add-custom-field">Add Custom Field</button>
                            <p class="description">Create custom meta mapping (key/value).</p>
                        </div>
                    </details>

                    <!-- Taxonomies -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title">Taxonomies</summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_import_slots( array(
                                'product_type'  => 'Product Type',
                                'product_cat'   => 'Categories',
                                'product_tag'   => 'Tags',
                                'product_brand' => 'Brands',
                            ), 'mapping[tax]' ); ?>
                        </div>
                    </details>

                    <!-- Other options -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title">Other options</summary>
                        <div class="mt_primelink-accordion-body">
                            <div class="mt_primelink-other-options">
                                <label class="mt_primelink-form-row">
                                    <span class="mt_primelink-label">Global prefix</span>
                                    <input type="text" id="mt_primelink-opt-global-prefix" placeholder="e.g. Brand - ">
                                </label>
                                <label class="mt_primelink-form-row">
                                    <span class="mt_primelink-label">Global suffix</span>
                                    <input type="text" id="mt_primelink-opt-global-suffix" placeholder="e.g. | MyStore">
                                </label>
                                <label class="mt_primelink-checkbox-row">
                                    <input type="checkbox" id="mt_primelink-opt-global-trim">
                                    <span>Trim whitespace on all mapped fields</span>
                                </label>
                                <label class="mt_primelink-checkbox-row">
                                    <input type="checkbox" id="mt_primelink-opt-slugify">
                                    <span>Auto-generate slug from Title (clean accents/separators)</span>
                                </label>
                                <label class="mt_primelink-form-row">
                                    <span class="mt_primelink-label">Fallback image URL</span>
                                    <input type="text" id="mt_primelink-opt-fallback-image" placeholder="https://example.com/fallback.jpg">
                                    <small class="description">Used if no image is mapped for a row.</small>
                                </label>
                                <label class="mt_primelink-form-row">
                                    <span class="mt_primelink-label">Skip row if field empty</span>
                                    <input type="text" id="mt_primelink-opt-skip-field" placeholder="e.g. sku">
                                    <small class="description">Rows with this source field empty will be skipped.</small>
                                </label>
                            </div>
                        </div>
                    </details>

                </div>
            </section>

            <!-- Preview (vertical, row navigator) -->
            <div id="mt-primelink-item-preview"
                 class="mt_primelink-preview-card"
                 data-import-id="<?php echo esc_attr( $import_id ); ?>">
            </div>

        </main>
    </div>
</div>

<!-- Row Filters (Step 5) -->
<div class="mt_primelink-card mt_primelink-card--glass" style="margin-top:12px;">
    <div class="mt_primelink-card-header">
        <h3><?php esc_html_e( 'Row Filters', 'primelink' ); ?></h3>
        <span id="mt_primelink-filter-active-count" style="display:none;" class="mt_primelink-summary-pill"></span>
    </div>
    <div class="mt_primelink-card-body">
        <div id="mt_primelink-filter-rows" class="mt_primelink-filter-rows" data-populated="0"></div>
        <div class="mt_primelink-filter-actions" style="margin-top:8px;">
            <button type="button" class="button" id="mt_primelink-add-filter-row"><?php esc_html_e( 'Add condition', 'primelink' ); ?></button>
        </div>
        <div id="mt_primelink-filter-notice" class="mt_primelink-empty" style="display:none;margin-top:6px;"></div>
    </div>
</div>

<!-- Advanced Mapping Editor -->
<div id="mt_primelink-map-editor" class="mt_primelink-map-editor" style="display:none;">
    <div class="mt_primelink-map-editor__backdrop"></div>
    <div class="mt_primelink-map-editor__panel">
        <div class="mt_primelink-map-editor__header">
            <div class="mt_primelink-editor-title-wrap">
                <h3>Field Options</h3>
            </div>
            <button type="button" class="button mt_primelink-map-editor-close"><?php esc_html_e( 'Close', 'primelink' ); ?></button>
        </div>

        <div class="mt_primelink-map-editor__body">
            <input type="hidden" id="mt_primelink-editor-target" value="" />

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Source Field', 'primelink' ); ?></label>
                <input type="text" id="mt_primelink-editor-source" placeholder="<?php esc_attr_e( 'e.g. title or title_slug', 'primelink' ); ?>" />
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Prefix / Suffix', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;">
                    <input type="text" id="mt_primelink-editor-prefix" placeholder="<?php esc_attr_e( 'Prefix (optional)', 'primelink' ); ?>" />
                    <input type="text" id="mt_primelink-editor-suffix" placeholder="<?php esc_attr_e( 'Suffix (optional)', 'primelink' ); ?>" />
                </div>
                <small class="description"><?php esc_html_e( 'Useful to add fixed text before/after Title or other fields.', 'primelink' ); ?></small>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Separator (split multi-values)', 'primelink' ); ?></label>
                <input type="text" id="mt_primelink-editor-separator" placeholder="<?php esc_attr_e( 'e.g. | , ; >', 'primelink' ); ?>" />
                <small class="description"><?php esc_html_e( 'Example: "img1.jpg,img2.jpg" use ","', 'primelink' ); ?></small>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Default value (if empty)', 'primelink' ); ?></label>
                <input type="text" id="mt_primelink-editor-default" placeholder="<?php esc_attr_e( 'Fallback value', 'primelink' ); ?>" />
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Template (for descriptions or titles)', 'primelink' ); ?></label>
                <textarea id="mt_primelink-editor-template" rows="3" placeholder="<?php esc_attr_e( 'Use {value} to inject the mapped field. Example: &quot;Special: {value}&quot;. Leave {value} out to append/prepend.', 'primelink' ); ?>"></textarea>
                <div style="margin-top:4px;">
                    <label style="margin-right:8px;"><?php esc_html_e( 'Template placement', 'primelink' ); ?></label>
                    <label><input type="radio" name="mt_primelink-editor-template-mode" value="before" checked> <?php esc_html_e( 'Before value', 'primelink' ); ?></label>
                    <label style="margin-left:12px;"><input type="radio" name="mt_primelink-editor-template-mode" value="after"> <?php esc_html_e( 'After value', 'primelink' ); ?></label>
                    <label style="margin-left:12px;"><input type="radio" name="mt_primelink-editor-template-mode" value="replace"> <?php esc_html_e( 'Replace (use template only)', 'primelink' ); ?></label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Basic Transforms', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:repeat(3, 1fr);gap:8px;">
                    <label><input type="checkbox" id="mt_primelink-editor-trim" checked /> <?php esc_html_e( 'Trim spaces', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-lowercase" /> <?php esc_html_e( 'Lowercase', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-uppercase" /> <?php esc_html_e( 'Uppercase', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-capitalize" /> <?php esc_html_e( 'Capitalize first', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-title-case" /> <?php esc_html_e( 'Title Case', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-normalize-html" /> <?php esc_html_e( 'Normalize HTML', 'primelink' ); ?></label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Advanced Transforms', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:repeat(2, 1fr);gap:8px;">
                    <label><input type="checkbox" id="mt_primelink-editor-remove-accents" /> <?php esc_html_e( 'Remove accents/diacritics', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-slugify" /> <?php esc_html_e( 'Slugify (URL-friendly)', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-extract-numbers" /> <?php esc_html_e( 'Extract numbers only', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-extract-text" /> <?php esc_html_e( 'Extract text only (remove numbers)', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-url-encode" /> <?php esc_html_e( 'URL encode', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-url-decode" /> <?php esc_html_e( 'URL decode', 'primelink' ); ?></label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Number Transforms', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:repeat(2, 1fr);gap:8px;">
                    <label>
                        <input type="checkbox" id="mt_primelink-editor-round" />
                        <?php esc_html_e( 'Round number', 'primelink' ); ?>
                        <input type="number" id="mt_primelink-editor-round-decimals" min="0" max="10" value="2" style="width:60px;margin-left:4px;" placeholder="<?php esc_attr_e( 'decimals', 'primelink' ); ?>" />
                    </label>
                    <label><input type="checkbox" id="mt_primelink-editor-ceil" /> <?php esc_html_e( 'Ceil (round up)', 'primelink' ); ?></label>
                    <label><input type="checkbox" id="mt_primelink-editor-floor" /> <?php esc_html_e( 'Floor (round down)', 'primelink' ); ?></label>
                    <label>
                        <?php esc_html_e( 'Format number', 'primelink' ); ?>
                        <select id="mt_primelink-editor-number-format" style="width:120px;margin-left:4px;">
                            <option value=""><?php esc_html_e( 'None', 'primelink' ); ?></option>
                            <option value="comma">1,234.56</option>
                            <option value="dot">1.234,56</option>
                            <option value="space">1 234.56</option>
                        </select>
                    </label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'String Operations', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:repeat(2, 1fr);gap:8px;">
                    <label>
                        <?php esc_html_e( 'Substring', 'primelink' ); ?>
                        <input type="number" id="mt_primelink-editor-substring-start" placeholder="<?php esc_attr_e( 'Start', 'primelink' ); ?>" style="width:80px;margin-left:4px;" />
                        <input type="number" id="mt_primelink-editor-substring-length" placeholder="<?php esc_attr_e( 'Length', 'primelink' ); ?>" style="width:80px;margin-left:4px;" />
                    </label>
                    <label>
                        <?php esc_html_e( 'Pad', 'primelink' ); ?>
                        <select id="mt_primelink-editor-pad-side" style="width:80px;margin-left:4px;">
                            <option value=""><?php esc_html_e( 'None', 'primelink' ); ?></option>
                            <option value="left"><?php esc_html_e( 'Left', 'primelink' ); ?></option>
                            <option value="right"><?php esc_html_e( 'Right', 'primelink' ); ?></option>
                        </select>
                        <input type="number" id="mt_primelink-editor-pad-length" placeholder="<?php esc_attr_e( 'Length', 'primelink' ); ?>" style="width:60px;margin-left:4px;" />
                        <input type="text" id="mt_primelink-editor-pad-char" placeholder="<?php esc_attr_e( 'Char', 'primelink' ); ?>" style="width:40px;margin-left:4px;" maxlength="1" />
                    </label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Date/Time Transforms', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:repeat(2, 1fr);gap:8px;">
                    <label>
                        <?php esc_html_e( 'Format date', 'primelink' ); ?>
                        <input type="text" id="mt_primelink-editor-date-format" placeholder="Y-m-d" style="width:120px;margin-left:4px;" />
                        <small class="description" style="display:block;margin-top:4px;"><?php esc_html_e( 'Input format (e.g. d/m/Y, Y-m-d)', 'primelink' ); ?></small>
                    </label>
                    <label>
                        <?php esc_html_e( 'Output format', 'primelink' ); ?>
                        <input type="text" id="mt_primelink-editor-date-output" placeholder="Y-m-d" style="width:120px;margin-left:4px;" />
                        <small class="description" style="display:block;margin-top:4px;"><?php esc_html_e( 'Output format (e.g. Y-m-d, d/m/Y)', 'primelink' ); ?></small>
                    </label>
                </div>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Regex Replace', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;">
                    <input type="text" id="mt_primelink-editor-regex-pattern" placeholder="<?php esc_attr_e( 'Pattern (e.g. /[0-9]+/)', 'primelink' ); ?>" />
                    <input type="text" id="mt_primelink-editor-regex-replacement" placeholder="<?php esc_attr_e( 'Replacement', 'primelink' ); ?>" />
                </div>
                <small class="description"><?php esc_html_e( 'Use regex pattern to find and replace. Example: /[0-9]+/ to replace all numbers.', 'primelink' ); ?></small>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Replace rules', 'primelink' ); ?></label>
                <div id="mt_primelink-editor-replace-wrap"></div>
                <button type="button" class="button" id="mt_primelink-editor-add-replace"><?php esc_html_e( 'Add Replace Rule', 'primelink' ); ?></button>
            </div>

            <div class="mt_primelink-form-row mt_primelink-price-adjust-row" style="display:none;">
                <label><?php esc_html_e( 'Price adjustment', 'primelink' ); ?></label>
                <div style="display:grid;grid-template-columns:1.2fr 1fr;gap:8px;">
                    <select id="mt_primelink-price-adjust-type">
                        <option value=""><?php esc_html_e( 'No adjustment', 'primelink' ); ?></option>
                        <option value="percent_plus"><?php esc_html_e( 'Increase by %', 'primelink' ); ?></option>
                        <option value="percent_minus"><?php esc_html_e( 'Decrease by %', 'primelink' ); ?></option>
                        <option value="fixed_plus"><?php esc_html_e( 'Increase by value', 'primelink' ); ?></option>
                        <option value="fixed_minus"><?php esc_html_e( 'Decrease by value', 'primelink' ); ?></option>
                    </select>
                    <input type="number" step="0.01" id="mt_primelink-price-adjust-amount" placeholder="10" />
                </div>
                <small class="description"><?php esc_html_e( 'Applies only to price / regular_price / sale_price targets.', 'primelink' ); ?></small>
            </div>

            <div class="mt_primelink-form-row">
                <label><?php esc_html_e( 'Conditions (all must pass)', 'primelink' ); ?></label>
                <div id="mt_primelink-editor-conditions-wrap"></div>
                <button type="button" class="button" id="mt_primelink-editor-add-condition"><?php esc_html_e( 'Add Condition', 'primelink' ); ?></button>
            </div>
        </div>

        <div class="mt_primelink-map-editor__footer">
            <button type="button" class="button button-primary" id="mt_primelink-editor-save"><?php esc_html_e( 'Save Options', 'primelink' ); ?></button>
        </div>
    </div>
</div>
