query("SELECT id, name FROM departments ORDER BY name ASC")->fetchAll(); $positions = $pdo->query("SELECT id, name FROM positions ORDER BY name ASC")->fetchAll(); $errors = []; $values = [ 'employee_code' => '', 'full_name' => '', 'email' => '', 'phone' => '', 'status' => 'Aktif', 'employment_type' => 'Kontrak', 'hire_date' => '', 'dept_id' => '', 'position_id' => '', ]; if ($_SERVER['REQUEST_METHOD'] === 'POST') { foreach ($values as $key => $val) { $values[$key] = trim($_POST[$key] ?? ''); } if ($values['employee_code'] === '') { $errors[] = 'Kode pegawai wajib diisi.'; } if ($values['full_name'] === '') { $errors[] = 'Nama lengkap wajib diisi.'; } if (!$errors) { $stmt = $pdo->prepare(" INSERT INTO employees (employee_code, full_name, email, phone, status, employment_type, hire_date, dept_id, position_id) VALUES (:employee_code, :full_name, :email, :phone, :status, :employment_type, :hire_date, :dept_id, :position_id) "); $stmt->bindValue(':employee_code', $values['employee_code']); $stmt->bindValue(':full_name', $values['full_name']); $stmt->bindValue(':email', $values['email'] !== '' ? $values['email'] : null); $stmt->bindValue(':phone', $values['phone'] !== '' ? $values['phone'] : null); $stmt->bindValue(':status', $values['status'] !== '' ? $values['status'] : 'Aktif'); $stmt->bindValue(':employment_type', $values['employment_type'] !== '' ? $values['employment_type'] : 'Kontrak'); $stmt->bindValue(':hire_date', $values['hire_date'] !== '' ? $values['hire_date'] : null); $stmt->bindValue(':dept_id', $values['dept_id'] !== '' ? (int)$values['dept_id'] : null, PDO::PARAM_INT); $stmt->bindValue(':position_id', $values['position_id'] !== '' ? (int)$values['position_id'] : null, PDO::PARAM_INT); $stmt->execute(); $newId = (int)$pdo->lastInsertId(); header('Location: employee_view.php?id=' . $newId . '&created=1'); exit; } } render_header('Tambah Pegawai', 'employees'); ?>
Lengkapi profil pegawai baru, status kerja, dan penempatan.