<?php

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

$enabled_roles = isset($settings['enabled_roles']) ? (array) $settings['enabled_roles'] : array();
$wpdt_active = class_exists('WPDataTable');
$viewer_table_id = isset($settings['wpdatatables_table_id']) ? (int) $settings['wpdatatables_table_id'] : 0;
$gf_role_map     = isset($settings['gf_role_map']) && is_array($settings['gf_role_map']) ? $settings['gf_role_map'] : array();
$gf_forms        = isset($gf_forms) && is_array($gf_forms) ? $gf_forms : array();
$gf_active       = !empty($gf_active);
$gf_samples_url  = isset($gf_samples_url) ? (string) $gf_samples_url : '';
?>
<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Enabled roles', 'ai-eshop-optimizer'); ?></h2>
    <form id="aieo-dmm-role-settings-form" class="aieo-dmm-form">
        <div class="aieo-dmm-checkbox-grid">
            <?php foreach ($all_roles as $slug => $label) : ?>
                <label>
                    <input type="checkbox" name="enabled_roles[]" value="<?php echo esc_attr($slug); ?>"
                        <?php checked(in_array($slug, $enabled_roles, true)); ?>>
                    <?php echo esc_html($label); ?>
                    <code><?php echo esc_html($slug); ?></code>
                </label>
            <?php endforeach; ?>
        </div>

        <h3><?php esc_html_e('Login message', 'ai-eshop-optimizer'); ?></h3>
        <p>
            <label>
                <input type="checkbox" name="show_login_message" <?php checked(!empty($settings['show_login_message'])); ?>>
                <?php esc_html_e('Show a message to guests inviting them to log in to see role prices', 'ai-eshop-optimizer'); ?>
            </label>
        </p>
        <p>
            <label for="aieo_dmm_login_message_text"><?php esc_html_e('Single-product message (HTML, may use {login_url})', 'ai-eshop-optimizer'); ?></label>
        </p>
        <?php
        wp_editor(
            (string) ($settings['login_message_text'] ?? ''),
            'aieo_dmm_login_message_text',
            array(
                'textarea_name' => 'login_message_text',
                'textarea_rows' => 4,
                'teeny'         => true,
                'media_buttons' => false,
                'quicktags'     => true,
            )
        );
        ?>
        <p style="margin-top:16px">
            <label for="aieo_dmm_login_message_archive"><?php esc_html_e('Archive message (HTML, may use {login_url})', 'ai-eshop-optimizer'); ?></label>
        </p>
        <?php
        wp_editor(
            (string) ($settings['login_message_archive'] ?? ''),
            'aieo_dmm_login_message_archive',
            array(
                'textarea_name' => 'login_message_archive',
                'textarea_rows' => 4,
                'teeny'         => true,
                'media_buttons' => false,
                'quicktags'     => true,
            )
        );
        ?>
        <p>
            <label><?php esc_html_e('Custom CSS class', 'ai-eshop-optimizer'); ?></label>
            <input type="text" name="login_message_css_class" value="<?php echo esc_attr((string) ($settings['login_message_css_class'] ?? '')); ?>" class="regular-text">
        </p>
        <p style="display:flex;flex-wrap:wrap;gap:8px 22px;align-items:center;">
            <label style="display:inline-flex;align-items:center;gap:6px;">
                <?php esc_html_e('Link colour', 'ai-eshop-optimizer'); ?>
                <input type="color" name="login_message_color" value="<?php echo esc_attr((string) ($settings['login_message_color'] ?? '#c82d52')); ?>">
            </label>
        </p>
        <p style="display:flex;flex-wrap:wrap;gap:8px 18px;align-items:center;">
            <strong style="min-width:140px;"><?php esc_html_e('Archive / cards font size', 'ai-eshop-optimizer'); ?></strong>
            <label style="display:inline-flex;align-items:center;gap:6px;">
                <?php esc_html_e('desktop (px)', 'ai-eshop-optimizer'); ?>
                <input type="number" name="login_message_font_archive_desktop" min="8" max="48" step="1" value="<?php echo esc_attr((string) ($settings['login_message_font_archive_desktop'] ?? '')); ?>" class="small-text" placeholder="14">
            </label>
            <label style="display:inline-flex;align-items:center;gap:6px;">
                <?php esc_html_e('mobile (px)', 'ai-eshop-optimizer'); ?>
                <input type="number" name="login_message_font_archive_mobile" min="8" max="48" step="1" value="<?php echo esc_attr((string) ($settings['login_message_font_archive_mobile'] ?? '')); ?>" class="small-text" placeholder="12">
            </label>
        </p>
        <p style="display:flex;flex-wrap:wrap;gap:8px 18px;align-items:center;">
            <strong style="min-width:140px;"><?php esc_html_e('Single product font size', 'ai-eshop-optimizer'); ?></strong>
            <label style="display:inline-flex;align-items:center;gap:6px;">
                <?php esc_html_e('desktop (px)', 'ai-eshop-optimizer'); ?>
                <input type="number" name="login_message_font_single_desktop" min="8" max="48" step="1" value="<?php echo esc_attr((string) ($settings['login_message_font_single_desktop'] ?? '')); ?>" class="small-text" placeholder="16">
            </label>
            <label style="display:inline-flex;align-items:center;gap:6px;">
                <?php esc_html_e('mobile (px)', 'ai-eshop-optimizer'); ?>
                <input type="number" name="login_message_font_single_mobile" min="8" max="48" step="1" value="<?php echo esc_attr((string) ($settings['login_message_font_single_mobile'] ?? '')); ?>" class="small-text" placeholder="14">
            </label>
        </p>
        <p class="description" style="margin-top:-4px;"><?php esc_html_e('Leave a font size empty to inherit the theme size. Colour applies to the link in both contexts.', 'ai-eshop-optimizer'); ?></p>
        <p style="margin-top:14px;">
            <label>
                <input type="checkbox" name="login_message_modal_enabled" value="1" <?php checked(!empty($settings['login_message_modal_enabled'])); ?>>
                <?php esc_html_e('On click, open a login modal (login form + social sign-in + the content below) instead of going to My Account', 'ai-eshop-optimizer'); ?>
            </label>
            <span class="description" style="display:block;margin-top:4px;"><?php esc_html_e('Social sign-in shows only if you have enabled providers for the “login” intent under Social login.', 'ai-eshop-optimizer'); ?></span>
        </p>
        <p>
            <label for="aieo_dmm_login_message_modal_content"><?php esc_html_e('Modal content (rich text — e.g. a link to your professionals page)', 'ai-eshop-optimizer'); ?></label>
        </p>
        <?php
        wp_editor(
            (string) ($settings['login_message_modal_content'] ?? ''),
            'aieo_dmm_login_message_modal_content',
            array(
                'textarea_name' => 'login_message_modal_content',
                'textarea_rows' => 4,
                'teeny'         => false,
                'media_buttons' => false,
                'quicktags'     => true,
            )
        );
        ?>
        <?php $aieo_rp_login_cluster = (bool) (class_exists('AIEO_DMM_Role_Pricing') ? AIEO_DMM_Role_Pricing::login_in_cluster() : false); ?>
        <p style="margin-top:10px;">
            <label>
                <input type="checkbox" name="login_message_in_cluster" value="1" <?php checked($aieo_rp_login_cluster); ?>>
                <?php esc_html_e('Position the single-product message in the product info column (orderable) instead of attached to the price', 'ai-eshop-optimizer'); ?>
            </label>
            <span class="description" style="display:block;margin-top:4px;">
                <?php
                printf(
                    
                    esc_html__('When ticked, the message becomes a draggable row you can position relative to Product Meta, Categories, etc. on %s. Archive pages keep showing it next to the price.', 'ai-eshop-optimizer'),
                    '<a href="' . esc_url(admin_url('admin.php?page=ai-eshop-dmm&dmm_tab=product_display')) . '">' . esc_html__('Product Display → Display Order', 'ai-eshop-optimizer') . '</a>'
                ); 
                ?>
            </span>
        </p>

        <h3 style="margin-top:24px;"><?php esc_html_e('Mini-cart badge', 'ai-eshop-optimizer'); ?></h3>
        <p class="description">
            <?php esc_html_e('Compact form of the message above, shown next to the price on the side cart, recently viewed, and recommendation cards. Hovering / tapping the badge reveals the full message as a tooltip. Same gate as above (only shows to anonymous users on products with role pricing).', 'ai-eshop-optimizer'); ?>
        </p>
        <table class="form-table" role="presentation">
            <tr>
                <th scope="row">
                    <label for="aieo_dmm_mini_cart_badge_icon"><?php esc_html_e('Icon', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="text" id="aieo_dmm_mini_cart_badge_icon" name="mini_cart_badge_icon"
                           value="<?php echo esc_attr((string) ($settings['mini_cart_badge_icon'] ?? '')); ?>"
                           class="regular-text" placeholder="%">
                    <p class="description"><?php esc_html_e('Emoji, glyph or short HTML (e.g. ⓘ, %, ⭐, an &lt;img&gt; tag). Leave empty to use just the text label.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_dmm_mini_cart_badge_text"><?php esc_html_e('Text label', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="text" id="aieo_dmm_mini_cart_badge_text" name="mini_cart_badge_text"
                           value="<?php echo esc_attr((string) ($settings['mini_cart_badge_text'] ?? '')); ?>"
                           class="regular-text" placeholder="PRO">
                    <p class="description"><?php esc_html_e('Short word shown next to the icon (1–4 characters works best). Plain text only.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_dmm_mini_cart_badge_alt"><?php esc_html_e('Tooltip text (HTML)', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <textarea id="aieo_dmm_mini_cart_badge_alt" name="mini_cart_badge_alt"
                              class="large-text" rows="3"
                              placeholder="<?php esc_attr_e('Defaults to the single-product message above when empty.', 'ai-eshop-optimizer'); ?>"><?php echo esc_textarea((string) ($settings['mini_cart_badge_alt'] ?? '')); ?></textarea>
                    <p class="description"><?php esc_html_e('Shown when the badge is hovered (desktop) or tapped (mobile). May use {login_url}.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
        </table>
        <p>
            <label><?php esc_html_e('wpDataTables table id (optional, for the price viewer)', 'ai-eshop-optimizer'); ?></label>
            <input type="number" min="0" name="wpdatatables_table_id" value="<?php echo esc_attr((string) $viewer_table_id); ?>" class="small-text">
            <?php if (!$wpdt_active) : ?>
                <span class="description"><?php esc_html_e('wpDataTables plugin not detected; falls back to a native table.', 'ai-eshop-optimizer'); ?></span>
            <?php endif; ?>
        </p>

        <h3 style="margin-top:24px;"><?php esc_html_e('Archive badge layout', 'ai-eshop-optimizer'); ?></h3>
        <p class="description">
            <?php esc_html_e('Where the SALE / PRO / Free-Gift badges appear on shop archive product cards. Whichever option you pick, the badges always stack vertically in the order SALE → PRO → Free Gift so they never overlap.', 'ai-eshop-optimizer'); ?>
        </p>
        <?php $badge_pos = isset($settings['archive_badge_position']) ? (string) $settings['archive_badge_position'] : 'image_top_left'; ?>
        <p>
            <label class="aieo-dmm-choice" style="display:block;margin:4px 0;">
                <input type="radio" name="archive_badge_position" value="image_top_left" <?php checked($badge_pos, 'image_top_left'); ?>>
                <strong><?php esc_html_e('Top-left over the product image', 'ai-eshop-optimizer'); ?></strong>
                — <?php esc_html_e('the standard WooCommerce SALE flash position', 'ai-eshop-optimizer'); ?>
            </label>
            <label class="aieo-dmm-choice" style="display:block;margin:4px 0;">
                <input type="radio" name="archive_badge_position" value="below_favourite" <?php checked($badge_pos, 'below_favourite'); ?>>
                <strong><?php esc_html_e('Vertical column under the favourite/wishlist icon', 'ai-eshop-optimizer'); ?></strong>
                — <?php esc_html_e('top-right of the card, stacked downward beneath the heart', 'ai-eshop-optimizer'); ?>
            </label>
        </p>

        <?php  if ( false ) : ?>
        <h3 style="margin-top:32px;"><?php esc_html_e('Gravity Forms registration', 'ai-eshop-optimizer'); ?></h3>
        <p class="description">
            <?php esc_html_e('Optional: drive professional registration / role updates from a Gravity Forms form. Leave both form IDs empty to disable. The hooks self-disable when Gravity Forms is not active. Each enabled role above gets a row below where you map it to the integer checkbox value the form emits — that is the integer you put in the GF checkbox option (Field → Choices → Value).', 'ai-eshop-optimizer'); ?>
        </p>
        <?php if (!$gf_active) : ?>
            <p class="description" style="color:#b45309;">
                <?php esc_html_e('Gravity Forms plugin not detected. You can still configure the IDs below; they will activate automatically once GF is installed and active.', 'ai-eshop-optimizer'); ?>
            </p>
        <?php endif; ?>

        <table class="form-table" role="presentation">
            <tr>
                <th scope="row">
                    <label for="aieo_gf_registration_form_id"><?php esc_html_e('Registration form ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <?php $reg_id = (int) ($settings['gf_registration_form_id'] ?? 0); ?>
                    <?php if (!empty($gf_forms)) : ?>
                        <select id="aieo_gf_registration_form_id" name="gf_registration_form_id">
                            <option value="0">— <?php esc_html_e('disabled', 'ai-eshop-optimizer'); ?> —</option>
                            <?php foreach ($gf_forms as $f) : ?>
                                <option value="<?php echo (int) $f['id']; ?>" <?php selected($reg_id, (int) $f['id']); ?>>
                                    #<?php echo (int) $f['id']; ?> — <?php echo esc_html($f['title']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php else : ?>
                        <input type="number" id="aieo_gf_registration_form_id" name="gf_registration_form_id" min="0" value="<?php echo esc_attr((string) $reg_id); ?>" class="small-text">
                    <?php endif; ?>
                    <p class="description"><?php esc_html_e('The form a guest fills to create a new account. Roles selected on this form are assigned on user_registered.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_gf_update_form_id"><?php esc_html_e('Update form ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <?php $upd_id = (int) ($settings['gf_update_form_id'] ?? 0); ?>
                    <?php if (!empty($gf_forms)) : ?>
                        <select id="aieo_gf_update_form_id" name="gf_update_form_id">
                            <option value="0">— <?php esc_html_e('disabled', 'ai-eshop-optimizer'); ?> —</option>
                            <?php foreach ($gf_forms as $f) : ?>
                                <option value="<?php echo (int) $f['id']; ?>" <?php selected($upd_id, (int) $f['id']); ?>>
                                    #<?php echo (int) $f['id']; ?> — <?php echo esc_html($f['title']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php else : ?>
                        <input type="number" id="aieo_gf_update_form_id" name="gf_update_form_id" min="0" value="<?php echo esc_attr((string) $upd_id); ?>" class="small-text">
                    <?php endif; ?>
                    <p class="description"><?php esc_html_e('The form a logged-in user fills to change their roles. Existing role assignments are reset and replaced.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_gf_checkbox_field_id"><?php esc_html_e('Checkbox field ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="number" id="aieo_gf_checkbox_field_id" name="gf_checkbox_field_id" min="1" value="<?php echo esc_attr((string) ($settings['gf_checkbox_field_id'] ?? 11)); ?>" class="small-text">
                    <p class="description"><?php esc_html_e('The numeric ID of the multi-checkbox field in the form (must be the same on both forms). Default 11.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
        </table>

        <h4 style="margin-top:18px;"><?php esc_html_e('Role → GF checkbox value', 'ai-eshop-optimizer'); ?></h4>
        <p class="description">
            <?php esc_html_e('Enter the integer value GF emits for each enabled role. (Edit the GF form → checkbox field → Choices column → "Value".) Roles left at 0 are not touched by the integration.', 'ai-eshop-optimizer'); ?>
        </p>
        <?php if (empty($enabled_roles)) : ?>
            <p class="description" style="color:#b45309;"><?php esc_html_e('Enable at least one role above first.', 'ai-eshop-optimizer'); ?></p>
        <?php else : ?>
            <table class="widefat striped" style="max-width:560px;">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Role slug', 'ai-eshop-optimizer'); ?></th>
                        <th><?php esc_html_e('Label', 'ai-eshop-optimizer'); ?></th>
                        <th style="width:120px;"><?php esc_html_e('GF value', 'ai-eshop-optimizer'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($enabled_roles as $slug) :
                        $label = isset($all_roles[$slug]) ? $all_roles[$slug] : $slug;
                        $val   = isset($gf_role_map[$slug]) ? (int) $gf_role_map[$slug] : 0;
                    ?>
                        <tr>
                            <td><code><?php echo esc_html($slug); ?></code></td>
                            <td><?php echo esc_html($label); ?></td>
                            <td>
                                <input type="number" name="gf_role_map[<?php echo esc_attr($slug); ?>]"
                                       min="0" value="<?php echo esc_attr((string) $val); ?>" class="small-text">
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>

        <h4 style="margin-top:18px;"><?php esc_html_e('Membership shortcode intros (optional)', 'ai-eshop-optimizer'); ?></h4>
        <p class="description">
            <?php esc_html_e('HTML rendered above the form by [membership_gf]. Tokens: {user_name}, {first_name}, {last_name} (logged-in intro); {return_url} (logged-out intro, e.g. for a social-login button block).', 'ai-eshop-optimizer'); ?>
        </p>
        <p>
            <label for="aieo_gf_intro_logged_in"><strong><?php esc_html_e('Logged-in intro', 'ai-eshop-optimizer'); ?></strong></label>
        </p>
        <textarea id="aieo_gf_intro_logged_in" name="gf_intro_logged_in" class="large-text code" rows="3"><?php echo esc_textarea((string) ($settings['gf_intro_logged_in'] ?? '')); ?></textarea>

        <p style="margin-top:10px;">
            <label for="aieo_gf_intro_logged_out"><strong><?php esc_html_e('Logged-out intro', 'ai-eshop-optimizer'); ?></strong></label>
        </p>
        <textarea id="aieo_gf_intro_logged_out" name="gf_intro_logged_out" class="large-text code" rows="3"><?php echo esc_textarea((string) ($settings['gf_intro_logged_out'] ?? '')); ?></textarea>

        <h4 style="margin-top:18px;"><?php esc_html_e('Advanced — membership page & program prompt (optional)', 'ai-eshop-optimizer'); ?></h4>
        <p class="description">
            <?php esc_html_e('Page hosting the [membership_gf] shortcode. When set, AIEO sends no-cache headers there and hard-refreshes after social login. The "prompt form" is a separate form on which submission can show a notice to professionals not yet enrolled.', 'ai-eshop-optimizer'); ?>
        </p>
        <table class="form-table" role="presentation">
            <tr>
                <th scope="row">
                    <label for="aieo_gf_professionals_page_id"><?php esc_html_e('Membership page ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="number" id="aieo_gf_professionals_page_id" name="gf_professionals_page_id" min="0" value="<?php echo esc_attr((string) ($settings['gf_professionals_page_id'] ?? 0)); ?>" class="small-text">
                    <p class="description"><?php esc_html_e('0 = disabled. The shortcode still renders without this; only the no-cache + post-login refresh are scoped to that page.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_gf_prompt_form_id"><?php esc_html_e('Prompt form ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <?php $prompt_id = (int) ($settings['gf_prompt_form_id'] ?? 0); ?>
                    <?php if (!empty($gf_forms)) : ?>
                        <select id="aieo_gf_prompt_form_id" name="gf_prompt_form_id">
                            <option value="0">— <?php esc_html_e('disabled', 'ai-eshop-optimizer'); ?> —</option>
                            <?php foreach ($gf_forms as $f) : ?>
                                <option value="<?php echo (int) $f['id']; ?>" <?php selected($prompt_id, (int) $f['id']); ?>>
                                    #<?php echo (int) $f['id']; ?> — <?php echo esc_html($f['title']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    <?php else : ?>
                        <input type="number" id="aieo_gf_prompt_form_id" name="gf_prompt_form_id" min="0" value="<?php echo esc_attr((string) $prompt_id); ?>" class="small-text">
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_gf_prompt_pro_field_id"><?php esc_html_e('"Is professional?" field ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="number" id="aieo_gf_prompt_pro_field_id" name="gf_prompt_pro_field_id" min="1" value="<?php echo esc_attr((string) ($settings['gf_prompt_pro_field_id'] ?? 51)); ?>" class="small-text">
                    <span class="description"><?php esc_html_e('value 1 = is pro', 'ai-eshop-optimizer'); ?></span>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="aieo_gf_prompt_member_field_id"><?php esc_html_e('"Already in program?" field ID', 'ai-eshop-optimizer'); ?></label>
                </th>
                <td>
                    <input type="number" id="aieo_gf_prompt_member_field_id" name="gf_prompt_member_field_id" min="1" value="<?php echo esc_attr((string) ($settings['gf_prompt_member_field_id'] ?? 48)); ?>" class="small-text">
                    <span class="description"><?php esc_html_e('value 1 = already enrolled (prompt suppressed)', 'ai-eshop-optimizer'); ?></span>
                </td>
            </tr>
        </table>

        <p style="margin-top:14px;">
            <button type="button" class="button aieo-dmm-gf-export-forms"
                    <?php disabled(!$gf_active); ?>>
                <?php esc_html_e('Export configured forms as sample JSON', 'ai-eshop-optimizer'); ?>
            </button>
            <span class="description">
                <?php esc_html_e('Bundles the registration + update + prompt forms above into a single JSON file you can hand to another site (importable via Forms → Import/Export → Import Forms).', 'ai-eshop-optimizer'); ?>
            </span>
        </p>
        <?php endif;  ?>

        <p class="submit">
            <button type="submit" class="button button-primary" data-action="aieo_dmm_role_pricing_save_settings">
                <?php esc_html_e('Save settings', 'ai-eshop-optimizer'); ?>
            </button>
        </p>
    </form>
</div>

<div class="aieo-dmm-section">
    <h2><?php esc_html_e('CSV upload', 'ai-eshop-optimizer'); ?></h2>
    <p class="description">
        <?php esc_html_e('Upload a CSV with one sku column and one column per enabled role. Prices accept both "." and "," decimal separators.', 'ai-eshop-optimizer'); ?>
    </p>
    <form id="aieo-dmm-role-upload-form" class="aieo-dmm-form" enctype="multipart/form-data">
        <div class="aieo-dmm-field-label"><?php esc_html_e('Import mode', 'ai-eshop-optimizer'); ?></div>
        <div style="margin-bottom:14px;">
            <label class="aieo-dmm-choice"><input type="radio" name="mode" value="merge" checked> <?php esc_html_e('Merge (update existing, add new)', 'ai-eshop-optimizer'); ?></label>
            <label class="aieo-dmm-choice"><input type="radio" name="mode" value="overwrite"> <?php esc_html_e('Overwrite (truncate + re-import)', 'ai-eshop-optimizer'); ?></label>
        </div>

        <div class="aieo-dmm-upload-row" style="margin-bottom:14px;">
            <input type="file" name="csv_file" accept=".csv,text/csv" required>
            <button type="submit" class="button button-primary aieo-dmm-btn-fixed" data-action="aieo_dmm_role_pricing_upload">
                <?php esc_html_e('Upload & process', 'ai-eshop-optimizer'); ?>
            </button>
        </div>

        <div class="aieo-dmm-actions">
            <button type="button" class="button aieo-dmm-btn-fixed aieo-dmm-download-template" data-action="aieo_dmm_role_pricing_download_template">
                <?php esc_html_e('Download sample template', 'ai-eshop-optimizer'); ?>
            </button>
            <button type="button" class="button aieo-dmm-btn-fixed aieo-dmm-export-prices" data-action="aieo_dmm_role_pricing_export">
                <?php esc_html_e('Export current prices', 'ai-eshop-optimizer'); ?>
            </button>
        </div>
    </form>

    <div class="aieo-dmm-progress" id="aieo-dmm-role-progress" style="display:none; margin-top:14px;">
        <div class="aieo-dmm-progress-bar"><span style="width:0%"></span></div>
        <p class="aieo-dmm-progress-text"></p>
        <pre class="aieo-dmm-progress-log" style="max-height:200px;overflow:auto;"></pre>
    </div>
</div>

<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Current role prices', 'ai-eshop-optimizer'); ?> <span class="count">(<?php echo (int) $total_rows; ?>)</span></h2>

    <?php if ($wpdt_active && $viewer_table_id > 0) : ?>
        <?php echo do_shortcode('[wpdatatable id=' . (int) $viewer_table_id . ']'); ?>
    <?php else : ?>
        <?php if (empty($price_rows)) : ?>
            <p class="description"><?php esc_html_e('No role prices imported yet.', 'ai-eshop-optimizer'); ?></p>
        <?php else : ?>
            <table class="widefat striped aieo-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Product ID', 'ai-eshop-optimizer'); ?></th>
                        <th><?php esc_html_e('SKU', 'ai-eshop-optimizer'); ?></th>
                        <th><?php esc_html_e('Type', 'ai-eshop-optimizer'); ?></th>
                        <?php foreach ($enabled_roles as $role) : ?>
                            <th><?php echo esc_html($role); ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($price_rows as $row) : ?>
                        <tr>
                            <td><?php echo (int) $row['product_id']; ?></td>
                            <td><code><?php echo esc_html((string) ($row['sku'] ?? '')); ?></code></td>
                            <td><?php echo esc_html((string) ($row['product_type'] ?? '')); ?></td>
                            <?php foreach ($enabled_roles as $role) :
                                $col = 'role_' . strtolower(preg_replace('/[^a-z0-9_]/', '_', $role));
                                if (preg_match('/^role_[0-9]/', $col)) {
                                    $col = 'role_r_' . substr($col, 5);
                                }
                            ?>
                                <td><?php echo isset($row[$col]) && $row[$col] !== null ? esc_html(number_format((float) $row[$col], 2)) : '—'; ?></td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <?php
            $total_pages = max(1, (int) ceil($total_rows / max(1, (int) $per_page)));
            if ($total_pages > 1) :
                $base_url = add_query_arg(array(
                    'page'    => AIEO_DMM::MENU_SLUG,
                    'dmm_tab' => 'role_pricing',
                ), admin_url('admin.php'));
                echo '<div class="aieo-dmm-pagination">';
                echo wp_kses_post(paginate_links(array(
                    'base'      => $base_url . '%_%',
                    'format'    => '&paged=%#%',
                    'current'   => (int) $current_page,
                    'total'     => $total_pages,
                    'prev_text' => '&laquo;',
                    'next_text' => '&raquo;',
                )));
                echo '</div>';
            endif;
            ?>
        <?php endif; ?>
    <?php endif; ?>
</div>
