<?php
session_start();
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) exit('Access Denied');

$code = $_GET['code'] ?? '';
$file = "lang/{$code}.php";

if (!file_exists($file)) die("Language file not found.");

// Include the file to get the $lang array
include($file);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newLang = [];
    foreach ($_POST['keys'] as $key => $value) {
        $newLang[$key] = $value;
    }
    $content = "<?php\n\$lang = " . var_export($newLang, true) . ";";
    file_put_contents($file, $content);
    header("Location: manage_region.php");
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Edit <?php echo strtoupper($code); ?></title>
    <style>
        body { font-family: sans-serif; background: #0c0f15; color: white; padding: 20px; }
        .card { background: rgba(255, 255, 255, 0.05); padding: 20px; border-radius: 12px; max-width: 600px; margin: 0 auto; }
        input { width: 100%; padding: 10px; margin: 10px 0; background: #1a1a1a; border: 1px solid #333; color: white; border-radius: 6px; }
        button { padding: 10px 20px; background: #3b82f6; border: none; color: white; border-radius: 6px; cursor: pointer; }
    </style>
</head>
<body>
    <div class="card">
        <h3>Editing: <?php echo strtoupper($code); ?></h3>
        <form method="POST">
            <?php foreach ($lang as $key => $value): ?>
                <label style="color:#aaa; font-size:0.8em; text-transform:uppercase;"><?php echo $key; ?></label>
                <input type="text" name="keys[<?php echo $key; ?>]" value="<?php echo htmlspecialchars($value); ?>">
            <?php endforeach; ?>
            <button type="submit">Save Changes</button>
        </form>
    </div>
    <script>
        let percent = 0;
        let pText = document.createElement('div');
        let tLabel = document.createElement('div');
        
        setInterval(() => {
            percent = (percent + 1) % 101;
            pText.innerText = percent + "%";
            
            if (percent < 33) {
                tLabel.innerText = "BRONZE TIER";
                tLabel.className = "tier-label color-bronze";
            } else if (percent < 66) {
                tLabel.innerText = "SILVER TIER";
                tLabel.className = "tier-label color-silver";
            } else if (percent < 90) {
                tLabel.innerText = "GOLD TIER";
                tLabel.className = "tier-label color-gold";
            } else {
                tLabel.innerText = "PLATINUM TIER";
                tLabel.className = "tier-label color-platinum";
            }
        }, 100);
        
        // AI MANAGER LOGIC
        function getAiManagerResponse(status) {
            const responses = {
                'Pending': "Hello! I'm your Portfolio Manager. I'm currently verifying your documents. Please sit tight—I'll have you active shortly.",
                'Active': "Welcome back! I've been monitoring your growth. Today is a great day to increase your savings stake to hit that next tier.",
                'Suspended': "I have some difficult news. Your account has been suspended due to a compliance flag. Please contact our support team immediately to resolve this.",
                'Default': "Hello. I am here to guide your financial journey. How can I assist you today?"
            };
            return responses[status] || responses['Default'];
        }
    </script>
</body>
</html>