<?php
session_start(); // Start the session

// Include your database connection
include 'connect.php';


// Ensure the user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php"); // Redirect to login if the user is not logged in
    exit();
}

$user_id = $_SESSION['user_id']; // Get user ID from the session


// Fetch user data from the database
$query = "SELECT u.firstname, u.lastname, u.email, u.phone,u.username,
                 j.skills, j.experience, j.education, j.location
          FROM users u
          LEFT JOIN job_seekers j ON u.user_id = j.user_id
          WHERE u.user_id = ?";


$stmt = $conn->prepare($query);
if (!$stmt) {
    die("Prepare failed: " . $conn->error);
}
$stmt->bind_param("i", $user_id);
if (!$stmt->execute()) {
    die("Query execution failed: " . $stmt->error);
}
$result = $stmt->get_result();

// Check if the user data was found
if ($result->num_rows > 0) {
    $user = $result->fetch_assoc();

} else {

    $user = []; // Set $user to an empty array to avoid errors
}
?>
<!DOCTYPE html>
<html lang="en" data-theme="light">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Update Profile</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/daisyui/2.6.0/full.css" rel="stylesheet">
</head>

<body class="min-h-screen bg-base-200 flex flex-col">
    <?php include 'navbar.php' ?>

    <div class="container mx-auto px-4 py-8 flex-grow">
        <div class="card max-w-2xl mx-auto bg-base-100 shadow-xl">
            <div class="card-body">
                <h2 class="card-title text-2xl font-bold text-center mb-6">Jobseeker Profile</h2>

                <form method="POST" action="updateprofile.php" enctype="multipart/form-data" class="space-y-6" onsubmit="return validateForm();" >
                    <!-- First Name -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">First Name</span>
                        </label>
                        <input type="text" name="first_name"
                            value="<?php echo isset($user['firstname']) ? htmlspecialchars($user['firstname']) : ''; ?>"
                            class="input input-bordered w-full" required>
                    </div>

                    <!-- Last Name -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Last Name</span>
                        </label>
                        <input type="text" name="last_name"
                            value="<?php echo isset($user['lastname']) ? htmlspecialchars($user['lastname']) : ''; ?>"
                            class="input input-bordered w-full" required>
                    </div>

                    <!-- Email -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Email</span>
                        </label>
                        <input type="email" name="email"
                            value="<?php echo isset($user['email']) ? htmlspecialchars($user['email']) : ''; ?>"
                            class="input input-bordered w-full" required>
                    </div>

                    <!-- username -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">username</span>
                        </label>
                        <input type="text" name="username"
                            value="<?php echo isset($user['username']) ? htmlspecialchars($user['username']) : ''; ?>"
                            class="input input-bordered w-full" required>
                    </div>

                    <!-- Phone -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Phone Number</span>
                        </label>
                        <input type="text" name="phone" id="phone"
                            value="<?php echo isset($user['phone']) ? htmlspecialchars($user['phone']) : ''; ?>"
                            class="input input-bordered w-full" required>
                        <small id="contact-error" class="text-error"></small>
                    </div>

                    <!-- Location -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Location</span>
                        </label>
                        <input type="text" name="location"
                            value="<?php echo isset($user['location']) ? htmlspecialchars($user['location']) : ''; ?>"
                            class="input input-bordered w-full" required>
                    </div>

                    <!-- Skills -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Skills</span>
                        </label>
                        <textarea name="skills" class="textarea textarea-bordered h-24"
                            required><?php echo isset($user['skills']) ? htmlspecialchars($user['skills']) : ''; ?></textarea>
                    </div>

                    <!-- Experience -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Experience</span>
                        </label>
                        <textarea name="experience" class="textarea textarea-bordered h-24"
                            required><?php echo isset($user['experience']) ? htmlspecialchars($user['experience']) : ''; ?></textarea>
                    </div>

                    <!-- Education -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text">Education</span>
                        </label>
                        <textarea name="education" class="textarea textarea-bordered h-24"
                            required><?php echo isset($user['education']) ? htmlspecialchars($user['education']) : ''; ?></textarea>
                    </div>

                    <!-- Submit Button -->
                    <div class="form-control mt-8">
                        <button type="submit" name="update" class="btn btn-primary w-full">Update Profile</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php include 'footer.php' ?>

<script src="phonevalidation.js"></script>

</body>

</html>