0) as HAS_IMAGE, e.HR_PREFIX_NAME, b.HR_DEPARTMENT_SUB_SUB_NAME, c.HR_LEVEL_NAME, d.HR_PERSON_TYPE_NAME, f.HR_POSITION_NAME FROM hr_person a LEFT OUTER JOIN hr_department_sub_sub b on a.HR_DEPARTMENT_SUB_SUB_ID = b.HR_DEPARTMENT_SUB_SUB_ID LEFT OUTER JOIN hr_level c ON a.HR_LEVEL_ID = c.HR_LEVEL_ID LEFT OUTER JOIN hr_person_type d ON a.HR_PERSON_TYPE_ID = d.HR_PERSON_TYPE_ID LEFT OUTER JOIN hr_prefix e ON a.HR_PREFIX_ID=e.HR_PREFIX_ID LEFT OUTER JOIN hr_position f ON a.HR_POSITION_ID=f.HR_POSITION_ID WHERE (a.HR_FNAME LIKE ? OR a.HR_LNAME LIKE ? OR a.HR_CID LIKE ?) AND a.HR_STATUS_ID='01' LIMIT 15"; $stmt = $pdo_hr->prepare($sql); $searchTerm = "%$keyword%"; $stmt->execute([$searchTerm, $searchTerm, $searchTerm]); $raw_results = $stmt->fetchAll(PDO::FETCH_ASSOC); $results = []; foreach ($raw_results as $person) { $prefix = $person['HR_PREFIX_NAME'] ?? ''; $fname = $person['HR_FNAME'] ?? ''; $lname = $person['HR_LNAME'] ?? ''; $fullName = trim($prefix . $fname . ' ' . $lname); $department = $person['HR_DEPARTMENT_SUB_SUB_NAME'] ?? 'ไม่ระบุ'; $position = trim(($person['HR_POSITION_NAME'] ?? '') . ' ' . ($person['HR_LEVEL_NAME'] ?? '')); $person_type = $person['HR_PERSON_TYPE_NAME'] ?? 'ไม่ระบุ'; $work_duration = 'ไม่ระบุวันที่เริ่มงาน'; if (!empty($person['HR_STARTWORK_DATE'])) { try { $startDate = new DateTime($person['HR_STARTWORK_DATE']); $currentDate = new DateTime(); if ($startDate <= $currentDate) { $interval = $currentDate->diff($startDate); $work_duration = $interval->format('%y ปี %m เดือน %d วัน'); } } catch (Exception $e) { } } $results[] = [ 'prefix' => $prefix, 'fname' => $fname, 'lname' => $lname, 'full_name' => $fullName, 'department' => $department, 'position' => $position, 'person_type' => $person_type, 'work_duration' => $work_duration, 'phone' => $person['HR_PHONE'] ?? '', 'sex' => $person['SEX'] ?? '', 'has_image' => (bool)$person['HAS_IMAGE'], 'cid' => $person['HR_CID'] ]; } jsonResponse(true, 'Success', $results); } catch (PDOException $e) { jsonResponse(false, 'Database Error: ' . $e->getMessage()); } ?>