<?php


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

// Function to check if an email is already present in the database
function isEmailUnique($link, $email) {
    $query = "SELECT COUNT(*) as count FROM employee WHERE empEmail = '$email'";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_assoc($result);
    header("Location: ../addemployee.php?status=5");
    exit(0);
}

// Function to check if a mobile number is already present in the database
function isMobileNumberUnique($link, $mobileNumber) {
    $query = "SELECT COUNT(*) as count FROM employee WHERE empPhone = '$mobileNumber'";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_assoc($result);
    header("Location: ../addemployee.php?status=5");
    exit(0);
}

// Define all required parameters
$requiredParameters = array(
    'empName', 'empAddr', 'empPhone', 'empPhoneAlt', 'empEmail', 'empAadhar',
    'empPAN', 'empDOB', 'empMarital', 'empEmName', 'empEmAddr', 'empEmPhone',
    'empEmPhoneAlt', 'empEmRel', 'salary', 'empTitle', 'empPrevId', 'empManager',
    'empDept', 'empLocation', 'empHREmail', 'empHRPhone', 'empStartDate', 'empEndDate'
);

// Check if all required parameters are set
foreach ($requiredParameters as $parameter) {
    if (!isset($_POST[$parameter])) {
        echo "Missing parameter: $parameter";
        exit;
    }
}

// Extract POST data
extract($_POST);

// Insert into employee table
if ($empMarital == 'married') {
    // Check for additional parameters if marital status is 'married'
    $requiredSpouseParameters = array('empSpouse', 'empSpousePhone', 'empNoOfChildren');
    foreach ($requiredSpouseParameters as $parameter) {
        if (!isset($_POST[$parameter])) {
            echo "Missing parameter: $parameter";
            exit;
        }
    }

    // Build SQL query for married employees
    $sql = "INSERT INTO `employee` (`empName`, `empAddr`, `empPhone`, `empPhoneAlt`, `empEmail`, `empAadhar`, `empPAN`, `empDOB`, `empMarital`, `empSpouse`, `empSpousePhone`, `empNofChildren`, `empEmName`, `empEmAddr`, `empEmPhone`, `empEmPhoneAlt`, `empEmRel`) 
                            VALUES ('$empName', '$empAddr', '$empPhone', '$empPhoneAlt', '$empEmail', '$empAadhar', '$empPAN', '$empDOB', '$empMarital', '$empSpouse', '$empSpousePhone', $empNoOfChildren, '$empEmName', '$empEmAddr', '$empEmPhone', '$empEmPhoneAlt', '$empEmRel')";

} else {
    // Build SQL query for unmarried employees
    $sql = "INSERT INTO `employee` (`empName`, `empAddr`, `empPhone`, `empPhoneAlt`, `empEmail`, `empAadhar`, `empPAN`, `empDOB`, `empMarital`, `empEmName`, `empEmAddr`, `empEmPhone`, `empEmPhoneAlt`, `empEmRel`) 
                               VALUES ('$empName', '$empAddr', '$empPhone', '$empPhoneAlt', '$empEmail', '$empAadhar', '$empPAN', '$empDOB', '$empMarital', '$empEmName', '$empEmAddr', '$empEmPhone', '$empEmPhoneAlt', '$empEmRel')";
}

// Execute the main SQL query
$result = $link->query($sql);
if (!$result) {
    echo "Error: " . $link->error;
    exit; // Terminate script execution if SQL query fails
}

// Retrieve the employee ID of the newly inserted record
$record = mysqli_query($link, "SELECT * FROM `employee` WHERE empId = (SELECT MAX(empId) FROM `employee`)");
while ($row = mysqli_fetch_array($record)) {
    $empId = $row['empId'];
}

// Insert previous employment details
foreach ($empTitle as $index => $titles) {
    $s_empTitle = $titles;
    $s_empPrevId = $empPrevId[$index];
    $s_empManager = $empManager[$index];
    $s_empDept = $empDept[$index];
    $s_empLocation = $empLocation[$index];
    $s_empHREmail = $empHREmail[$index];
    $s_empHRPhone = $empHRPhone[$index];
    $s_empStartDate = $empStartDate[$index];
    $s_empEndDate = $empEndDate[$index];

    $query = "INSERT INTO `previousEmployment` (`empId`, `empTitle`, `empPrevId`, `empManager`, `empDept`, `empLocation`, `empHREmail`, `empHRPhone`, `empStartDate`, `empEndDate`)  
                VALUES ($empId, '$s_empTitle', $s_empPrevId, '$s_empManager', '$s_empDept', '$s_empLocation', '$s_empHREmail', $s_empHRPhone, '$s_empStartDate', '$s_empEndDate')";
    $query_run = mysqli_query($link, $query);
}

// Check if the previous employment details were inserted successfully
if ($query_run) {
    // Redirect to the page showing all employees
    header("Location: ../allemployees.php");
    exit(0);
} else {
    echo "Error: Unable to complete the operation";
    exit; // Terminate script execution if operation fails
}
?>
