<?php
include('connection.php');


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
    $id = $_POST['id'];
    $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
    $marital_status = $_POST['marital_status'];
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $number = mysqli_real_escape_string($conn, $_POST['number']);
    $profile = mysqli_real_escape_string($conn, $_POST['profile']);
    $experience = mysqli_real_escape_string($conn, $_POST['experience']);
    $location = mysqli_real_escape_string($conn, $_POST['location']);
    $salary_expectations = $_POST['salary_expectations'];
    $linkedin = mysqli_real_escape_string($conn, $_POST['linkedin_username']);
    $whatsapp = mysqli_real_escape_string($conn, $_POST['whatsapp_username']);
  
    
    $linkedin_url = "https://www.linkedin.com/in/";
    $whatsapp_url = "https://wa.me/+";
   

    $existing_resume = ''; 
    $query = "SELECT resume FROM add_resume WHERE id = $id";
    $result = mysqli_query($conn, $query);
    if ($row = mysqli_fetch_assoc($result)) {
        $existing_resume = $row['resume'];
    }

   
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
        $fileExt = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $uploadPath = "uploads/" . $_FILES['file']['name'];

        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadPath)) {
        

            $resume_file = $_FILES['file']['name']; 
        } else {
           
            echo 'Error uploading file.';
            exit(); 
        }
    } else {
     

        $resume_file = $existing_resume; 
    }

   
    $sql = "UPDATE add_resume SET 
            full_name = '$first_name',
            marital_status='$marital_status' ,
            email = '$email',
            number = '$number',
            work_profile = '$profile',
            work_experience = '$experience',
            location = '$location',
            salary_expectations ='$salary_expectations',
            linkedin = '$linkedin_url$linkedin',
            whatsapp_username = '$whatsapp_url$whatsapp',
            resume = '$resume_file' 
            WHERE id = $id"; 

  
    if (mysqli_query($conn, $sql)) {
        header("Location: resume-view-all.php?success=Resume Updated Successfully!!!");
        exit(); 
    } else {

        echo 'error: ' . mysqli_error($conn);
    }
} else {

 
    header("Location: your_form_page.php");
    exit();
}
?>