<?php
// member/profile.php - User Profile Management
require_once '../config.php';
requireLogin();

$userId = $_SESSION['user_id'];
$user = getUserById($userId);
$message = '';
$error = '';

// Handle profile update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['update_profile'])) {
        $fullName = trim($_POST['full_name'] ?? '');
        $phone = trim($_POST['phone'] ?? '');
        $email = trim($_POST['email'] ?? '');
        
        if (updateUserProfile($userId, ['full_name' => $fullName, 'phone' => $phone, 'email' => $email])) {
            $message = 'Profile updated successfully';
            $user = getUserById($userId);
        } else {
            $error = 'Failed to update profile';
        }
    } elseif (isset($_POST['change_password'])) {
        $current = $_POST['current_password'] ?? '';
        $new = $_POST['new_password'] ?? '';
        $confirm = $_POST['confirm_password'] ?? '';
        
        if ($new !== $confirm) {
            $error = 'New passwords do not match';
        } elseif (strlen($new) < 8) {
            $error = 'Password must be at least 8 characters';
        } else {
            $result = changePassword($userId, $current, $new);
            if ($result['success']) {
                $message = $result['message'];
            } else {
                $error = $result['message'];
            }
        }
    }
}

$theme = getTheme();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profile - Soul Guard</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            transition: all 0.3s ease;
        }
        
        body.dark {
            background: #0a0a0f;
            color: #e4e4e7;
        }
        
        body.light {
            background: #f5f5f5;
            color: #1a1a2e;
        }
        
        .navbar {
            background: rgba(19, 19, 26, 0.95);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid rgba(255, 255, 255, 0.06);
            padding: 15px 30px;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        body.light .navbar {
            background: rgba(255, 255, 255, 0.95);
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }
        
        .nav-container {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .logo h2 {
            background: linear-gradient(135deg, #06b6d4, #14b8a6);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 24px;
        }
        
        .nav-links {
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
        }
        
        .nav-links a {
            color: #a1a1aa;
            text-decoration: none;
            padding: 8px 14px;
            border-radius: 8px;
            transition: all 0.2s;
        }
        
        .nav-links a:hover, .nav-links a.active {
            background: rgba(6, 182, 212, 0.1);
            color: #06b6d4;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        
        .card {
            background: rgba(255, 255, 255, 0.03);
            border: 1px solid rgba(255, 255, 255, 0.06);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
        }
        
        body.light .card {
            background: #fff;
            border: 1px solid #e0e0e0;
        }
        
        .card h3 {
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(6, 182, 212, 0.3);
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
        }
        
        input {
            width: 100%;
            padding: 12px 16px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            color: inherit;
            font-size: 14px;
        }
        
        body.light input {
            background: #f9f9f9;
            border: 1px solid #ddd;
            color: #1a1a2e;
        }
        
        input:focus {
            outline: none;
            border-color: #06b6d4;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: linear-gradient(135deg, #06b6d4, #14b8a6);
            color: white;
            text-decoration: none;
            border-radius: 10px;
            border: none;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
        }
        
        .btn-secondary {
            background: rgba(255, 255, 255, 0.1);
        }
        
        body.light .btn-secondary {
            background: #e0e0e0;
            color: #1a1a2e;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .message {
            background: rgba(34, 197, 94, 0.2);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #22c55e;
            padding: 12px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        
        .error {
            background: rgba(239, 68, 68, 0.2);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #ef4444;
            padding: 12px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        
        .theme-toggle {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 18px;
        }
        
        .avatar {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #06b6d4, #14b8a6);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 32px;
            margin-bottom: 20px;
        }
        
        @media (max-width: 600px) {
            .container {
                padding: 20px;
            }
            .card {
                padding: 20px;
            }
        }
    </style>
</head>
<body class="<?php echo $theme; ?>">
    <nav class="navbar">
        <div class="nav-container">
            <div class="logo">
                <h2>🛡️ Soul Guard</h2>
            </div>
            <div class="nav-links">
                <a href="dashboard.php">Dashboard</a>
                <a href="profile.php" class="active">Profile</a>
                <a href="organization.php">Organization</a>
                <a href="reports.php">Reports</a>
                <a href="logout.php">Logout</a>
                <div class="theme-toggle" onclick="toggleTheme()">
                    <?php echo $theme === 'dark' ? '☀️' : '🌙'; ?>
                </div>
            </div>
        </div>
    </nav>
    
    <div class="container">
        <?php if ($message): ?>
            <div class="message"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        
        <div class="card">
            <div class="avatar">
                <?php echo substr($_SESSION['full_name'] ?: $_SESSION['username'], 0, 1); ?>
            </div>
            
            <h3>👤 Profile Information</h3>
            <form method="POST">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" value="<?php echo htmlspecialchars($user['username']); ?>" disabled>
                </div>
                
                <div class="form-group">
                    <label>Full Name</label>
                    <input type="text" name="full_name" value="<?php echo htmlspecialchars($user['full_name']); ?>">
                </div>
                
                <div class="form-group">
                    <label>Email Address</label>
                    <input type="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>">
                </div>
                
                <div class="form-group">
                    <label>Phone Number</label>
                    <input type="tel" name="phone" value="<?php echo htmlspecialchars($user['phone']); ?>">
                </div>
                
                <button type="submit" name="update_profile" class="btn">Update Profile</button>
            </form>
        </div>
        
        <div class="card">
            <h3>🔐 Change Password</h3>
            <form method="POST">
                <div class="form-group">
                    <label>Current Password</label>
                    <input type="password" name="current_password" required>
                </div>
                
                <div class="form-group">
                    <label>New Password</label>
                    <input type="password" name="new_password" required>
                    <small style="opacity: 0.7;">Minimum 8 characters with uppercase, lowercase, and numbers</small>
                </div>
                
                <div class="form-group">
                    <label>Confirm New Password</label>
                    <input type="password" name="confirm_password" required>
                </div>
                
                <button type="submit" name="change_password" class="btn">Change Password</button>
            </form>
        </div>
        
        <div class="card">
            <h3>ℹ️ Account Information</h3>
            <table style="width: 100%;">
                <tr><th>Member Since</th><td><?php echo date('F j, Y', strtotime($user['created_at'])); ?></td></tr>
                <tr><th>Last Login</th><td><?php echo $user['last_login'] ? date('F j, Y g:i A', strtotime($user['last_login'])) : 'Never'; ?></td></tr>
                <tr><th>Status</th><td><?php echo $user['is_active'] ? '<span style="color:#22c55e">✓ Active</span>' : '<span style="color:#ef4444">✗ Inactive</span>'; ?></td></tr>
            </table>
        </div>
    </div>
    
    <script>
        function toggleTheme() {
            const body = document.body;
            const isDark = body.classList.contains('dark');
            const newTheme = isDark ? 'light' : 'dark';
            
            body.classList.remove('dark', 'light');
            body.classList.add(newTheme);
            
            document.cookie = `soulguard_theme=${newTheme}; path=/; max-age=31536000`;
            
            location.reload();
        }
    </script>
</body>
</html>