/home/ejrndhmu/.trash/reseller.5/pages/settings.php
<?php
// Get current user data
$current_user = getUserById($user_id);

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $csrf_token = $_POST['csrf_token'] ?? '';
    
    if (!verifyCSRFToken($csrf_token)) {
        $error = 'Token keamanan tidak valid.';
    } else {
        switch ($action) {
            case 'update_profile':
                $full_name_new = sanitizeInput($_POST['full_name'] ?? '');
                $email_new = sanitizeInput($_POST['email'] ?? '');
                
                if (empty($full_name_new) || empty($email_new)) {
                    $error = 'Nama lengkap dan email harus diisi.';
                } else {
                    $result = updateUserProfile($user_id, $full_name_new, $email_new);
                    if ($result['success']) {
                        $success = 'Profil berhasil diperbarui.';
                        $_SESSION['full_name'] = $full_name_new;
                        $_SESSION['email'] = $email_new;
                        $current_user = getUserById($user_id); // Refresh data
                    } else {
                        $error = $result['message'];
                    }
                }
                break;
                
            case 'change_password':
                $current_password = $_POST['current_password'] ?? '';
                $new_password = $_POST['new_password'] ?? '';
                $confirm_password = $_POST['confirm_password'] ?? '';
                
                if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
                    $error = 'Semua field password harus diisi.';
                } elseif ($new_password !== $confirm_password) {
                    $error = 'Konfirmasi password tidak cocok.';
                } elseif (strlen($new_password) < 6) {
                    $error = 'Password baru minimal 6 karakter.';
                } elseif (!verifyPassword($current_password, $current_user['password'])) {
                    $error = 'Password saat ini salah.';
                } else {
                    $result = updateUserPassword($user_id, $new_password);
                    if ($result['success']) {
                        $success = 'Password berhasil diubah.';
                    } else {
                        $error = $result['message'];
                    }
                }
                break;
        }
    }
}
?>

<!-- Settings Page -->
<div class="mb-6">
    <h2 class="text-2xl font-bold text-gray-800 mb-2">Setting Akun</h2>
    <p class="text-gray-600">Kelola informasi akun dan keamanan Anda.</p>
</div>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
    <!-- Profile Settings -->
    <div class="bg-white rounded-lg shadow-sm border border-gray-200">
        <div class="px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                <i class="fas fa-user mr-2 text-blue-primary"></i>
                Informasi Profil
            </h3>
        </div>
        
        <form method="POST" class="p-6 space-y-4">
            <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
            <input type="hidden" name="action" value="update_profile">
            
            <div>
                <label for="username_display" class="block text-sm font-medium text-gray-700 mb-1">Username</label>
                <input type="text" id="username_display" value="<?php echo htmlspecialchars($current_user['username']); ?>" 
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg bg-gray-100" disabled>
                <p class="text-xs text-gray-500 mt-1">Username tidak dapat diubah</p>
            </div>
            
            <div>
                <label for="full_name" class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap</label>
                <input type="text" id="full_name" name="full_name" 
                       value="<?php echo htmlspecialchars($current_user['full_name']); ?>" required
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
            </div>
            
            <div>
                <label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
                <input type="email" id="email" name="email" 
                       value="<?php echo htmlspecialchars($current_user['email']); ?>" required
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Role</label>
                <div class="flex items-center">
                    <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-sm font-medium">
                        <i class="fas fa-user-tie mr-1"></i>
                        <?php echo ucfirst($current_user['role']); ?>
                    </span>
                </div>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Bergabung Sejak</label>
                <p class="text-sm text-gray-600"><?php echo formatDate($current_user['created_at']); ?></p>
            </div>
            
            <button type="submit" 
                    class="w-full bg-blue-primary hover:bg-blue-dark text-white py-2 px-4 rounded-lg transition duration-200">
                <i class="fas fa-save mr-2"></i>
                Simpan Perubahan
            </button>
        </form>
    </div>
    
    <!-- Security Settings -->
    <div class="bg-white rounded-lg shadow-sm border border-gray-200">
        <div class="px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                <i class="fas fa-shield-alt mr-2 text-green-600"></i>
                Keamanan Akun
            </h3>
        </div>
        
        <div class="p-6">
            <!-- Change Password -->
            <div class="mb-6">
                <h4 class="text-md font-semibold text-gray-800 mb-3">Ubah Password</h4>
                
                <form method="POST" class="space-y-4">
                    <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                    <input type="hidden" name="action" value="change_password">
                    
                    <div>
                        <label for="current_password" class="block text-sm font-medium text-gray-700 mb-1">Password Saat Ini</label>
                        <input type="password" id="current_password" name="current_password" required
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
                    </div>
                    
                    <div>
                        <label for="new_password" class="block text-sm font-medium text-gray-700 mb-1">Password Baru</label>
                        <input type="password" id="new_password" name="new_password" required minlength="6"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
                        <p class="text-xs text-gray-500 mt-1">Minimal 6 karakter</p>
                    </div>
                    
                    <div>
                        <label for="confirm_password" class="block text-sm font-medium text-gray-700 mb-1">Konfirmasi Password Baru</label>
                        <input type="password" id="confirm_password" name="confirm_password" required minlength="6"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-primary focus:border-transparent">
                    </div>
                    
                    <button type="submit" 
                            class="w-full bg-green-600 hover:bg-green-700 text-white py-2 px-4 rounded-lg transition duration-200">
                        <i class="fas fa-key mr-2"></i>
                        Ubah Password
                    </button>
                </form>
            </div>
            
            <!-- Account Status -->
            <div class="border-t border-gray-200 pt-6">
                <h4 class="text-md font-semibold text-gray-800 mb-3">Status Akun</h4>
                
                <div class="space-y-3">
                    <div class="flex items-center justify-between">
                        <span class="text-sm text-gray-600">Status:</span>
                        <span class="<?php echo $current_user['status'] === 'active' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?> px-2 py-1 rounded-full text-xs font-medium">
                            <?php echo ucfirst($current_user['status']); ?>
                        </span>
                    </div>
                    
                    <div class="flex items-center justify-between">
                        <span class="text-sm text-gray-600">Terakhir Login:</span>
                        <span class="text-sm text-gray-800">
                            <?php echo isset($current_user['last_login']) && $current_user['last_login'] ? formatDate($current_user['last_login']) : 'Belum pernah'; ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Account Statistics -->
