<?php
// Check if language and currency work together

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

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

// Debug info
$lang = $_SESSION['selected_language'] ?? 'ENG';
$curr = $_SESSION['selected_currency'] ?? 'EUR';

?>
<!DOCTYPE html>
<html lang="<?php echo ($_SESSION['selected_language'] === 'ITA' ? 'it' : ($_SESSION['selected_language'] === 'ESP' ? 'es' : ($_SESSION['selected_language'] === 'GER' ? 'de' : 'en'))); ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Language & Currency Check</title>
    <style>
        body { font-family: Arial; padding: 30px; background: #f5f5f5; }
        .container { max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #333; }
        .status { background: #e7f3ff; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #2196F3; }
        .test { background: #f0f0f0; padding: 15px; margin: 15px 0; border-radius: 5px; }
        .success { color: #4CAF50; font-weight: bold; }
        .error { color: #f44336; font-weight: bold; }
        .price { font-size: 24px; color: #F07E2D; font-weight: bold; }
        button { padding: 10px 20px; margin: 5px; background: #2196F3; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 14px; }
        button:hover { background: #0b7dda; }
    </style>
</head>
<body>

<div class="container">
    <h1>✅ Language & Currency System Test</h1>
    
    <div class="status">
        <h2>Current Settings</h2>
        <p><strong>Language Session:</strong> <span class="success"><?php echo $lang; ?></span></p>
        <p><strong>Currency Session:</strong> <span class="success"><?php echo $curr; ?></span></p>
        <p><strong>HTML Lang Attribute:</strong> <span class="success"><?php echo $_SESSION['selected_language'] === 'ITA' ? 'it' : ($_SESSION['selected_language'] === 'ESP' ? 'es' : ($_SESSION['selected_language'] === 'GER' ? 'de' : 'en')); ?></span></p>
    </div>

    <div class="test">
        <h3>Translation Test</h3>
        <p><strong>Key:</strong> 'NEED_HELP'</p>
        <p><strong>Translated Text:</strong> <span class="success"><?php echo __('NEED_HELP'); ?></span></p>
        <p style="color: #666; font-size: 12px;">The text above should change when you select a different language below.</p>
    </div>

    <div class="test">
        <h3>Currency Test</h3>
        <p><strong>Test Price:</strong> €100 (stored in database)</p>
        <p><strong>Converted Price:</strong> <span class="price"><?php echo getPriceDisplay(100); ?></span></p>
        <p style="color: #666; font-size: 12px;">The price and symbol above should change when you select a different currency below.</p>
    </div>

    <div class="test">
        <h3>Change Language (Page will reload)</h3>
        <p>Current: <strong><?php echo $lang; ?></strong></p>
        <button onclick="changeLang('ENG')">English</button>
        <button onclick="changeLang('ITA')">Italiano</button>
        <button onclick="changeLang('ESP')">Español</button>
        <button onclick="changeLang('GER')">Deutsch</button>
    </div>

    <div class="test">
        <h3>Change Currency (Page will reload)</h3>
        <p>Current: <strong><?php echo $curr; ?></strong></p>
        <button onclick="changeCurr('EUR')">Euro (€)</button>
        <button onclick="changeCurr('USD')">Dollar ($)</button>
        <button onclick="changeCurr('GBP')">Pound (£)</button>
        <button onclick="changeCurr('CAD')">Canadian ($)</button>
    </div>

    <div class="status" style="background: #f0f7f0; border-left-color: #4CAF50; margin-top: 30px;">
        <h3 style="color: #4CAF50;">✓ How It Works</h3>
        <ol>
            <li><strong>Language:</strong> Click a language button → API saves to session → Page reloads → Text changes</li>
            <li><strong>Currency:</strong> Click a currency button → API saves to session → Page reloads → Prices update</li>
            <li><strong>Translation File:</strong> Located in <code>config/translation_helper.php</code></li>
            <li><strong>Currency Conversion:</strong> Located in <code>config/currency_converter.php</code></li>
        </ol>
    </div>

    <div style="background: #fff3cd; padding: 15px; margin-top: 20px; border-radius: 5px; border-left: 4px solid #ff9800;">
        <h3>📋 Console Logs</h3>
        <p>Check the browser console (F12) for detailed logs when you change language or currency.</p>
        <p>Look for: "Changing language to:" and "Language saved:" messages</p>
    </div>
</div>

<script>
function changeLang(lang) {
    console.log('Test: 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('Test: Language saved:', data);
        setTimeout(() => { location.reload(); }, 500);
    })
    .catch(e => console.error('Test: Error:', e));
}

function changeCurr(curr) {
    console.log('Test: 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('Test: Currency saved:', data);
        setTimeout(() => { location.reload(); }, 500);
    })
    .catch(e => console.error('Test: Error:', e));
}

console.log('Translation & Currency system test page loaded');
console.log('Current language:', '<?php echo $lang; ?>');
console.log('Current currency:', '<?php echo $curr; ?>');
</script>

</body>
</html>
