<?php
require_once 'auth_helper.php';
require_login();
$user = get_user();

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Online Election System for Senior High School';
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Settings | <?= htmlspecialchars($projectDescription) ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/dashboard.css?v=<?= time() ?>">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style>
        .settings-card {
            background: white;
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            max-width: 600px;
        }
        .settings-group {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 16px 0;
            border-bottom: 1px solid #f1f5f9;
        }
        .settings-group:last-child {
            border-bottom: none;
        }
        .settings-info h3 {
            margin: 0 0 4px 0;
            font-size: 1rem;
            color: #1e293b;
        }
        .settings-info p {
            margin: 0;
            font-size: 0.875rem;
            color: #64748b;
        }
        /* Toggle Switch */
        .switch {
            position: relative;
            display: inline-block;
            width: 50px;
            height: 24px;
        }
        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #e2e8f0;
            transition: .4s;
            border-radius: 24px;
        }
        .slider:before {
            position: absolute;
            content: "";
            height: 18px;
            width: 18px;
            left: 3px;
            bottom: 3px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }
        input:checked + .slider {
            background-color: #4f46e5;
        }
        input:checked + .slider:before {
            transform: translateX(26px);
        }
        
        /* Dark mode preview styles (optional, can be integrated into global CSS later) */
        .dark-mode-preview {
            margin-top: 20px;
            padding: 12px;
            background: #1e293b;
            color: white;
            border-radius: 8px;
            display: none;
        }
    </style>
</head>
<body class="dashboard-body <?= ($user['theme'] ?? 'light') === 'dark' ? 'dark-theme' : '' ?>">

    <?php require_once 'includes/sidebar.php'; ?>

    <div class="main-wrapper">
        <?php require_once 'includes/header.php'; ?>

        <main class="dashboard-content animate-fade-in">
            <div class="dashboard-header">
                <div>
                    <h1 style="margin: 0 0 4px 0; font-size: 1.5rem; color: #1e293b;">Settings</h1>
                    <div class="welcome-msg">Manage your account preferences</div>
                </div>
            </div>

            <div class="settings-card">
                <div class="settings-group">
                    <div class="settings-info">
                        <h3>Theme Toggle</h3>
                        <p>Switch between Light and Dark mode</p>
                    </div>
                    <div>
                        <label class="switch">
                            <input type="checkbox" id="themeToggle" <?= ($user['theme'] ?? 'light') === 'dark' ? 'checked' : '' ?>>
                            <span class="slider"></span>
                        </label>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script>
        lucide.createIcons();

        document.getElementById('themeToggle').addEventListener('change', function() {
            const theme = this.checked ? 'dark' : 'light';
            
            // Apply theme immediately to body
            if (theme === 'dark') {
                document.body.classList.add('dark-theme');
            } else {
                document.body.classList.remove('dark-theme');
            }

            // Save to database
            fetch('api/update_theme.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ theme: theme }),
            })
            .then(response => response.json())
            .then(data => {
                if (!data.success) {
                    console.error('Failed to update theme:', data.error);
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });
        });
    </script>
</body>
</html>
