<?php
session_start();
include "db.php";

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

$user_id = $_SESSION['user_id'];

// Update profile
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $first_name = $_POST['first_name'];
    $middle_name = $_POST['middle_name'];
    $last_name = $_POST['last_name'];
    $dob = $_POST['dob'];
    $qualification = $_POST['qualification'];
    $address = $_POST['address'];
    $mobile = $_POST['mobile'];

    $resume = null;
    if (!empty($_FILES['resume']['name'])) {
        $resume = "uploads/" . time() . "_" . basename($_FILES['resume']['name']);
        move_uploaded_file($_FILES['resume']['tmp_name'], $resume);
        $conn->query("UPDATE users SET resume='$resume' WHERE id=$user_id");
    }

    $sql = "UPDATE users SET first_name='$first_name', middle_name='$middle_name', last_name='$last_name', date_of_birth='$dob', highest_qualification='$qualification', address='$address', mobile_number='$mobile' WHERE id=$user_id";

    if ($conn->query($sql)) {
        echo "Profile updated successfully.";
    } else {
        echo "Error: " . $conn->error;
    }
}

$user = $conn->query("SELECT * FROM users WHERE id=$user_id")->fetch_assoc();
?>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <style>
.btn {
  display: inline-block;
  padding: 10px 20px;
  background-color: #007bff; /* Blue */
  color: #fff;
  text-decoration: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: bold;
  transition: background 0.3s;
}

.btn:hover {
  background-color: #0056b3; /* Darker blue */
}
</style>
<body>
<div class="container mt-5">
  <div class="card shadow-lg p-4 rounded-4">
    <h2 class="text-primary mb-4">Edit Profile</h2>
    <form method="POST" enctype="multipart/form-data" class="row g-3">
      <div class="col-md-4"><label class="form-label">First Name</label>
        <input type="text" name="first_name" value="<?php echo $user['first_name']; ?>" class="form-control">
      </div>
      <div class="col-md-4"><label class="form-label">Middle Name</label>
        <input type="text" name="middle_name" value="<?php echo $user['middle_name']; ?>" class="form-control">
      </div>
      <div class="col-md-4"><label class="form-label">Last Name</label>
        <input type="text" name="last_name" value="<?php echo $user['last_name']; ?>" class="form-control">
      </div>
      <div class="col-md-6"><label class="form-label">Date of Birth</label>
        <input type="date" name="dob" value="<?php echo $user['date_of_birth']; ?>" class="form-control">
      </div>
      <div class="col-md-6"><label class="form-label">Qualification</label>
        <input type="text" name="qualification" value="<?php echo $user['highest_qualification']; ?>" class="form-control">
      </div>
      <div class="col-12"><label class="form-label">Address</label>
        <textarea name="address" class="form-control"><?php echo $user['address']; ?></textarea>
      </div>
      <div class="col-md-6"><label class="form-label">Mobile</label>
        <input type="text" name="mobile" value="<?php echo $user['mobile_number']; ?>" class="form-control">
      </div>
      <div class="col-md-6"><label class="form-label">Resume</label>
        <?php if($user['resume']){ ?><a href="<?php echo $user['resume']; ?>" class="btn btn-link" target="_blank">View</a><?php } ?>
        <input type="file" name="resume" class="form-control">
      </div>
      <div class="col-12 text-center">
        <button type="submit" class="btn btn-warning px-5">Update Profile</button>
      </div>
    </form>
  </div>
</div>

<button onclick="window.location.href='dashboard.php'">Back to Dashboard</button>

</body>
</html>