<?php
include '../includes/db.php'; // your existing database connection

$q = isset($_GET['q']) ? $_GET['q'] : '';
$q = $conn->real_escape_string($q);

if($q != '') {
    // Only search visible services
    $sql = "SELECT id, test_name, description 
            FROM Services 
            WHERE is_visible = 1 AND test_name LIKE '%$q%' 
            ORDER BY test_name ASC 
            LIMIT 10";

    $result = $conn->query($sql);

    if(!$result){
        die("SQL Error: " . $conn->error);
    }

    if($result->num_rows > 0){
        echo '<ul style="list-style:none; margin:0; padding:0;">';
        while($row = $result->fetch_assoc()){
            $name = htmlspecialchars($row['test_name'], ENT_QUOTES);
           

            echo '<li style="padding:8px; border-bottom:1px solid #eee; cursor:pointer;" ';
            echo "onclick=\"window.location.href='service-details.php?id={$row['id']}'\">";

            echo "<strong>{$name}</strong><br><small>{$desc}</small>";
            echo '</li>';
        }
        echo '</ul>';
    } else {
        echo '<div style="padding:8px;">No services found</div>';
    }
}

$conn->close();
?>

<script>
function selectService(name){
    document.getElementById('service-search').value = name;
    document.getElementById('search-results').style.display = 'none';
}
</script>
