<link rel="stylesheet" href="../css/sweet-alert.css" /> 

<?php 
include("../connection.php");
include("../user.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$user = $_SESSION['user'];
$branch_id = $_SESSION['branch_id'];
$ho_access = $_SESSION['ha'];
?>

<?php
$branch = $_REQUEST['branch'];
$emp_code = $_REQUEST['emp_code'];
$emp_name = $_REQUEST['emp_name'];
$email = $_REQUEST['email'];
$mbl = $_REQUEST['mbl'];
$gender = $_REQUEST['gender'];
$dob = $_REQUEST['dob'];
$f_name = $_REQUEST['f_name'];
$department = $_REQUEST['department'];
$adhaar_no = $_REQUEST['adhaar_no'];
$blood = $_REQUEST['blood'];
$address = $_REQUEST['address'];

$default_img = "/asset_temp/master/test/default.png";
$fileName = "file";

$filepath = $default_img; // Set default first

if (!empty($_FILES[$fileName]['name'])) {
    $upload_folder = "test/";
    $filename = basename($_FILES[$fileName]['name']);
    $target_path = $upload_folder . $filename;

    // Create folder if not exists
    if (!is_dir($upload_folder)) {
        mkdir($upload_folder, 0777, true);
    }

    if (move_uploaded_file($_FILES[$fileName]['tmp_name'], $target_path)) {
        $filepath = "/asset_temp/master/" . $target_path;
    }
}

$sql = "INSERT INTO employee_master 
    (emp_no, employee_name, fathers_name, dob, department, gender, mobile, adhar_no, blood_group, mail_id, address, profile_photo, status, branch_id, created_by)
    VALUES 
    ('$emp_code', '$emp_name', '$f_name', '$dob', '$department', '$gender', '$mbl', '$adhaar_no', '$blood', '$email', '$address', '$filepath', '0', '$branch', '$user')";

$result = mysqli_query($con, $sql);

if ($result) {
?>
    <script>
        setTimeout(function () {
            swal({
                title: "Success",
                text: "Employee inserted successfully!",
                type: "success"
            }, function () {
                window.location = "employee_bulk_report.php";
            });
        }, 100);
    </script>
<?php
} else {
?>
    <script>
        setTimeout(function () {
            swal({
                title: "Error",
                text: "Insertion failed!",
                type: "error"
            }, function () {
                window.location = "employee_bulk_report.php";
            });
        }, 100);
    </script>
<?php
}
?>

<script src="../js/sweet-alert.js"></script>