<div class="mt-6 bg-white rounded-lg shadow-sm border border-gray-200">
    <div class="px-6 py-4 border-b border-gray-200">
        <h3 class="text-lg font-semibold text-gray-800 flex items-center">
            <i class="fas fa-chart-bar mr-2 text-purple-600"></i>
            Statistik Akun
        </h3>
    </div>
    
    <div class="p-6">
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
            <div class="text-center">
                <div class="bg-blue-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-users text-blue-primary text-2xl"></i>
                </div>
                <h4 class="text-2xl font-bold text-gray-800"><?php echo $stats['total_users']; ?></h4>
                <p class="text-sm text-gray-600">Total User Dibuat</p>
            </div>
            
            <div class="text-center">
                <div class="bg-green-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-user-check text-green-600 text-2xl"></i>
                </div>
                <h4 class="text-2xl font-bold text-gray-800"><?php echo $stats['active_users']; ?></h4>
                <p class="text-sm text-gray-600">User Aktif</p>
            </div>
            
            <div class="text-center">
                <div class="bg-purple-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-key text-purple-600 text-2xl"></i>
                </div>
                <h4 class="text-2xl font-bold text-gray-800"><?php echo $stats['total_access_granted']; ?></h4>
                <p class="text-sm text-gray-600">Akses Produk Diberikan</p>
            </div>
        </div>
    </div>
</div>

<!-- Tips & Information -->
<div class="mt-6 bg-blue-50 border border-blue-200 rounded-lg p-6">
    <div class="flex items-start">
        <div class="bg-blue-primary w-10 h-10 rounded-lg flex items-center justify-center mr-4 flex-shrink-0">
            <i class="fas fa-lightbulb text-white"></i>
        </div>
        <div>
            <h4 class="text-lg font-semibold text-blue-800 mb-2">Tips Keamanan</h4>
            <ul class="text-sm text-blue-700 space-y-1">
                <li class="flex items-start">
                    <i class="fas fa-check text-blue-600 mr-2 mt-0.5 flex-shrink-0"></i>
                    Gunakan password yang kuat dengan kombinasi huruf, angka, dan simbol
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-blue-600 mr-2 mt-0.5 flex-shrink-0"></i>
                    Jangan bagikan informasi login Anda kepada orang lain
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-blue-600 mr-2 mt-0.5 flex-shrink-0"></i>
                    Logout dari akun setelah selesai menggunakan, terutama di komputer umum
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-blue-600 mr-2 mt-0.5 flex-shrink-0"></i>
                    Perbarui password secara berkala untuk menjaga keamanan akun
                </li>
            </ul>
        </div>
    </div>
</div>

<script>
// Password confirmation validation
document.getElementById('confirm_password').addEventListener('input', function() {
    const newPassword = document.getElementById('new_password').value;
    const confirmPassword = this.value;
    
    if (newPassword !== confirmPassword) {
        this.setCustomValidity('Password tidak cocok');
    } else {
        this.setCustomValidity('');
    }
});

document.getElementById('new_password').addEventListener('input', function() {
    const confirmPassword = document.getElementById('confirm_password');
    if (confirmPassword.value) {
        confirmPassword.dispatchEvent(new Event('input'));
    }
});
</script>