<?php
// Test page - Verify Language & Currency Defaults

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// Set defaults
if (!isset($_SESSION['selected_language'])) {
    $_SESSION['selected_language'] = 'ENG';
}
if (!isset($_SESSION['selected_currency'])) {
    $_SESSION['selected_currency'] = 'USD';
}

require_once __DIR__ . '/config/currency_converter.php';

$lang = $_SESSION['selected_language'];
$curr = $_SESSION['selected_currency'];
$lang_code = ['ENG' => 'en', 'ITA' => 'it', 'ESP' => 'es', 'GER' => 'de'][$lang] ?? 'en';

?>
<!DOCTYPE html>
<html lang="<?php echo $lang_code; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Language & Currency Defaults Test</title>
    <style>
        body { font-family: Arial; padding: 30px; background: #f5f5f5; }
        .container { max-width: 700px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #333; margin-top: 0; }
        .box { background: #f0f0f0; padding: 20px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #2196F3; }
        .success { color: #4CAF50; font-weight: bold; font-size: 18px; }
        .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin: 20px 0; }
        .stat { background: #ecf0f1; padding: 15px; border-radius: 5px; text-align: center; }
        .stat-label { color: #666; font-size: 12px; font-weight: bold; text-transform: uppercase; }
        .stat-value { font-size: 24px; color: #2196F3; font-weight: bold; margin-top: 10px; }
        .price { font-size: 28px; color: #F07E2D; font-weight: bold; }
        .btn { padding: 12px 20px; margin: 5px; background: #2196F3; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 14px; font-weight: bold; }
        .btn:hover { background: #0b7dda; }
        .btn.active { background: #4CAF50; }
    </style>
</head>
<body>

<div class="container">
    <h1>✅ Language & Currency - Defaults Test</h1>

    <div class="box" style="background: #d4edda; border-left-color: #4CAF50;">
        <h2 style="color: #4CAF50; margin-top: 0;">🌍 Default Settings (CORRECT!)</h2>
        <div class="grid">
            <div class="stat">
                <div class="stat-label">DEFAULT LANGUAGE</div>
                <div class="stat-value" style="color: #4CAF50;"><?php echo $lang; ?></div>
            </div>
            <div class="stat">
                <div class="stat-label">DEFAULT CURRENCY</div>
                <div class="stat-value" style="color: #4CAF50;"><?php echo $curr; ?></div>
            </div>
        </div>
        <p style="margin: 0; color: #333; font-weight: bold;">✓ Website opens with <strong>English</strong> and <strong>Dollar ($)</strong></p>
    </div>

    <div class="box">
        <h2>📊 Current Settings</h2>
        <p><strong>Language:</strong> <span class="success"><?php echo $lang; ?></span></p>
        <p><strong>Currency:</strong> <span class="success"><?php echo $curr; ?></span></p>
        <p><strong>Price Test (€100):</strong> <span class="price"><?php echo getPriceDisplay(100); ?></span></p>
    </div>

    <div class="box">
        <h2>🌐 Change Language (Google Translate)</h2>
        <div style="display: flex; flex-wrap: wrap; gap: 10px;">
            <button class="btn <?php echo $lang === 'ENG' ? 'active' : ''; ?>" onclick="changeLang('ENG')">🇬🇧 English</button>
            <button class="btn <?php echo $lang === 'ITA' ? 'active' : ''; ?>" onclick="changeLang('ITA')">🇮🇹 Italiano</button>
            <button class="btn <?php echo $lang === 'ESP' ? 'active' : ''; ?>" onclick="changeLang('ESP')">🇪🇸 Español</button>
            <button class="btn <?php echo $lang === 'GER' ? 'active' : ''; ?>" onclick="changeLang('GER')">🇩🇪 Deutsch</button>
        </div>
    </div>

    <div class="box">
        <h2>💱 Change Currency</h2>
        <div style="display: flex; flex-wrap: wrap; gap: 10px;">
            <button class="btn <?php echo $curr === 'USD' ? 'active' : ''; ?>" onclick="changeCurr('USD')">🇺🇸 USD ($)</button>
            <button class="btn <?php echo $curr === 'EUR' ? 'active' : ''; ?>" onclick="changeCurr('EUR')">🇪🇺 EUR (€)</button>
            <button class="btn <?php echo $curr === 'GBP' ? 'active' : ''; ?>" onclick="changeCurr('GBP')">🇬🇧 GBP (£)</button>
            <button class="btn <?php echo $curr === 'CAD' ? 'active' : ''; ?>" onclick="changeCurr('CAD')">🇨🇦 CAD (C$)</button>
        </div>
    </div>

</div>

<script>
function changeLang(lang) {
    console.log('Changing language to:', lang);
    fetch('/emporio-sport/api_set_preference.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ type: 'language', value: lang })
    })
    .then(r => r.json())
    .then(data => {
        console.log('Language saved:', data);
        alert('Language changed to ' + lang);
        setTimeout(() => location.reload(), 800);
    })
    .catch(e => console.error('Error:', e));
}

function changeCurr(curr) {
    console.log('Changing currency to:', curr);
    fetch('/emporio-sport/api_set_preference.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ type: 'currency', value: curr })
    })
    .then(r => r.json())
    .then(data => {
        console.log('Currency saved:', data);
        alert('Currency changed to ' + curr);
        setTimeout(() => location.reload(), 500);
    })
    .catch(e => console.error('Error:', e));
}

console.log('Test loaded - Language:', '<?php echo $lang; ?>', 'Currency:', '<?php echo $curr; ?>');
</script>

</body>
</html>
