<?php  
error_reporting(E_ALL);  
ini_set('display_errors', 1);  
  
require_once('includes/load.php'); // Load database connection and functions  
  
// Function to update an employee record  
function update_employee($id, $name, $position, $division, $status) {  
    global $db;  
  
    $id = intval($id);  
    $name = $db->escape($name);  
    $position = $db->escape($position);  
    $division = $db->escape($division);  
    $status = $db->escape($status);  
  
    $sql = "UPDATE employees SET   
            name = '{$name}',   
            position = '{$position}',   
            division = '{$division}',   
            status = '{$status}'   
            WHERE id = {$id}";  
  
    return $db->query($sql);  
}  
  
// Process form submission for updating an employee  
if (isset($_POST['submit_employee'])) {  
    $employee_id = $_POST['employee_id'];  
    $name = $_POST['name'];  
    $position = $_POST['position'];  
    $division = $_POST['division'];  
    $status = $_POST['status'];  
  
    if (update_employee($employee_id, $name, $position, $division, $status)) {  
        $msg = "Employee updated successfully.";  
    } else {  
        $msg = "Failed to update employee.";  
    }  
    header('Location: nama_pegawai.php?msg=' . urlencode($msg));  
    exit();  
}  
  
// Fetch employee data for editing  
if (isset($_GET['id'])) {  
    $id = intval($_GET['id']);  
    $sql = "SELECT * FROM employees WHERE id = {$id}";  
    $employee = $db->query($sql);  
    $employee = $db->fetch_assoc($employee);  
} else {  
    header('Location: nama_pegawai.php');  
    exit();  
}  
?>  
  
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Edit Employee - HRIS</title>  
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">  
</head>  
<body>  
<div class="container mt-5">  
    <h2>Edit Employee</h2>  
    <form action="edit_pegawai.php" method="post">  
        <input type="hidden" name="employee_id" value="<?php echo $employee['id']; ?>">  
        <div class="form-group">  
            <label for="name">Name:</label>  
            <input type="text" name="name" id="name" class="form-control" value="<?php echo htmlspecialchars($employee['name']); ?>" required>  
        </div>  
        <div class="form-group">  
            <label for="position">Position:</label>  
            <input type="text" name="position" id="position" class="form-control" value="<?php echo htmlspecialchars($employee['position']); ?>">  
        </div>  
        <div class="form-group">  
            <label for="division">Division:</label>  
            <select name="division" id="division" class="form-control" required>  
                <option value="">Select Division</option>  
                <option value="warehouse surabaya" <?php echo ($employee['division'] == 'warehouse surabaya') ? 'selected' : ''; ?>>Warehouse Surabaya</option>  
                <option value="warehouse jakarta" <?php echo ($employee['division'] == 'warehouse jakarta') ? 'selected' : ''; ?>>Warehouse Jakarta</option>  
                <option value="warehouse makassar" <?php echo ($employee['division'] == 'warehouse makassar') ? 'selected' : ''; ?>>Warehouse Makassar</option>  
                <option value="warehouse bali" <?php echo ($employee['division'] == 'warehouse bali') ? 'selected' : ''; ?>>Warehouse Bali</option>  
                <option value="warehouse semarang" <?php echo ($employee['division'] == 'warehouse semarang') ? 'selected' : ''; ?>>Warehouse Semarang</option>  
                <option value="produksi" <?php echo ($employee['division'] == 'produksi') ? 'selected' : ''; ?>>Produksi</option>  
            </select>  
        </div>  
        <div class="form-group">  
            <label for="status">Status:</label>  
            <input type="text" name="status" id="status" class="form-control" value="<?php echo htmlspecialchars($employee['status']); ?>">  
        </div>  
        <button type="submit" name="submit_employee" class="btn btn-primary">Update Employee</button>  
    </form>  
</div>  
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>  
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>  
</body>  
</html>  
