<?php

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

class AIEO_DMM_Brand_Display extends AIEO_DMM_Module_Base {

    const OPTION_KEY = 'aieo_dmm_brand_display_settings';

    public function get_id()       { return 'brand_display'; }
    public function get_label()    { return __( 'Brand Display', 'ai-eshop-optimizer' ); }
    public function get_priority() { return 35; }
    public function get_group()    { return 'product_display'; }

    public function register_hooks() {
        add_action( 'wp_ajax_aieo_dmm_brand_display_save', array( $this, 'ajax_save' ) );
        add_shortcode( 'aieo_brand_display',   array( $this, 'shortcode_handler' ) );
        add_shortcode( 'om_ppe_product_brand', array( $this, 'shortcode_handler' ) );

        $s = $this->get_settings();
        if ( empty( $s['enabled'] ) ) { return; }

        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );

        
        
        
        
        
        
        
        
        
        
        add_action( 'woocommerce_single_product_summary', array( $this, 'output_classic_before_title' ),     4 );
        add_action( 'woocommerce_single_product_summary', array( $this, 'output_classic_under_title' ),      6 );
        add_action( 'woocommerce_product_meta_end',       array( $this, 'output_classic_after_post_meta' )    );
    }

    
    public function output_classic_before_title() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        if ( $s['position'] === 'before_title' ) {
            echo wp_kses_post( $this->get_html( 'before_title' ) );
            return;
        }
        if ( $s['position'] === 'mobile_before_desktop_inline' ) {
            echo wp_kses_post( $this->get_html( 'mobile_only_before' ) );
        }
    }

    
    public function output_classic_under_title() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        if ( in_array( $s['position'], array( 'under_title', 'in_title_row', 'mobile_before_desktop_inline' ), true ) ) {
            echo wp_kses_post( $this->get_html() );
        }
    }

    
    public function output_classic_after_post_meta() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        if ( $s['position'] === 'after_post_meta' ) {
            echo wp_kses_post( $this->get_html() );
        }
    }

    public function get_settings() {
        $stored = get_option( self::OPTION_KEY, array() );
        if ( ! is_array( $stored ) ) { $stored = array(); }
        $merged = array_merge( array(
            'enabled'                 => false,
            'mode'                    => 'text',           
            'position'                => 'under_title',
            
            
            
            
            
            
            
            
            'show_label'              => false,            
            'label_text'              => __( 'Brand', 'ai-eshop-optimizer' ),
            
            
            
            
            
            'image_max_width'         => 120,
            'image_max_height'        => 40,
            
            'image_max_width_mobile'  => 90,
            'image_max_height_mobile' => 30,
            'mobile_breakpoint'       => 768,              
            'text_color'              => '#212121',
            'label_color'             => '#6b7280',
            'link_color'              => '',
            'title_bold'              => true,
            'font_size'               => '0.92em',
            'align'                   => 'flex-start',     
            'margin_top'              => 10,
            'margin_bottom'           => 10,
            'custom_class'            => '',
        ), $stored );
        
        
        
        
        
        
        
        
        
        if ( class_exists( 'AIEO_DMM_Product_Display' ) ) {
            $bz = AIEO_DMM_Product_Display::get_cluster_zones();
            $bz = isset( $bz['brand_display'] ) ? (string) $bz['brand_display'] : 'info';
            if ( $bz === 'beside_title' ) {
                $merged['position'] = 'in_title_row';
            } elseif ( in_array( $merged['position'], array( 'in_title_row', 'mobile_before_desktop_inline' ), true ) ) {
                $merged['position'] = 'under_title';
            }
        }
        return $merged;
    }

    public function ajax_save() {
        AIEO_DMM::verify_ajax_request();
        update_option( self::OPTION_KEY, array(
            'enabled'                 => ! empty( $_POST['enabled'] ),
            'mode'                    => ( isset( $_POST['mode'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['mode'] ) ), array( 'text', 'image' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : 'text',
            'position'                => ( isset( $_POST['position'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['position'] ) ), array( 'before_title', 'in_title_row', 'under_title', 'after_post_meta', 'mobile_before_desktop_inline' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['position'] ) ) : 'under_title',
            'show_label'              => ! empty( $_POST['show_label'] ),
            'label_text'              => isset( $_POST['label_text'] ) ? sanitize_text_field( wp_unslash( $_POST['label_text'] ) ) : '',
            'image_max_width'         => max( 20, min( 400, isset( $_POST['image_max_width'] ) ? (int) $_POST['image_max_width'] : 120 ) ),
            'image_max_height'        => max( 20, min( 400, isset( $_POST['image_max_height'] ) ? (int) $_POST['image_max_height'] : 40 ) ),
            'image_max_width_mobile'  => max( 20, min( 400, isset( $_POST['image_max_width_mobile'] ) ? (int) $_POST['image_max_width_mobile'] : 90 ) ),
            'image_max_height_mobile' => max( 20, min( 400, isset( $_POST['image_max_height_mobile'] ) ? (int) $_POST['image_max_height_mobile'] : 30 ) ),
            'mobile_breakpoint'       => max( 320, min( 1280, isset( $_POST['mobile_breakpoint'] ) ? (int) $_POST['mobile_breakpoint'] : 768 ) ),
            'text_color'              => isset( $_POST['text_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['text_color'] ) ) ?: '#212121' ) : '#212121',
            'label_color'             => isset( $_POST['label_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['label_color'] ) ) ?: '#6b7280' ) : '#6b7280',
            'link_color'              => isset( $_POST['link_color'] ) ? ( sanitize_hex_color( wp_unslash( $_POST['link_color'] ) ) ?: '' ) : '',
            'title_bold'              => ! empty( $_POST['title_bold'] ),
            'font_size'               => ( isset( $_POST['font_size'] ) && preg_match( '/^[0-9]*\.?[0-9]+\s*(rem|em|px|%|vw|vh|pt)$/i', trim( (string) wp_unslash( $_POST['font_size'] ) ) ) ) ? trim( (string) wp_unslash( $_POST['font_size'] ) ) : '0.92em',
            'align'                   => ( isset( $_POST['align'] ) && in_array( sanitize_text_field( wp_unslash( $_POST['align'] ) ), array( 'flex-start', 'center', 'flex-end' ), true ) ) ? sanitize_text_field( wp_unslash( $_POST['align'] ) ) : 'flex-start',
            'margin_top'              => max( 0, min( 100, isset( $_POST['margin_top'] ) ? (int) $_POST['margin_top'] : 10 ) ),
            'margin_bottom'           => max( 0, min( 100, isset( $_POST['margin_bottom'] ) ? (int) $_POST['margin_bottom'] : 10 ) ),
            'custom_class'            => isset( $_POST['custom_class'] ) ? sanitize_html_class( wp_unslash( $_POST['custom_class'] ) ) : '',
        ), false );
        wp_send_json_success( array( 'message' => __( 'Brand Display settings saved.', 'ai-eshop-optimizer' ) ) );
    }

    public function enqueue_styles() {
        if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; }
        $s = $this->get_settings();

        
        
        
        
        
        $css = sprintf(
            '.aieo-brand-display{' .
                'display:flex;' .
                'align-items:center;' .
                'justify-content:%1$s;' .
                'gap:6px;' .
                'margin:%2$dpx 0 %3$dpx;' .
                'color:%4$s;' .
                'font-size:%11$s;' .
            '}' .
            '.aieo-brand-display a{color:inherit;text-decoration:none;}' .
            '.aieo-brand-display a:hover{text-decoration:underline;}' .
            '.aieo-brand-display-label{color:%5$s;}' .
            '.aieo-brand-display--image img{' .
                'display:block;' .
                'width:%6$dpx;' .
                'height:%7$dpx;' .
                'object-fit:contain;' .
                'object-position:%1$s center;' .
            '}' .
            
            '.aieo-brand-display--mobile-only-before{display:none;}' .
            '.aieo-brand-display--desktop-only-inline{display:flex;}' .
            '@media (max-width:%8$dpx){' .
                '.aieo-brand-display--image img{' .
                    'width:%9$dpx;' .
                    'height:%10$dpx;' .
                '}' .
                
                '.aieo-brand-display--before-title{' .
                    'justify-content:center;' .
                '}' .
                '.aieo-brand-display--before-title.aieo-brand-display--image img{' .
                    'object-position:center center;' .
                '}' .
                
                '.aieo-brand-display--mobile-only-before{display:flex;}' .
                '.aieo-brand-display--desktop-only-inline{display:none !important;}' .
            '}',
            esc_attr( $s['align'] ),                       
            (int) $s['margin_top'],                        
            (int) $s['margin_bottom'],                     
            esc_attr( $s['text_color'] ),                  
            esc_attr( $s['label_color'] ),                 
            (int) $s['image_max_width'],                   
            (int) $s['image_max_height'],                  
            (int) $s['mobile_breakpoint'],                 
            (int) $s['image_max_width_mobile'],            
            (int) $s['image_max_height_mobile'],           
            esc_attr( (string) $s['font_size'] )           
        );
        
        
        $css .= '.aieo-brand-display .aieo-brand-display-label{font-weight:' . ( ! empty( $s['title_bold'] ) ? '600' : '400' ) . ';}';
        if ( ! empty( $s['link_color'] ) ) {
            $css .= '.aieo-brand-display a{color:' . esc_attr( (string) $s['link_color'] ) . ';}';
        }
        
        
        wp_register_style( 'aieo-brand-display', false, array(), defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1.0' );
        wp_enqueue_style( 'aieo-brand-display' );
        wp_add_inline_style( 'aieo-brand-display', $css );
    }

    
    public function is_active() {
        $s = $this->get_settings();
        return ! empty( $s['enabled'] );
    }

    
    public function render_before_title() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        
        
        
        if ( $s['position'] === 'before_title' ) {
            echo wp_kses_post( $this->get_html( 'before_title' ) );
            return;
        }
        if ( $s['position'] === 'mobile_before_desktop_inline' ) {
            echo wp_kses_post( $this->get_html( 'mobile_only_before' ) );
            return;
        }
    }

    
    public function render_under_title() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        if ( $s['position'] !== 'under_title' ) { return; }
        echo wp_kses_post( $this->get_html() );
    }

    
    public function render_after_post_meta() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        if ( $s['position'] !== 'after_post_meta' ) { return; }
        echo wp_kses_post( $this->get_html() );
    }

    
    public function render_in_title_row() {
        if ( ! $this->is_active() ) { return; }
        $s = $this->get_settings();
        
        
        
        if ( $s['position'] === 'in_title_row' ) {
            echo wp_kses_post( $this->get_html( 'in_title_row' ) );
            return;
        }
        if ( $s['position'] === 'mobile_before_desktop_inline' ) {
            echo wp_kses_post( $this->get_html( 'desktop_only_inline' ) );
            return;
        }
    }

    
    public function output() {
        if ( ! $this->is_active() ) { return; }
        echo wp_kses_post( $this->get_html() );
    }

    public function shortcode_handler( $atts = array() ) {
        $s = $this->get_settings();
        if ( empty( $s['enabled'] ) ) { return ''; }
        if ( ! is_singular( 'product' ) ) {
            if ( current_user_can( 'manage_options' ) ) {
                return '<p style="color:red;">' . esc_html__( 'Brand Display shortcode can only be used on single product pages.', 'ai-eshop-optimizer' ) . '</p>';
            }
            return '';
        }
        return $this->get_html();
    }

    
    private function get_brand_term( $product_id ) {
        if ( ! $product_id ) { return null; }

        $detected = class_exists( 'AIEO_DMM_Brand_Discounts' )
            ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy()
            : null;
        $taxes = $detected
            ? array( $detected )
            : array( 'product_brand', 'pwb-brand', 'yith_product_brand', 'product_brands', 'berocket_brand' );

        foreach ( $taxes as $tax ) {
            if ( ! taxonomy_exists( $tax ) ) { continue; }
            $terms = get_the_terms( $product_id, $tax );
            if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
                return $terms[0];
            }
        }
        return null;
    }

    
    private function get_brand_logo_id( $term_id ) {
        if ( ! $term_id ) { return 0; }
        global $wpdb;
        $table = $wpdb->prefix . 'aieo_dmm_brand_extras';
        
        
        
        if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) {
            
            $id = (int) $wpdb->get_var( $wpdb->prepare(
                "SELECT logo_image_id FROM {$table} WHERE term_id = %d LIMIT 1",
                $term_id
            ) );
            if ( $id > 0 ) { return $id; }
        }
        
        $wc_id = (int) get_term_meta( $term_id, 'thumbnail_id', true );
        return $wc_id > 0 ? $wc_id : 0;
    }

    
    private function build_logo_img_html( $image_id, $brand_term, $max_w, $max_h ) {
        $src = wp_get_attachment_image_url( $image_id, 'medium' );
        if ( ! $src ) { return ''; }

        $srcset = function_exists( 'wp_get_attachment_image_srcset' )
            ? (string) wp_get_attachment_image_srcset( $image_id, 'medium' )
            : '';
        $sizes  = $srcset !== '' ? sprintf( '%dpx', $max_w ) : '';

        $alt   = (string) $brand_term->name;
        $title = (string) $brand_term->name;

        $parts = array(
            'src="' . esc_url( $src ) . '"',
        );
        if ( $srcset !== '' ) {
            $parts[] = 'srcset="' . esc_attr( $srcset ) . '"';
            $parts[] = 'sizes="'  . esc_attr( $sizes )  . '"';
        }
        $parts[] = 'width="'  . (int) $max_w . '"';
        $parts[] = 'height="' . (int) $max_h . '"';
        $parts[] = 'class="aieo-brand-display-img"';
        $parts[] = 'alt="'   . esc_attr( $alt )   . '"';
        $parts[] = 'title="' . esc_attr( $title ) . '"';
        $parts[] = 'loading="lazy"';
        $parts[] = 'decoding="async"';

        return '<img ' . implode( ' ', $parts ) . '>';
    }

    private function get_html( $render_position = '' ) {
        
        
        
        
        
        
        
        
        global $product;
        $product_id = 0;
        if ( $product && is_a( $product, 'WC_Product' ) ) {
            $product_id = (int) $product->get_id();
        } elseif ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) {
            $product_id = (int) get_the_ID();
        }
        if ( ! $product_id ) { return ''; }

        $brand_term = $this->get_brand_term( $product_id );
        if ( ! $brand_term ) { return ''; }

        $url = get_term_link( $brand_term );
        if ( is_wp_error( $url ) ) { return ''; }

        $s = $this->get_settings();
        $custom_class = ! empty( $s['custom_class'] ) ? ' ' . esc_attr( $s['custom_class'] ) : '';
        
        
        
        
        
        
        if ( $render_position === 'in_title_row' ) {
            $custom_class .= ' aieo-brand-display--in-title-row';
        } elseif ( $render_position === 'before_title' ) {
            $custom_class .= ' aieo-brand-display--before-title';
        } elseif ( $render_position === 'mobile_only_before' ) {
            
            
            $custom_class .= ' aieo-brand-display--before-title aieo-brand-display--mobile-only-before';
        } elseif ( $render_position === 'desktop_only_inline' ) {
            
            $custom_class .= ' aieo-brand-display--in-title-row aieo-brand-display--desktop-only-inline';
        }

        $label = '';
        if ( ! empty( $s['show_label'] ) && ! empty( $s['label_text'] ) ) {
            $label = '<span class="aieo-brand-display-label">' . esc_html( $s['label_text'] ) . '</span> ';
        }

        
        if ( $s['mode'] === 'image' ) {
            $image_id = $this->get_brand_logo_id( (int) $brand_term->term_id );
            if ( $image_id > 0 ) {
                $img = $this->build_logo_img_html( $image_id, $brand_term, (int) $s['image_max_width'], (int) $s['image_max_height'] );
                if ( $img !== '' ) {
                    return sprintf(
                        '<div class="aieo-brand-display aieo-brand-display--image%s">%s<a href="%s" title="%s">%s</a></div>',
                        $custom_class,
                        $label,
                        esc_url( $url ),
                        esc_attr( $brand_term->name ),
                        $img
                    );
                }
            }
            
        }

        
        return sprintf(
            '<div class="aieo-brand-display aieo-brand-display--text%s">%s<a href="%s">%s</a></div>',
            $custom_class,
            $label,
            esc_url( $url ),
            esc_html( $brand_term->name )
        );
    }

    public function render_subtab() {
        $s     = $this->get_settings();
        $nonce = wp_create_nonce( AIEO_DMM::NONCE_ACTION );
        $ajax  = admin_url( 'admin-ajax.php' );

        echo '<div class="aieo-dmm-section">';
        echo '<h2>' . esc_html__( 'Brand Display', 'ai-eshop-optimizer' ) . '</h2>';
        echo '<p class="description">' . esc_html__( 'Show the product brand on single product pages as either text or the brand logo image, linked to the brand archive. The brand is read from the `product_brand` taxonomy; the logo comes from the AIEO brand extras table (same source as the brand-archive hero).', 'ai-eshop-optimizer' ) . '</p>';

        echo '<form id="aieo-dmm-bd-form" class="aieo-dmm-form aieo-dmm-card">';
        echo '<table class="form-table">';

        echo '<tr><th>' . esc_html__( 'Enable', 'ai-eshop-optimizer' ) . '</th><td><label><input type="checkbox" name="enabled" value="1" ' . checked( ! empty( $s['enabled'] ), true, false ) . '> ' . esc_html__( 'Show brand on single product pages', 'ai-eshop-optimizer' ) . '</label></td></tr>';

        echo '<tr><th>' . esc_html__( 'Display mode', 'ai-eshop-optimizer' ) . '</th><td>';
        foreach ( array( 'text' => __( 'Brand name (text link)', 'ai-eshop-optimizer' ), 'image' => __( 'Brand logo (image link, falls back to text if no logo)', 'ai-eshop-optimizer' ) ) as $val => $label_txt ) {
            echo '<label style="margin-right:20px;"><input type="radio" name="mode" value="' . esc_attr( $val ) . '" ' . checked( $s['mode'], $val, false ) . '> ' . esc_html( $label_txt ) . '</label>';
        }
        echo '</td></tr>';

        echo '<tr><th>' . esc_html__( 'Position', 'ai-eshop-optimizer' ) . '</th><td>';
        foreach ( array(
            'before_title'                 => __( 'Before the product title (brand right after the product gallery)', 'ai-eshop-optimizer' ),
            'in_title_row'                 => __( 'In the title row (between title and wishlist heart, vertically centred)', 'ai-eshop-optimizer' ),
            'mobile_before_desktop_inline' => __( '⭐ Responsive — mobile: before title (under gallery); desktop: in title row', 'ai-eshop-optimizer' ),
            'under_title'                  => __( 'Under the product title', 'ai-eshop-optimizer' ),
            'after_post_meta'              => __( 'After SKU + barcode (post-meta block)', 'ai-eshop-optimizer' ),
        ) as $val => $label_txt ) {
            echo '<label style="display:block;margin-bottom:6px;"><input type="radio" name="position" value="' . esc_attr( $val ) . '" ' . checked( $s['position'], $val, false ) . '> ' . esc_html( $label_txt ) . '</label>';
        }
        echo '</td></tr>';

        echo '<tr><th colspan="2"><h3 style="margin:14px 0 0;">' . esc_html__( 'Logo dimensions (Brand logo mode only)', 'ai-eshop-optimizer' ) . '</h3><p class="description" style="margin:4px 0 0;">' . esc_html__( 'These exact values become the rendered <img> width/height — Core Web Vitals friendly (zero CLS). The image is fit inside the box with object-fit:contain, so the aspect ratio is preserved.', 'ai-eshop-optimizer' ) . '</p></th></tr>';
        echo '<tr><th>' . esc_html__( 'Desktop width', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="image_max_width" min="20" max="400" value="' . (int) $s['image_max_width'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Desktop height', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="image_max_height" min="20" max="400" value="' . (int) $s['image_max_height'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Mobile width', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="image_max_width_mobile" min="20" max="400" value="' . (int) $s['image_max_width_mobile'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Mobile height', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="image_max_height_mobile" min="20" max="400" value="' . (int) $s['image_max_height_mobile'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Mobile breakpoint', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="mobile_breakpoint" min="320" max="1280" value="' . (int) $s['mobile_breakpoint'] . '" class="small-text"> px <span style="color:#6b7280;font-size:12px;">' . esc_html__( '(viewport below this width uses the mobile dimensions)', 'ai-eshop-optimizer' ) . '</span></td></tr>';

        echo '<tr><th>' . esc_html__( 'Show label prefix', 'ai-eshop-optimizer' ) . '</th><td><label><input type="checkbox" name="show_label" value="1" ' . checked( ! empty( $s['show_label'] ), true, false ) . '> ' . esc_html__( 'Prepend a label before the brand (e.g. "Brand:")', 'ai-eshop-optimizer' ) . '</label></td></tr>';
        echo '<tr><th>' . esc_html__( 'Label text', 'ai-eshop-optimizer' ) . '</th><td><input type="text" name="label_text" value="' . esc_attr( (string) $s['label_text'] ) . '" class="regular-text"></td></tr>';

        echo '<tr><th>' . esc_html__( 'Alignment', 'ai-eshop-optimizer' ) . '</th><td><select name="align">';
        foreach ( array( 'flex-start' => __( 'Left', 'ai-eshop-optimizer' ), 'center' => __( 'Center', 'ai-eshop-optimizer' ), 'flex-end' => __( 'Right', 'ai-eshop-optimizer' ) ) as $val => $label_txt ) {
            echo '<option value="' . esc_attr( $val ) . '" ' . selected( $s['align'], $val, false ) . '>' . esc_html( $label_txt ) . '</option>';
        }
        echo '</select></td></tr>';

        echo '<tr><th>' . esc_html__( 'Brand text colour', 'ai-eshop-optimizer' ) . '</th><td><input type="color" name="text_color" value="' . esc_attr( (string) $s['text_color'] ) . '"></td></tr>';
        echo '<tr><th>' . esc_html__( 'Label colour', 'ai-eshop-optimizer' ) . '</th><td><input type="color" name="label_color" value="' . esc_attr( (string) $s['label_color'] ) . '"></td></tr>';
        echo '<tr><th>' . esc_html__( 'Link colour', 'ai-eshop-optimizer' ) . '</th><td><input type="text" name="link_color" value="' . esc_attr( (string) $s['link_color'] ) . '" placeholder="#0073aa" style="width:120px;"><p class="description">' . esc_html__( 'Colour of the brand link. Leave blank to inherit the brand text colour.', 'ai-eshop-optimizer' ) . '</p></td></tr>';
        echo '<tr><th>' . esc_html__( 'Bold the title', 'ai-eshop-optimizer' ) . '</th><td><label><input type="checkbox" name="title_bold" value="1" ' . checked( ! empty( $s['title_bold'] ), true, false ) . '> ' . esc_html__( 'Render the label (when shown) in bold', 'ai-eshop-optimizer' ) . '</label></td></tr>';
        echo '<tr><th>' . esc_html__( 'Font size (text mode)', 'ai-eshop-optimizer' ) . '</th><td><input type="text" name="font_size" value="' . esc_attr( (string) $s['font_size'] ) . '" placeholder="0.92em" style="width:120px;"><p class="description">' . esc_html__( 'CSS size, e.g. 0.92em or 13px. Match Categories / SKU for a consistent info column.', 'ai-eshop-optimizer' ) . '</p></td></tr>';
        echo '<tr><th>' . esc_html__( 'Margin top', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="margin_top" min="0" max="100" value="' . (int) $s['margin_top'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Margin bottom', 'ai-eshop-optimizer' ) . '</th><td><input type="number" name="margin_bottom" min="0" max="100" value="' . (int) $s['margin_bottom'] . '" class="small-text"> px</td></tr>';
        echo '<tr><th>' . esc_html__( 'Custom CSS class', 'ai-eshop-optimizer' ) . '</th><td><input type="text" name="custom_class" value="' . esc_attr( (string) $s['custom_class'] ) . '" class="regular-text"></td></tr>';

        echo '</table>';
        echo '<p><button type="button" class="button button-primary" id="aieo-dmm-bd-save">' . esc_html__( 'Save', 'ai-eshop-optimizer' ) . '</button>';
        echo ' <span id="aieo-dmm-bd-status" style="margin-left:10px;color:#646970;font-size:13px;"></span></p>';
        echo '</form>';
        echo '<script>(function(){var f=document.getElementById("aieo-dmm-bd-form");var b=document.getElementById("aieo-dmm-bd-save");var s=document.getElementById("aieo-dmm-bd-status");if(!b)return;b.addEventListener("click",function(){var fd=new FormData(f);fd.set("action","aieo_dmm_brand_display_save");fd.set("nonce","' . esc_js( $nonce ) . '");s.style.color="#646970";s.textContent="' . esc_js( __( 'Saving…', 'ai-eshop-optimizer' ) ) . '";fetch("' . esc_js( $ajax ) . '",{method:"POST",credentials:"same-origin",body:fd}).then(function(r){return r.json();}).then(function(j){if(j&&j.success){s.style.color="#15803d";s.textContent=j.data.message;}else{s.style.color="#b91c1c";s.textContent=(j&&j.data&&j.data.message)||"' . esc_js( __( 'Save failed.', 'ai-eshop-optimizer' ) ) . '";}});});})();</script>';
        echo '</div>';
    }
}
