"error", "message" => "Missing parameters"]); exit; } $hn_raw = decrypt_param($_POST['hn']); // Note: If frontend sends encrypted hn, we should decrypt it first. Or does frontend send raw hn? // Actually, let's just use $_POST['hn'] as raw hn if it's raw, but let's check what we pass. // It's safer to pass encrypted hn from frontend and decrypt here. $hn = mysqli_real_escape_string($conn1, $hn_raw); $keyword = mysqli_real_escape_string($conn1, $_POST['keyword']); if (strlen($keyword) < 2) { echo json_encode(["status" => "success", "data" => []]); exit; } $results = []; try { // 1. Search Medications $sql_med = "SELECT o.vstdate, o.vn, o.an, concat(s.name, ' ', s.strength, ' ', s.units) as item_name, 'med' as item_type FROM opitemrece o LEFT JOIN s_drugitems s ON s.icode = o.icode WHERE o.hn = '$hn' AND s.name LIKE '%$keyword%' GROUP BY o.vstdate, o.vn, o.an, item_name ORDER BY o.vstdate DESC LIMIT 20"; $res_med = mysqli_query($conn1, $sql_med); if ($res_med) { while ($row = mysqli_fetch_assoc($res_med)) { $results[] = [ 'vstdate' => $row['vstdate'], 'vstdate_thai' => thai_date2($row['vstdate']), 'enc_vdate' => encrypt_param($row['vstdate']), 'enc_nn' => encrypt_param($row['an'] ? $row['an'] : $row['vn']), 'item_name' => $row['item_name'], 'type' => 'med', 'type_label' => 'ยา', 'color' => 'indigo' ]; } } // 2. Search Labs $sql_lab = "SELECT o.vstdate, h.vn, i.lab_items_name as item_name, 'lab' as item_type FROM lab_order l LEFT JOIN lab_items i ON i.lab_items_code = l.lab_items_code LEFT JOIN lab_head h ON h.lab_order_number = l.lab_order_number LEFT JOIN ovst o ON o.vn = h.vn WHERE h.hn = '$hn' AND i.lab_items_name LIKE '%$keyword%' GROUP BY o.vstdate, h.vn, i.lab_items_name ORDER BY o.vstdate DESC LIMIT 20"; $res_lab = mysqli_query($conn1, $sql_lab); if ($res_lab) { while ($row = mysqli_fetch_assoc($res_lab)) { // Fallback to order_date if ovst doesn't have it for some reason $vstdate = $row['vstdate'] ? $row['vstdate'] : (isset($row['order_date']) ? $row['order_date'] : date('Y-m-d')); $results[] = [ 'vstdate' => $vstdate, 'vstdate_thai' => thai_date2($vstdate), 'enc_vdate' => encrypt_param($vstdate), 'enc_nn' => encrypt_param($row['vn']), 'item_name' => $row['item_name'], 'type' => 'lab', 'type_label' => 'แล็บ', 'color' => 'purple' ]; } } // 3. Search X-Rays $sql_xray = "SELECT o.vstdate, xr.vn, xh.xray_list as item_name, 'xray' as item_type FROM xray_report xr LEFT JOIN xray_head xh ON xh.vn = xr.vn LEFT JOIN ovst o ON o.vn = xr.vn WHERE xh.hn = '$hn' AND xh.xray_list LIKE '%$keyword%' GROUP BY o.vstdate, xr.vn, xh.xray_list ORDER BY o.vstdate DESC LIMIT 20"; $res_xray = mysqli_query($conn1, $sql_xray); if ($res_xray) { while ($row = mysqli_fetch_assoc($res_xray)) { // Fallback to report_date if ovst doesn't have it $vstdate = $row['vstdate'] ? $row['vstdate'] : (isset($row['report_date']) ? $row['report_date'] : date('Y-m-d')); $results[] = [ 'vstdate' => $vstdate, 'vstdate_thai' => thai_date2($vstdate), 'enc_vdate' => encrypt_param($vstdate), 'enc_nn' => encrypt_param($row['vn']), 'item_name' => $row['item_name'], 'type' => 'xray', 'type_label' => 'X-Ray', 'color' => 'amber' ]; } } // Sort combined results by date descending usort($results, function($a, $b) { return strtotime($b['vstdate']) - strtotime($a['vstdate']); }); // Limit to top 50 $results = array_slice($results, 0, 50); echo json_encode(["status" => "success", "data" => $results]); } catch (Exception $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } ?>