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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve data from the form
    $name = $_POST["name"];
    $employeeId = $_POST["employee_id"];
    $number1 = $_POST["number1"];
    $number2 = $_POST["number2"];
    $email1 = $_POST["email1"];
    $email2 = $_POST["email2"];
    $designation = $_POST["designation"];
    $salary = $_POST["salary"];
    $joiningDate = $_POST["joining_date"];
    $previousCompany = $_POST["previous_company"];
    $companyWebsite = $_POST["company_website"];
    $contactPerson = $_POST["contact_person"];
    $contactNumber = $_POST["contact_number"];
    $lastSalary = $_POST["last_salary"];
    $bankName = $_POST["bank_name"];
    $accountName = $_POST["account_name"];
    $accountNumber = $_POST["account_number"];
    $ifscCode = $_POST["ifsc_code"];
    $password = $_POST["password"];
    $facebook_username = mysqli_real_escape_string($conn, $_POST['facebook_username']);
    $twitter_username = mysqli_real_escape_string($conn, $_POST['twitter_username']);
    $instagram_username = mysqli_real_escape_string($conn, $_POST['instagram_username']);
    $linkedin_username = mysqli_real_escape_string($conn, $_POST['linkedin_username']);
    $currentDateTime = date('Y-m-d H:i:s');
    
    // Handle profile picture upload
    $targetDir = "uploads/"; // Directory where you want to store uploaded images
    $profilePicture = basename($_FILES["file"]["name"]);
    $targetFilePath = $targetDir . $profilePicture;

    // Check if everything is okay and move the file
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)) {
        echo "The file " . htmlspecialchars($profilePicture) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

    // SQL query to insert data into the employee table (modify this based on your table structure)
    $sql = "INSERT INTO add_employee (name, employee_id, number1, number2, email1, email2, designation, salary, joining_date, previous_company, company_website, contact_person, contact_number, last_salary, bank_name, account_holder_name, account_number, ifsc_code, facebook, twitter, instagram, linkedin, profile_picture,password,datetime) 
            VALUES ('$name', '$employeeId', '$number1', '$number2', '$email1', '$email2', '$designation', '$salary', '$joiningDate', '$previousCompany', '$companyWebsite', '$contactPerson', '$contactNumber', '$lastSalary', '$bankName', '$accountName', '$accountNumber', '$ifscCode', 'https://www.facebook.com/$facebook_username', 'https://twitter.com/$twitter_username',  'https://www.instagram.com/$instagram_username', 'https://www.linkedin.com/in/$linkedin_username', '$targetFilePath','$password','$currentDateTime')";

    if ($conn->query($sql) === TRUE) {

        header("Location: add-employee.php?success=Employee Added Successfully!!!");
        exit(); // Exit to prevent further script execution


    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    // Close the database connection
    $conn->close();
}
?>