false, 'message' => 'Unauthorized']); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $cid = $_POST['cid'] ?? ''; if (empty($cid)) { echo json_encode(['success' => false, 'message' => 'ไม่พบข้อมูล CID']); exit; } if (!$conn_hosoffice) { echo json_encode(['success' => false, 'message' => 'ไม่สามารถเชื่อมต่อฐานข้อมูล HR (hosoffice) ได้']); exit; } $cid_safe = mysqli_real_escape_string($conn_hosoffice, $cid); $query = "SELECT HR_STATUS_ID FROM hr_person WHERE HR_CID = '$cid_safe' LIMIT 1"; $result = mysqli_query($conn_hosoffice, $query); if ($result && mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); $hr_status = $row['HR_STATUS_ID']; $new_status = ($hr_status === '01') ? 'Y' : 'N'; // Update line_staff_register $stmt = $pdo_bot->prepare("UPDATE line_staff_register SET ksh_user = ? WHERE cid = ?"); $stmt->execute([$new_status, $cid]); echo json_encode(['success' => true, 'new_status' => $new_status, 'message' => 'อัปเดตสถานะเรียบร้อย']); } else { // Not found in hr_person $stmt = $pdo_bot->prepare("UPDATE line_staff_register SET ksh_user = 'N' WHERE cid = ?"); $stmt->execute([$cid]); echo json_encode(['success' => true, 'new_status' => 'N', 'message' => 'ไม่พบ CID นี้ในระบบ HR (อัปเดตเป็นพ้นสภาพ)']); } }