<?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.username, u.email, u.phone,
                 e.address
          FROM users u
          LEFT JOIN employers e ON u.user_id = e.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">
    <?php include 'navbar.php' ?>

    <div class="container mx-auto px-4 py-8">
        <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">Employer Profile</h2>
                
                <form method="POST" action="updateemployeeprofile.php" enctype="multipart/form-data" class="space-y-6">
                    <!-- 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']) ? $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']) ? $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']) ? $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']) ? $user['username'] : ''; ?>" 
                            class="input input-bordered w-full" 
                            required
                        >
                    </div>

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

                    <!-- Company Address -->
                    <div class="form-control">
                        <label class="label">
                            <span class="label-text"> Address</span>
                        </label>
                        <input 
                            type="text" 
                            name="address" 
                            value="<?php echo isset($user['address']) ? $user['address'] : ''; ?>" 
                            class="input input-bordered w-full" 
                            required
                        >
                    </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'?>
</body>
</html>