<?php

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

class AIEO_DMM_Smart_Shipping_Label extends AIEO_DMM_Module_Base {

    const OPTION_KEY = 'aieo_dmm_smart_shipping_label';

    public function get_id()       { return 'smart_shipping_label'; }
    public function get_label()    { return __('Smart Shipping Label', 'ai-eshop-optimizer'); }
    public function get_priority() { return 100; }
    public function get_group()    { return 'checkout_handling'; }

    public function register_hooks() {
        add_action('wp_ajax_aieo_dmm_smart_shipping_label_save', array($this, 'ajax_save'));

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

        
        
        
        
        add_action('wp',                                       array($this, 'set_customer_default_location'), 5);
        add_action('woocommerce_init',                         array($this, 'set_customer_default_location_ajax'), 10);
        
        
        
        
        add_action('woocommerce_load_cart_from_session',       array($this, 'set_customer_default_location'), 5);
        
        
        add_filter('rest_pre_dispatch', array($this, 'maybe_seed_customer_for_storeapi'), 5, 3);
        
        
        add_filter('woocommerce_cart_shipping_packages',        array($this, 'set_default_for_empty_address'), 10);
        add_filter('woocommerce_cart_shipping_packages',        array($this, 'fallback_for_unmatched_postcode'), 20);
        add_filter('option_woocommerce_shipping_cost_requires_address', array($this, 'always_show_shipping'));

        
        add_filter('woocommerce_checkout_get_value',           array($this, 'hide_default_postcode_field_value'), 10, 2);
        add_filter('woocommerce_checkout_fields',              array($this, 'clear_postcode_defaults_fields'), 999);
        add_action('woocommerce_before_checkout_form',         array($this, 'clear_default_from_customer_display'));
        add_action('wp_footer',                                array($this, 'clear_default_postcode_js'));
        add_filter('woocommerce_package_rates',                 array($this, 'filter_shipping_rate_labels'), 10000, 2);
        add_filter('woocommerce_cart_shipping_method_full_label', array($this, 'strip_duplicate_prices_from_label'), 200, 2);
        add_filter('woocommerce_cart_shipping_method_full_label', array($this, 'filter_shipping_method_label'), 100, 2);
    }

    public function get_settings() {
        $stored = get_option(self::OPTION_KEY, array());
        if (!is_array($stored)) { $stored = array(); }
        $legacy = get_option('om_checkout_settings', array());
        if (!is_array($legacy)) { $legacy = array(); }
        return array_merge(array(
            'enabled'              => 'no',
            'default_postcode'     => isset($legacy['default_postcode']) ? (string) $legacy['default_postcode'] : '10431',
            'default_city'         => isset($legacy['default_city']) ? (string) $legacy['default_city'] : 'Athens',
            'shipping_display_from' => isset($legacy['shipping_display_from']) ? (string) $legacy['shipping_display_from'] : 'yes',
            'from_label'           => __('Από', 'ai-eshop-optimizer'),
        ), $stored);
    }

    public function ajax_save() {
        AIEO_DMM::verify_ajax_request();
        update_option(self::OPTION_KEY, array(
            'enabled'               => !empty($_POST['enabled']) ? 'yes' : 'no',
            'default_postcode'      => isset($_POST['default_postcode']) ? sanitize_text_field(wp_unslash($_POST['default_postcode'])) : '10431',
            'default_city'          => isset($_POST['default_city']) ? sanitize_text_field(wp_unslash($_POST['default_city'])) : 'Athens',
            'shipping_display_from' => !empty($_POST['shipping_display_from']) ? 'yes' : 'no',
            'from_label'            => isset($_POST['from_label']) ? sanitize_text_field(wp_unslash($_POST['from_label'])) : __('Από', 'ai-eshop-optimizer'),
        ), false);
        wp_send_json_success(array('message' => __('Smart shipping label settings saved.', 'ai-eshop-optimizer')));
    }

    
    
    

    private function get_default_postcode() {
        $s = $this->get_settings();
        return (string) $s['default_postcode'];
    }
    private function get_default_city() {
        $s = $this->get_settings();
        return (string) $s['default_city'];
    }
    
