<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit();
}

$upi_file = 'data/upi.txt';
$upi = file_exists($upi_file) ? trim(file_get_contents($upi_file)) : 'Not Set';
$upi_file2 = 'data/upi2.txt';
$upi2 = file_exists($upi_file2) ? trim(file_get_contents($upi_file2)) : 'Not Set';

$json_file = 'data/language_amounts.json';
$languages = ["assamase","bangla","gujrati","Hindi","kannada","malyalam","marathi","odia","punjab","tamil","telugu"];
$data = [];

if (file_exists($json_file)) {
    $data = json_decode(file_get_contents($json_file), true);
}

// Button visibility settings
$settings_file = 'data/button_settings.json';
$default_settings = [
    'phonepe_visible' => true,
    'paytm_visible' => true
];

if (file_exists($settings_file)) {
    $settings = json_decode(file_get_contents($settings_file), true);
    // Merge with defaults to ensure all settings exist
    $settings = array_merge($default_settings, $settings);
} else {
    $settings = $default_settings;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard</title>
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --accent-color: #4895ef;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4bb543;
            --danger-color: #ff3333;
        }
        * { margin:0; padding:0; box-sizing:border-box; font-family:'Segoe UI',Tahoma,Geneva,Verdana,sans-serif; }
        body { background-color:#f5f7fa; color:var(--dark-color); line-height:1.6; }
        .container { max-width:1200px; margin:0 auto; padding:20px; }
        header { background-color:white; box-shadow:0 2px 10px rgba(0,0,0,0.1); padding:15px 0; margin-bottom:30px; }
        .header-content { display:flex; justify-content:space-between; align-items:center; }
        .welcome-message { font-size:1.5rem; color:var(--primary-color); font-weight:600; }
        .nav-links a { color:var(--primary-color); text-decoration:none; margin-left:20px; padding:8px 15px; border-radius:5px; transition:all 0.3s ease; }
        .nav-links a:hover { background-color:var(--primary-color); color:white; }
        .dashboard-card { background-color:white; border-radius:10px; box-shadow:0 4px 6px rgba(0,0,0,0.1); padding:25px; margin-bottom:30px; }
        .card-title { font-size:1.3rem; margin-bottom:20px; color:var(--secondary-color); border-bottom:2px solid #eee; padding-bottom:10px; }
        .current-upi { font-size:1.1rem; margin-bottom:20px; padding:15px; background-color:#f8f9fa; border-radius:5px; border-left:4px solid var(--accent-color); }
        .upi-form, .amount-form, .settings-form { display:flex; flex-direction:column; gap:20px; }
        .form-group { display:flex; flex-direction:column; }
        .form-group label { margin-bottom:8px; font-weight:500; }
        .form-group input { padding:10px; border:1px solid #ddd; border-radius:5px; font-size:1rem; }
        .btn { padding:12px 25px; background-color:var(--primary-color); color:white; border:none; border-radius:5px; cursor:pointer; font-size:1rem; }
        .btn:hover { background-color:var(--secondary-color); }
        .language-block { border:1px solid #eee; padding:15px; border-radius:8px; background:#fdfdfd; }
        .lang-title { font-size:1.1rem; font-weight:600; margin-bottom:10px; color:var(--accent-color); }
        .toggle-container { display:flex; align-items:center; gap:15px; margin-bottom:15px; }
        .toggle-label { font-weight:500; }
        .toggle-switch { position:relative; display:inline-block; width:60px; height:34px; }
        .toggle-switch input { opacity:0; width:0; height:0; }
        .toggle-slider { position:absolute; cursor:pointer; top:0; left:0; right:0; bottom:0; background-color:#ccc; transition:.4s; border-radius:34px; }
        .toggle-slider:before { position:absolute; content:""; height:26px; width:26px; left:4px; bottom:4px; background-color:white; transition:.4s; border-radius:50%; }
        input:checked + .toggle-slider { background-color:var(--success-color); }
        input:checked + .toggle-slider:before { transform:translateX(26px); }
        @media(max-width:768px){ .form-group{width:100%;} .btn{width:100%;} }
    </style>
</head>
<body>
    <header>
        <div class="container header-content">
            <div class="welcome-message">Welcome, <?php echo htmlspecialchars($_SESSION['admin']); ?>!</div>
            <div class="nav-links">
                <a href="logout.php">Logout</a>
                <a href="change_password.php">Change Password</a>
            </div>
        </div>
    </header>

    <main class="container">
        <div class="dashboard-card">
            <h3 class="card-title">Phonepe UPI ID Management</h3>
            <div class="current-upi">
                <strong>Current UPI ID:</strong> <?php echo htmlspecialchars($upi); ?>
            </div>
            <form method="POST" action="update_upi.php" class="upi-form">
                <div class="form-group">
                    <label for="upi">New UPI ID:</label>
                    <input type="text" id="upi" name="upi" required placeholder="Enter new UPI ID">
                </div>
                <button type="submit" class="btn">Update UPI</button>
            </form>
        </div>

        <div class="dashboard-card">
            <h3 class="card-title">Paytm UPI ID Management</h3>
            <div class="current-upi">
                <strong>Current UPI ID:</strong> <?php echo htmlspecialchars($upi2); ?>
            </div>
            <form method="POST" action="update_upi2.php" class="upi-form">
                <div class="form-group">
                    <label for="upi">New UPI ID:</label>
                    <input type="text" id="upi" name="upi" required placeholder="Enter new UPI ID">
                </div>
                <button type="submit" class="btn">Update UPI</button>
            </form>
        </div>

        <div class="dashboard-card">
            <h3 class="card-title">Button Visibility Settings</h3>
            <form method="POST" action="update_button_settings.php" class="settings-form">
                <div class="toggle-container">
                    <span class="toggle-label">PhonePe Button Visible:</span>
                    <label class="toggle-switch">
                        <input type="checkbox" name="phonepe_visible" <?php echo $settings['phonepe_visible'] ? 'checked' : ''; ?>>
                        <span class="toggle-slider"></span>
                    </label>
                </div>
                
                <div class="toggle-container">
                    <span class="toggle-label">Paytm Button Visible:</span>
                    <label class="toggle-switch">
                        <input type="checkbox" name="paytm_visible" <?php echo $settings['paytm_visible'] ? 'checked' : ''; ?>>
                        <span class="toggle-slider"></span>
                    </label>
                </div>
                
                <button type="submit" class="btn">Save Settings</button>
            </form>
        </div>

        <div class="dashboard-card">
            <h3 class="card-title">Language Wise Amounts</h3>
            <form method="POST" action="update_amounts.php" class="amount-form">
                <?php foreach ($languages as $lang): 
                    $min = isset($data[$lang]['min']) ? $data[$lang]['min'] : '';
                    $max = isset($data[$lang]['max']) ? $data[$lang]['max'] : '';
                ?>
                <div class="language-block">
                    <div class="lang-title"><?php echo ucfirst($lang); ?></div>
                    <div class="form-group">
                        <label>Min Amount:</label>
                        <input type="number" name="amounts[<?php echo $lang; ?>][min]" value="<?php echo htmlspecialchars($min); ?>" required>
                    </div>
                    <div class="form-group">
                        <label>Max Amount:</label>
                        <input type="number" name="amounts[<?php echo $lang; ?>][max]" value="<?php echo htmlspecialchars($max); ?>" required>
                    </div>
                </div>
                <?php endforeach; ?>
                <button type="submit" class="btn">Save Amounts</button>
            </form>
        </div>
    </main>
</body>
</html>