
<?php
// Get user information
if (isAdmin()) {
    $userId = $_SESSION['user_id'];
    $stmt = $pdo->prepare("SELECT * FROM admins WHERE id = ?");
    $stmt->execute([$userId]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    $userType = 'Admin';
} else {
    $userId = $_SESSION['user_id'];
    $stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?");
    $stmt->execute([$userId]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    $userType = 'Student';
}
?>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                Profile - <?php echo $userType; ?>
            </div>
            <div class="card-body">
                <?php if ($userType === 'Admin'): ?>
                    <form method="post" action="">
                        <input type="hidden" name="action" value="update_profile">
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="username" class="form-label">Username</label>
                                <input type="text" class="form-control" id="username" name="username" value="<?php echo $user['username']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="email" class="form-label">Email</label>
                                <input type="email" class="form-control" id="email" name="email" value="<?php echo $user['email']; ?>" required>
                            </div>
                            <div class="col-md-12 mb-3">
                                <label for="full_name" class="form-label">Full Name</label>
                                <input type="text" class="form-control" id="full_name" name="full_name" value="<?php echo $user['full_name']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="password" class="form-label">New Password (leave blank to keep current)</label>
                                <input type="password" class="form-control" id="password" name="password">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="confirm_password" class="form-label">Confirm New Password</label>
                                <input type="password" class="form-control" id="confirm_password" name="confirm_password">
                            </div>
                        </div>
                        
                        <button type="submit" class="btn btn-primary">Update Profile</button>
                    </form>
                <?php else: ?>
                    <form method="post" action="">
                        <input type="hidden" name="action" value="update_profile">
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="surname" class="form-label">Surname</label>
                                <input type="text" class="form-control" id="surname" name="surname" value="<?php echo $user['surname']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="first_name" class="form-label">First Name</label>
                                <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo $user['first_name']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="other_name" class="form-label">Other Name</label>
                                <input type="text" class="form-control" id="other_name" name="other_name" value="<?php echo $user['other_name']; ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="email" class="form-label">Email</label>
                                <input type="email" class="form-control" id="email" name="email" value="<?php echo $user['email']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="mobile_phone" class="form-label">Mobile Phone</label>
                                <input type="tel" class="form-control" id="mobile_phone" name="mobile_phone" value="<?php echo $user['mobile_phone']; ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="residential_phone" class="form-label">Residential Phone</label>
                                <input type="tel" class="form-control" id="residential_phone" name="residential_phone" value="<?php echo $user['residential_phone']; ?>">
                            </div>
                            <div class="col-md-12 mb-3">
                                <label for="permanent_address" class="form-label">Permanent Address</label>
                                <textarea class="form-control" id="permanent_address" name="permanent_address" rows="3" required><?php echo $user['permanent_address']; ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="password" class="form-label">New Password (leave blank to keep current)</label>
                                <input type="password" class="form-control" id="password" name="password">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="confirm_password" class="form-label">Confirm New Password</label>
                                <input type="password" class="form-control" id="confirm_password" name="confirm_password">
                            </div>
                        </div>
                        
                        <button type="submit" class="btn btn-primary">Update Profile</button>
                    </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
