<?php
include 'includes/db.php';
include 'includes/header.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $phone = $conn->real_escape_string($_POST['phone']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    // Update Basic Info
    $sql_update = "UPDATE users SET full_name='$full_name', phone='$phone' WHERE id=$user_id";
    if ($conn->query($sql_update) === TRUE) {
        $_SESSION['full_name'] = $full_name; // Update session
        $message = "Profile updated successfully!";
    } else {
        $message = "Error updating profile: " . $conn->error;
    }

    // Update Password if provided
    if (!empty($password)) {
        if ($password === $confirm_password) {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $sql_pass = "UPDATE users SET password='$hashed_password' WHERE id=$user_id";
            if ($conn->query($sql_pass) === TRUE) {
                $message .= " Password changed successfully!";
            } else {
                $message .= " Error changing password.";
            }
        } else {
            $message .= " Passwords do not match.";
        }
    }
}

// Fetch current user data
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = $conn->query($sql);
$user = $result->fetch_assoc();
?>

<div class="dashboard-container">
    <div class="sidebar">
        <ul class="sidebar-menu">
            <li><a href="dashboard.php"><i class="fas fa-tachometer-alt" style="margin-right: 10px;"></i> Dashboard</a></li>
            <li><a href="application.php"><i class="fas fa-edit" style="margin-right: 10px;"></i> New Application</a></li>
            <li><a href="profile.php" class="active"><i class="fas fa-user" style="margin-right: 10px;"></i> My Profile</a></li>
            <li><a href="logout.php"><i class="fas fa-sign-out-alt" style="margin-right: 10px;"></i> Logout</a></li>
        </ul>
    </div>

    <div class="main-content">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">My Profile</h3>
            </div>
            
            <?php if($message): ?>
                <div class="alert alert-info"><?php echo $message; ?></div>
            <?php endif; ?>

            <form method="POST" action="">
                <div class="form-group">
                    <label>Email (Cannot be changed)</label>
                    <input type="email" class="form-control" value="<?php echo $user['email']; ?>" disabled>
                </div>
                <div class="form-group">
                    <label>CNIC (Cannot be changed)</label>
                    <input type="text" class="form-control" value="<?php echo $user['cnic']; ?>" disabled>
                </div>
                
                <div class="form-group">
                    <label>Full Name</label>
                    <input type="text" name="full_name" class="form-control" value="<?php echo $user['full_name']; ?>" required>
                </div>
                <div class="form-group">
                    <label>Phone Number</label>
                    <input type="text" name="phone" class="form-control" value="<?php echo $user['phone']; ?>" required>
                </div>
                
                <hr style="margin: 30px 0; border: 0; border-top: 1px solid #eee;">
                
                <h4 style="margin-bottom: 20px;">Change Password <small style="color: #999; font-weight: normal;">(Leave blank to keep current)</small></h4>
                <div class="form-group">
                    <label>New Password</label>
                    <input type="password" name="password" class="form-control">
                </div>
                <div class="form-group">
                    <label>Confirm New Password</label>
                    <input type="password" name="confirm_password" class="form-control">
                </div>

                <div style="margin-top: 30px;">
                    <button type="submit" class="btn-primary" style="padding: 10px 30px;">Update Profile</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
