<?php


session_start(); 
    $u_uname = '';
    if (isset($_COOKIE['auth_token']))
    {
        $u_uname = $_COOKIE['auth_token'];
    }else{
        header("location:../login.php");
    }
    
include '../include/connection.php';

// Check if all required parameters are set
if (isset($_POST['name'], $_POST['email'], $_POST['mobile'], $_POST['address'])) {
    // Assign values to variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $address = $_POST['address'];

    // Handle file uploads
    $resumePath = '';
    $photoPath = '';

    if (isset($_FILES['resume']['tmp_name']) && isset($_FILES['photo']['tmp_name'])) {
        $uploadDir = '../uploads/';

        $resumeName = $_FILES['resume']['name'];
        $resumeTmpName = $_FILES['resume']['tmp_name'];
        $resumePath = $uploadDir . $resumeName;
        move_uploaded_file($resumeTmpName, $resumePath);

        $photoName = $_FILES['photo']['name'];
        $photoTmpName = $_FILES['photo']['tmp_name'];
        $photoPath = $uploadDir . $photoName;
        move_uploaded_file($photoTmpName, $photoPath);
    } else {
        echo "Error: Missing file uploads";
        exit();
    }

    // SQL query with prepared statement
    $sql = "INSERT INTO `upcomingStaff`(`fullName`, `mobile`, `email`, `address`, `resume`, `pass_size_photo`) 
            VALUES (?, ?, ?, ?, ?, ?)";

    // Prepare the statement
    $stmt = $link->prepare($sql);

    // Bind parameters
    $stmt->bind_param("ssssss", $name, $mobile, $email, $address, $resumePath, $photoPath);

    // Execute the statement
    if ($stmt->execute()) {
        header('Location:../addupcomingstaff.php?status=1');
    } else {
        echo "Error: Unable to execute the query";
    }

    // Close the statement and connection
    $stmt->close();
    $link->close();
} else {
    // Handle case where parameters are missing
    echo "Error: Missing parameters";
}
?>