    public static function normalize_postcode($postcode) {
        return str_replace(' ', '', trim((string) $postcode));
    }

    
    private function is_checkout_context() {
        
        
        
        
        
        
        if (defined('REST_REQUEST') && REST_REQUEST) {
            $route = '';
            if (function_exists('rest_get_url_prefix')) {
                $uri = isset($_SERVER['REQUEST_URI']) ? (string) sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
                $route = (string) wp_parse_url($uri, PHP_URL_PATH);
            }
            
            if (strpos($route, '/wc/store/v1/cart') !== false ||
                strpos($route, '/wc/store/v1/checkout') !== false ||
                strpos($route, '/wc/store/cart') !== false ||
                strpos($route, '/wc/store/checkout') !== false) {
                return true;
            }
            return false;
        }
        if (!function_exists('is_checkout') || !function_exists('is_cart')) { return false; }
        if (is_checkout() || is_cart()) { return true; }
        if (wp_doing_ajax()) {
            $wc_ajax = isset($_GET['wc-ajax']) ? sanitize_text_field(wp_unslash($_GET['wc-ajax'])) : '';
            if (in_array($wc_ajax, array('update_order_review', 'checkout', 'get_refreshed_fragments'), true)) { return true; }
            $action = isset($_POST['action']) ? sanitize_text_field(wp_unslash($_POST['action'])) : '';
            if (strpos($action, 'woocommerce_checkout') !== false || strpos($action, 'wc_') !== false) { return true; }
        }
        return false;
    }

    
    private function get_table_rate_minimum($instance_id) {
        global $wpdb;
        $table = $wpdb->prefix . 'woocommerce_shipping_table_rates';
        $exists = (string) $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) === $table;
        if (!$exists) { return false; }
        $min = $wpdb->get_var($wpdb->prepare(
            "SELECT MIN(CAST(rate_cost AS DECIMAL(10,2))) FROM {$table}
             WHERE shipping_method_id = %d AND rate_cost > 0",
            (int) $instance_id
        ));
        return $min !== null ? (float) $min : false;
    }

    
    private function get_all_table_rate_minimums() {
        global $wpdb;
        static $cache = null;
        if ($cache !== null) { return $cache; }
        $cache = array();
        $rows = $wpdb->get_results(
            "SELECT zm.instance_id, zm.zone_id
             FROM {$wpdb->prefix}woocommerce_shipping_zone_methods zm
             WHERE zm.method_id = 'table_rate' AND zm.is_enabled = 1"
        );
        if (empty($rows)) { return $cache; }
        foreach ($rows as $r) {
            $instance_id = (int) $r->instance_id;
            if (isset($cache[$instance_id])) { continue; }
            $opt   = get_option('woocommerce_table_rate_' . $instance_id . '_settings', array());
            $title = (is_array($opt) && !empty($opt['title'])) ? (string) $opt['title'] : __('Table Rate', 'ai-eshop-optimizer');
            $min   = $this->get_table_rate_minimum($instance_id);
            if ($min !== false) {
                $cache[$instance_id] = array(
                    'title'    => $title,
                    'min_cost' => $min,
                    'rate_id'  => 'table_rate:' . $instance_id,
                    'zone_id'  => (int) $r->zone_id,
                );
            }
        }
        return $cache;
    }

    
    private function cart_qualifies_for_free_shipping() {
        static $q = null;
        if ($q !== null) { return $q; }
        $q = false;
        if (!function_exists('WC') || !WC() || !WC()->cart) { return false; }
        $subtotal = (float) WC()->cart->get_subtotal();
        $zones = WC_Data_Store::load('shipping-zone')->get_zones();
        foreach ($zones as $z) {
            $zone = new WC_Shipping_Zone($z);
            foreach ($zone->get_shipping_methods(true) as $m) {
                if ($m->id !== 'free_shipping') { continue; }
                $min = isset($m->min_amount) ? (float) $m->min_amount : 0;
                if ($min === 0.0 || $subtotal >= $min) { return $q = true; }
            }
        }
        
        $z0 = new WC_Shipping_Zone(0);
        foreach ($z0->get_shipping_methods(true) as $m) {
            if ($m->id !== 'free_shipping') { continue; }
            $min = isset($m->min_amount) ? (float) $m->min_amount : 0;
            if ($min === 0.0 || $subtotal >= $min) { return $q = true; }
        }
        return $q;
    }

    
    private function postcode_matches_zone($postcode) {
        if (empty($postcode)) { return false; }
        static $cache = array();
        if (isset($cache[$postcode])) { return $cache[$postcode]; }
        if (!class_exists('WC_Shipping_Zones')) { return $cache[$postcode] = true; }
        $zone = WC_Shipping_Zones::get_zone_matching_package(array(
            'destination' => array('country' => 'GR', 'state' => '', 'postcode' => $postcode, 'city' => ''),
        ));
        if (!$zone) { return $cache[$postcode] = false; }
        foreach ($zone->get_shipping_methods(true) as $m) {
            if (in_array($m->id, array('table_rate', 'flat_rate', 'box_now_delivery'), true)
                || strpos($m->id, 'box_now') !== false
                || strpos($m->id, 'courier') !== false
                || strpos($m->id, 'delivery') !== false) {
                return $cache[$postcode] = true;
            }
        }
        return $cache[$postcode] = false;
    }

    
    
    

    public function set_customer_default_location() {
        if (is_admin() && !wp_doing_ajax() && !(defined('REST_REQUEST') && REST_REQUEST)) { return; }
        if (!function_exists('WC') || !WC()->customer) { return; }

        
        
        
        
        
        
        
        
        $is_rest_storeapi = (defined('REST_REQUEST') && REST_REQUEST);
        if (!$is_rest_storeapi
            && function_exists('is_checkout') && function_exists('is_cart')
            && !is_checkout() && !is_cart() && !wp_doing_ajax()) {
            return;
        }

        $cur_pc = WC()->customer->get_shipping_postcode();
        $cur_co = WC()->customer->get_shipping_country();
        $def_pc = $this->get_default_postcode();
        $def_ci = $this->get_default_city();

        if ($cur_pc === '' && $cur_co === '') {
            WC()->customer->set_shipping_country('GR');
            WC()->customer->set_shipping_postcode($def_pc);
            WC()->customer->set_shipping_city($def_ci);
            $b_pc = WC()->customer->get_billing_postcode();
            $b_co = WC()->customer->get_billing_country();
            if ($b_pc === '' && $b_co === '') {
                WC()->customer->set_billing_country('GR');
                WC()->customer->set_billing_postcode($def_pc);
                WC()->customer->set_billing_city($def_ci);
            }
        }
    }

    
    public function maybe_seed_customer_for_storeapi($result, $server, $request) {
        if ($result !== null) { return $result; } 
        $route = $request instanceof WP_REST_Request ? (string) $request->get_route() : '';
        if (strpos($route, '/wc/store/') === false) { return $result; }
        $this->set_customer_default_location();
        return $result;
    }

    public function set_customer_default_location_ajax() {
        if (!wp_doing_ajax()) { return; }
        $wc_ajax = isset($_GET['wc-ajax']) ? sanitize_text_field(wp_unslash($_GET['wc-ajax'])) : '';
        $action  = isset($_REQUEST['action']) ? sanitize_text_field(wp_unslash($_REQUEST['action'])) : '';
        $hits = in_array($wc_ajax, array('update_order_review', 'checkout', 'wc_update_order_review'), true)
            || in_array($action, array('update_order_review', 'checkout', 'wc_update_order_review'), true)
            || strpos($action, 'woocommerce') !== false;
        if ($hits) { $this->set_customer_default_location(); }
    }

    public function set_default_for_empty_address($packages) {
        $def_pc = $this->get_default_postcode();
        $def_ci = $this->get_default_city();

        
        
        
        
        
        
        if (function_exists('WC') && WC() && WC()->customer) {
            $cur_pc = WC()->customer->get_shipping_postcode();
            $cur_co = WC()->customer->get_shipping_country();
            if ($cur_pc === '' && ($cur_co === '' || $cur_co === 'GR')) {
                WC()->customer->set_shipping_country('GR');
                WC()->customer->set_shipping_postcode($def_pc);
                if (method_exists(WC()->customer, 'set_shipping_city')) {
                    WC()->customer->set_shipping_city($def_ci);
                }
                $b_pc = WC()->customer->get_billing_postcode();
                $b_co = WC()->customer->get_billing_country();
                if ($b_pc === '' && ($b_co === '' || $b_co === 'GR')) {
                    WC()->customer->set_billing_country('GR');
                    WC()->customer->set_billing_postcode($def_pc);
                    if (method_exists(WC()->customer, 'set_billing_city')) {
                        WC()->customer->set_billing_city($def_ci);
                    }
                }
            }
        }

        foreach ($packages as $key => $package) {
            $pc = isset($package['destination']['postcode']) ? trim((string) $package['destination']['postcode']) : '';
            $co = isset($package['destination']['country']) ? (string) $package['destination']['country'] : '';
            
            if ($co === 'GR' && $pc !== '') {
                $packages[$key]['destination']['postcode'] = self::normalize_postcode($pc);
            }
            if ($pc === '' && $co === '') {
                $packages[$key]['destination']['country']  = 'GR';
                $packages[$key]['destination']['postcode'] = $def_pc;
                $packages[$key]['destination']['city']     = $def_ci;
            }
        }
        return $packages;
    }

    public function fallback_for_unmatched_postcode($packages) {
        $def_pc = $this->get_default_postcode();
        $def_ci = $this->get_default_city();
        foreach ($packages as $key => $package) {
            $pc = isset($package['destination']['postcode']) ? trim((string) $package['destination']['postcode']) : '';
            $co = isset($package['destination']['country']) ? (string) $package['destination']['country'] : '';
            if ($co !== 'GR' || $pc === '' || $pc === $def_pc) { continue; }
            if (!$this->postcode_matches_zone($pc)) {
                
                $packages[$key]['om_original_postcode'] = $pc;
                $packages[$key]['destination']['postcode'] = $def_pc;
                $packages[$key]['destination']['city']     = $def_ci;
            }
        }
        return $packages;
    }

    
    public function filter_shipping_rate_labels($rates, $package) {
        if (!$this->is_checkout_context()) { return $rates; }
        $s = $this->get_settings();
        if ($s['shipping_display_from'] !== 'yes') { return $rates; }

        $tr_min = $this->get_all_table_rate_minimums();
        $def_pc = $this->get_default_postcode();
        $def_pc_norm = self::normalize_postcode($def_pc);
        $user_entered_postcode = false;

        
        if (!is_user_logged_in()) {
            if (!empty($_POST)) {
                foreach (array('billing_postcode','shipping_postcode','postcode','s_postcode') as $f) {
                    if (!empty($_POST[$f])) {
                        $v = self::normalize_postcode((string) wp_unslash($_POST[$f]));
                        if (strlen($v) >= 4 && $v !== $def_pc_norm) { $user_entered_postcode = true; break; }
                    }
                }
            }
        } elseif (function_exists('WC') && WC() && WC()->customer) {
            $sp = self::normalize_postcode((string) WC()->customer->get_shipping_postcode());
            if (strlen($sp) >= 4 && $sp !== $def_pc_norm) {
                $user_entered_postcode = true;
            } else {
                $bp = self::normalize_postcode((string) WC()->customer->get_billing_postcode());
                if (strlen($bp) >= 4 && $bp !== $def_pc_norm) { $user_entered_postcode = true; }
            }
        }

        $qualifies_free = $this->cart_qualifies_for_free_shipping();
        $show_from   = !$user_entered_postcode && !$qualifies_free;
        $hide_prices = $qualifies_free;
        $from_label  = (string) $s['from_label'];

        foreach ($rates as $rate_id => $rate) {
            
            if (strpos($rate_id, 'table_rate:') === 0) {
                $parts = explode(':', $rate_id);
                $instance_id = isset($parts[1]) ? (int) $parts[1] : 0;
                if (isset($tr_min[$instance_id])) {
                    $title = (string) $tr_min[$instance_id]['title'];
                    if ($show_from) {
                        $min = $tr_min[$instance_id]['min_cost'];
                        $new_label = sprintf('%s (%s %s€)', $title, $from_label, number_format($min, 2, ',', ''));
                        $rate->set_label($new_label);
                        $rate->label = $new_label;
                        $rate->cost  = 0;
                        $rate->taxes = array();
                    } elseif ($hide_prices) {
                        $rate->set_label($title);
                        $rate->label = $title;
                        $rate->cost  = 0;
                        $rate->taxes = array();
                    } else {
                        $rate->set_label($title);
                        $rate->label = $title;
                    }
                }
            }
            
            if (strpos($rate_id, 'box_now_delivery') !== false) {
                if ($show_from) {
                    $cost = (float) $rate->get_cost();
                    
                    $new_label = sprintf(__('Box Now Delivery (%1$s %2$s€ for all of Greece)', 'ai-eshop-optimizer'), $from_label, number_format($cost, 2, ',', ''));
                    $rate->set_label($new_label);
                    $rate->cost  = 0;
                    $rate->taxes = array();
                } elseif ($hide_prices) {
                    $rate->set_label('Box Now Delivery');
                    $rate->cost  = 0;
                    $rate->taxes = array();
                }
            }
        }

        
        
        if ($hide_prices) {
            foreach ($rates as $rate_id => $rate) {
                if (strpos($rate_id, 'free_shipping') !== false) { unset($rates[$rate_id]); }
            }
        }

        
        
        $seen = array();
        $out  = array();
        foreach ($rates as $rate_id => $rate) {
            if (strpos($rate_id, 'local_pickup') === false && strpos($rate_id, 'pickup_location') === false) { $out[$rate_id] = $rate; continue; }
            $label = $rate->get_label();
            if (isset($seen[$label])) { continue; }
            $seen[$label] = true;
            $out[$rate_id] = $rate;
        }
        return $out;
    }

    
    public function strip_duplicate_prices_from_label($label, $method) {
        $rate_id = $method->get_id();
        if (strpos($rate_id, 'table_rate') !== false && strpos($label, 'Από') === false) {
            $rate_label = $method->get_label();
            if (strpos($rate_label, 'Από') !== false) { $label = $rate_label; }
        }
        if (strpos($label, 'Από') !== false || strpos($label, 'από') !== false) {
            $label = preg_replace('/\)\s*:.*$/s', ')', $label);
        }
        return trim((string) $label);
    }

    
    public function filter_shipping_method_label($label, $method) {
        $rate_id = $method->get_id();
        if (strpos($rate_id, 'table_rate:') !== 0) { return $label; }
        $parts = explode(':', $rate_id);
        if (!isset($parts[1])) { return $label; }
        $instance_id = (int) $parts[1];
        $tr = $this->get_all_table_rate_minimums();
        if (!isset($tr[$instance_id])) { return $label; }
        if (strpos($label, 'από') !== false || strpos($label, 'Από') !== false) { return $label; }
        $title = (string) $tr[$instance_id]['title'];
        $cost  = (float) $method->get_cost();
        if ($cost > 0) { return sprintf('%s: %s', $title, wc_price($cost)); }
        return $title;
    }

    
    
    

    public function hide_default_postcode_field_value($value, $input) {
        if (!function_exists('is_checkout') || !is_checkout()) { return $value; }
        if (is_user_logged_in()) { return $value; }
        if (in_array($input, array('billing_postcode','shipping_postcode'), true)) {
            $def = self::normalize_postcode($this->get_default_postcode());
            if ($def !== '' && self::normalize_postcode((string) $value) === $def) { return ''; }
        }
        if (in_array($input, array('billing_city','shipping_city'), true)) {
            $def_city = $this->get_default_city();
            if ($def_city !== '' && $value !== '' && trim(strtolower((string) $value)) === trim(strtolower($def_city))) { return ''; }
        }
        return $value;
    }

    public function clear_postcode_defaults_fields($fields) {
        if (!function_exists('is_checkout') || !is_checkout()) { return $fields; }
        if (is_user_logged_in()) { return $fields; }
        foreach (array('billing','shipping') as $section) {
            if (isset($fields[$section]['postcode'])) {
                $fields[$section]['postcode']['default'] = '';
                $fields[$section]['postcode']['value']   = '';
            }
            if (isset($fields[$section]['city'])) {
                $fields[$section]['city']['default'] = '';
                $fields[$section]['city']['value']   = '';
            }
        }
        return $fields;
    }

    public function clear_default_from_customer_display() {
        if (!is_checkout() || is_user_logged_in()) { return; }
        if (!function_exists('WC') || !WC() || !WC()->customer) { return; }
        $def_pc = $this->get_default_postcode();
        $def_ci = $this->get_default_city();
        if (WC()->customer->get_billing_postcode() === $def_pc) { WC()->customer->set_billing_postcode(''); }
        if (WC()->customer->get_billing_city()     === $def_ci) { WC()->customer->set_billing_city(''); }
        if (WC()->customer->get_shipping_postcode() === $def_pc) { WC()->customer->set_shipping_postcode(''); }
        if (WC()->customer->get_shipping_city()     === $def_ci) { WC()->customer->set_shipping_city(''); }
    }

    public function clear_default_postcode_js() {
        if (!function_exists('is_checkout') || !is_checkout()) { return; }
        if (is_user_logged_in()) { return; }
        $def = $this->get_default_postcode();
        if ($def === '') { return; }
        ?>
        <script>
            jQuery(function($){
                var defaultPostcode = <?php echo wp_json_encode($def); ?>;
                function normalize(v){ return (v || '').toString().replace(/\s+/g,'').trim(); }
                function clearDefault(){
                    $('#billing_postcode, #shipping_postcode').each(function(){
                        var $f = $(this);
                        if ($f.data('omUserInput')) return;
                        if (normalize($f.val()) === normalize(defaultPostcode)) { $f.val(''); }
                    });
                }
                $(document).on('input', '#billing_postcode, #shipping_postcode', function(){ $(this).data('omUserInput', true); });
                clearDefault();
                $(document.body).on('updated_checkout', clearDefault);
            });
        </script>
        <?php
    }

    public function always_show_shipping($value) {
        return $this->is_checkout_context() ? 'no' : $value;
    }

    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__('Smart Shipping Label', 'ai-eshop-optimizer') . '</h2>';
        echo '<p class="description">' . esc_html__('Tackles the "how much will shipping cost?" abandonment driver: shows "Carrier (Από €X.XX)" for guests / users who haven\'t entered a postcode (using the MIN(rate_cost) from your table-rate config). Once a real postcode is entered, exact rates appear. When the cart qualifies for free shipping, all method prices hide and the generic "Free shipping" pseudo-method is removed so the customer picks a real carrier.', 'ai-eshop-optimizer') . '</p>';
        echo '<div class="notice notice-warning inline" style="margin:12px 0;padding:8px 12px;"><p>' . esc_html__('⚠ Touches checkout shipping display in detail. Test on staging first. Make sure the Default postcode is one that actually matches a valid shipping zone with delivery methods — otherwise couriers won\'t appear.', 'ai-eshop-optimizer') . '</p></div>';
        echo '<form id="aieo-dmm-ssl-form" class="aieo-dmm-form aieo-dmm-card"><table class="form-table">';
        echo '<tr><th>' . esc_html__('Enable', 'ai-eshop-optimizer') . '</th><td>';
        echo '<label><input type="checkbox" name="enabled" value="1" ' . checked($s['enabled'] === 'yes', true, false) . '></label></td></tr>';
        echo '<tr><th>' . esc_html__('Default postcode', 'ai-eshop-optimizer') . '</th><td>';
        echo '<input type="text" name="default_postcode" value="' . esc_attr((string) $s['default_postcode']) . '" style="width:140px;font-family:monospace;">';
        echo '<p class="description">' . esc_html__('Used internally for zone matching when no postcode is entered. Pick one that matches a zone with all your delivery methods (typically Athens / Πρώτη Ζώνη).', 'ai-eshop-optimizer') . '</p>';
        echo '</td></tr>';
        echo '<tr><th>' . esc_html__('Default city', 'ai-eshop-optimizer') . '</th><td>';
        echo '<input type="text" name="default_city" value="' . esc_attr((string) $s['default_city']) . '" class="regular-text">';
        echo '</td></tr>';
        echo '<tr><th>' . esc_html__('Show "Από €X" labels', 'ai-eshop-optimizer') . '</th><td>';
        echo '<label><input type="checkbox" name="shipping_display_from" value="1" ' . checked($s['shipping_display_from'] === 'yes', true, false) . '></label></td></tr>';
        echo '<tr><th>' . esc_html__('"From" label text', 'ai-eshop-optimizer') . '</th><td>';
        echo '<input type="text" name="from_label" value="' . esc_attr((string) $s['from_label']) . '" style="width:120px;">';
        echo '<p class="description">' . esc_html__('Default "Από". The legacy plugin used this Greek word; change for non-Greek shops.', 'ai-eshop-optimizer') . '</p>';
        echo '</td></tr></table>';
        echo '<p><button type="button" class="button button-primary" id="aieo-dmm-ssl-save">' . esc_html__('Save', 'ai-eshop-optimizer') . '</button>';
        echo ' <span id="aieo-dmm-ssl-status" style="margin-left:10px;color:#646970;font-size:13px;"></span></p></form>';
        echo '<script>(function(){var f=document.getElementById("aieo-dmm-ssl-form");var b=document.getElementById("aieo-dmm-ssl-save");var s=document.getElementById("aieo-dmm-ssl-status");if(!b)return;b.addEventListener("click",function(){var fd=new FormData(f);fd.set("action","aieo_dmm_smart_shipping_label_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>';
    }
}
