<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
    ini_set('session.use_strict_mode', 1);
    ini_set('session.use_only_cookies', 1);
    session_start();
}

$lang = $_SESSION['selected_language'] ?? 'it';
$curr = $_SESSION['selected_currency'] ?? 'eur';
?>
<!DOCTYPE html>
<html>
<head>
    <title>🧪 Absolute Minimum Test</title>
    <style>
        body { font-family: Arial; margin: 40px; }
        select { padding: 10px; font-size: 16px; }
        button { padding: 10px 20px; margin: 10px 0; background: blue; color: white; cursor: pointer; }
        .log { background: #f0f0f0; padding: 20px; margin-top: 20px; font-family: monospace; min-height: 200px; }
    </style>
</head>
<body>
    <h1>Absolute Minimum Language/Currency Change Test</h1>
    
    <p>Current: Lang=<?php echo htmlspecialchars($lang); ?>, Curr=<?php echo htmlspecialchars($curr); ?></p>
    
    <h3>Change Language:</h3>
    <select id="lang">
        <option value="it" <?php echo $lang === 'it' ? 'selected' : ''; ?>>Italian</option>
        <option value="en" <?php echo $lang === 'en' ? 'selected' : ''; ?>>English</option>
        <option value="es" <?php echo $lang === 'es' ? 'selected' : ''; ?>>Spanish</option>
        <option value="de" <?php echo $lang === 'de' ? 'selected' : ''; ?>>German</option>
    </select>
    <button onclick="setLang()">Change Language</button>
    
    <h3>Change Currency:</h3>
    <select id="curr">
        <option value="eur" <?php echo $curr === 'eur' ? 'selected' : ''; ?>>EUR</option>
        <option value="usd" <?php echo $curr === 'usd' ? 'selected' : ''; ?>>USD</option>
        <option value="gbp" <?php echo $curr === 'gbp' ? 'selected' : ''; ?>>GBP</option>
        <option value="cad" <?php echo $curr === 'cad' ? 'selected' : ''; ?>>CAD</option>
    </select>
    <button onclick="setCurr()">Change Currency</button>
    
    <div class="log" id="log"></div>
    
    <script>
    function log(msg) {
        document.getElementById('log').innerHTML += msg + '<br>';
    }
    
    function setLang() {
        const val = document.getElementById('lang').value;
        log('Setting language to: ' + val);
        
        fetch('/emporio-sport/api_set_preference.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ type: 'language', value: val })
        })
        .then(r => {
            log('Status: ' + r.status);
            return r.json();
        })
        .then(d => {
            log('Response: ' + JSON.stringify(d));
            if (d.success) {
                log('✓ Success! Reloading...');
                setTimeout(() => location.reload(), 1000);
            }
        })
        .catch(e => {
            log('✗ Error: ' + e);
        });
    }
    
    function setCurr() {
        const val = document.getElementById('curr').value;
        log('Setting currency to: ' + val);
        
        fetch('/emporio-sport/api_set_preference.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ type: 'currency', value: val })
        })
        .then(r => {
            log('Status: ' + r.status);
            return r.json();
        })
        .then(d => {
            log('Response: ' + JSON.stringify(d));
            if (d.success) {
                log('✓ Success! Reloading...');
                setTimeout(() => location.reload(), 1000);
            }
        })
        .catch(e => {
            log('✗ Error: ' + e);
        });
    }
    
    log('Test page loaded');
    log('Session language: <?php echo htmlspecialchars($lang); ?>');
    log('Session currency: <?php echo htmlspecialchars($curr); ?>');
    </script>
</body>
</html>
