'error', 'message' => 'Unauthorized']); exit; } $queryParam = $_POST['query'] ?? $_POST['cid'] ?? $_GET['query'] ?? $_GET['cid'] ?? ''; if (empty($queryParam)) { echo json_encode(['status' => 'error', 'message' => 'กรุณาระบุเลขบัตรประชาชน หรือ ชื่อ-สกุล']); exit; } $hos_pdo = Database::getHosInstance(); if (!$hos_pdo) { echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถเชื่อมต่อฐานข้อมูล HR ได้']); exit; } try { $sql = "SELECT a.*, b.HR_DEPARTMENT_SUB_SUB_NAME, c.HR_LEVEL_NAME, d.HR_PERSON_TYPE_NAME, e.HR_PREFIX_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"; $is_cid = (is_numeric(trim($queryParam)) && strlen(trim($queryParam)) === 13); if ($is_cid) { $sql .= " WHERE a.HR_CID = ? AND a.HR_STATUS_ID='01'"; $stmt = $hos_pdo->prepare($sql); $stmt->execute([trim($queryParam)]); } else { $terms = explode(' ', preg_replace('/\s+/', ' ', trim($queryParam))); if (count($terms) > 1) { $sql .= " WHERE a.HR_FNAME LIKE ? AND a.HR_LNAME LIKE ? AND a.HR_STATUS_ID='01' LIMIT 10"; $stmt = $hos_pdo->prepare($sql); $stmt->execute(["%{$terms[0]}%", "%{$terms[1]}%"]); } else { $sql .= " WHERE (a.HR_FNAME LIKE ? OR a.HR_LNAME LIKE ?) AND a.HR_STATUS_ID='01' LIMIT 10"; $stmt = $hos_pdo->prepare($sql); $stmt->execute(["%{$terms[0]}%", "%{$terms[0]}%"]); } } $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if (count($results) > 0) { $formatted_results = []; foreach ($results as $person) { $fullName = ($person['HR_PREFIX_NAME'] ?? '') . ($person['HR_FNAME'] ?? '') . ' ' . ($person['HR_LNAME'] ?? ''); $department = $person['HR_DEPARTMENT_SUB_SUB_NAME'] ?? 'ไม่ระบุ'; $position = ($person['HR_POSITION_NAME'] ?? '') . ' ' . ($person['HR_LEVEL_NAME'] ?? ''); $person_type = $person['HR_PERSON_TYPE_NAME'] ?? 'ไม่ระบุ'; $cid = $person['HR_CID'] ?? ''; // Calculate work duration $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) { // Keep default } } $formatted_results[] = [ 'cid' => $cid, 'full_name' => trim($fullName), 'department' => trim($department), 'position' => trim($position), 'person_type' => trim($person_type), 'work_duration' => $work_duration ]; } if (count($formatted_results) === 1) { echo json_encode([ 'status' => 'success', 'message' => 'พบข้อมูลเจ้าหน้าที่', 'data' => $formatted_results[0] ]); } else { echo json_encode([ 'status' => 'multiple', 'message' => 'พบรายชื่อมากกว่า 1 คน กรุณาเลือกรายการที่ถูกต้อง', 'data' => $formatted_results ]); } } else { echo json_encode(['status' => 'error', 'message' => 'ไม่พบข้อมูลเจ้าหน้าที่ หรือไม่ได้อยู่ในสถานะปฏิบัติงาน']); } } catch (PDOException $e) { echo json_encode(['status' => 'error', 'message' => 'เกิดข้อผิดพลาดในการสืบค้นข้อมูล: ' . $e->getMessage()]); }