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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
    $id = $_POST["id"];
    $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"];
    $facebook = $_POST["facebook_username"];
    $twitter = $_POST["twitter_username"];
    $instagram = $_POST["instagram_username"];
    $linkedin = $_POST["linkedin_username"];

    // Check if a file is uploaded
    if (!empty($_FILES["file"]["name"])) {
        $targetDir = "uploads/"; // Specify the directory where files will be uploaded
        $targetFile = $targetDir . basename($_FILES["file"]["name"]); // Get the file name

        // Check if the file is a valid image
        $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
        $allowedExtensions = array("jpg", "jpeg", "png", "gif");
        if (!in_array($imageFileType, $allowedExtensions)) {
            echo "Invalid file format. Only JPG, JPEG, PNG, and GIF files are allowed.";
            exit();
        }

        // Upload the file
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
            // File uploaded successfully, now update the database with the file name
            $sql = "UPDATE add_employee SET 
                    name='$name',
                    employee_id='$employeeId',
                    number1='$number1',
                    number2='$number2',
                    email1='$email1',
                    email2='$email2',
                    designation='$designation',
                    salary='$salary',
                    joining_date='$joiningDate',
                    previous_company='$previousCompany',
                    company_website='$companyWebsite',
                    contact_person='$contactPerson',
                    contact_number='$contactNumber',
                    last_salary='$lastSalary',
                    bank_name='$bankName',
                    account_holder_name='$accountName',
                    account_number='$accountNumber',
                    ifsc_code='$ifscCode',
                    facebook='https://www.facebook.com/$facebook',
                    twitter='https://twitter.com/$twitter',
                    instagram='https://www.instagram.com/$instagram',
                    linkedin='https://www.linkedin.com/in/$linkedin',
                    profile_picture='$targetFile'
                    WHERE id='$id'";

            if ($conn->query($sql) === TRUE) {
                header("Location: all-employees.php?success=Employee Updated Successfully!!!");
                exit(); // Exit to prevent further script execution
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        } else {
            echo "File upload failed!";
        }
    } else {
        // If no file is uploaded, update only other details
        $sql = "UPDATE add_employee SET 
                name='$name',
                employee_id='$employeeId',
                number1='$number1',
                number2='$number2',
                email1='$email1',
                email2='$email2',
                designation='$designation',
                salary='$salary',
                joining_date='$joiningDate',
                previous_company='$previousCompany',
                company_website='$companyWebsite',
                contact_person='$contactPerson',
                contact_number='$contactNumber',
                last_salary='$lastSalary',
                bank_name='$bankName',
                account_holder_name='$accountName',
                account_number='$accountNumber',
                ifsc_code='$ifscCode',
                facebook='https://www.facebook.com/$facebook',
                twitter='https://twitter.com/$twitter',
                instagram='https://www.instagram.com/$instagram',
                linkedin='https://www.linkedin.com/in/$linkedin'
                WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            header("Location: all-employees.php?success=Employee Updated Successfully!!!");
            exit(); // Exit to prevent further script execution
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }

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