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

<?php 
include("../connection.php");
include("../user.php");
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start(); // Ensure session is started

$user = $_SESSION['user'] ?? '';
$branch_id = $_SESSION['branch_id'] ?? '';
$ho_access = $_SESSION['ha'] ?? '';
?>

<?php
$id = $_REQUEST['id'];
$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'];
$photo = $_REQUEST['filename'];

$default_img = "/asset_temp/master/test/default.png";
$fileName = "file";
$filepath = "/asset_temp/master/test/" . basename($_FILES[$fileName]['name']);
$target_path = "test/" . basename($_FILES[$fileName]['name']);

if (!empty($_FILES[$fileName]['name'])) {
    move_uploaded_file($_FILES[$fileName]['tmp_name'], $target_path);
} else {
    $filepath = $photo; // retain existing photo if no new upload
}

$update_query = "
    UPDATE employee_master SET 
        emp_no = '$emp_code',
        employee_name = '$emp_name',
        fathers_name = '$f_name',
        dob = '$dob',
        department = '$department',
        gender = '$gender',
        mobile = '$mbl',
        adhar_no = '$adhaar_no',
        blood_group = '$blood',
        mail_id = '$email',
        address = '$address',
        profile_photo = '$filepath',
        branch_id = '$branch',
        modified_by = '$user',
        modified_on = NOW()
    WHERE id = '$id'
";

$employee_master = mysqli_query($con, $update_query);

if ($employee_master) {
?>
<script>
setTimeout(function() {
    swal({
        title: "Successfully",
        text: "Updated!",
        type: "success"
    }, function() {
        window.location = "employee_bulk_report.php";
    });
}, 100);
</script>  
<?php
} else {
?>
<script>
setTimeout(function() {
    swal({
        title: "Not",
        text: "updated!",
        type: "error"
    }, function() {
        window.location = "employee_bulk_report.php";
    });
}, 100);
</script> 	  
<?php
}
?>

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