<?php
include("config2.php");

$search = $conn->real_escape_string($_POST['search']);
$today = date('Y-m-d');
$nextDay = date('Y-m-d', strtotime($today . ' +1 day'));
$prevDay = date('Y-m-d', strtotime($today . ' -1 day'));
$sql = "
SELECT DISTINCT
    c.id,
    c.customer_name,
    c.personal_phn
FROM customer c

/* join bill for search */
LEFT JOIN bill b 
    ON b.customer_id = c.id

/* join bookings for search */
LEFT JOIN bookings bk
    ON bk.customer_id = c.id

WHERE
      c.nid LIKE '%$search%'
   OR c.personal_phn LIKE '%$search%'
   OR c.customer_name LIKE '%$search%'
   OR b.bill_id LIKE '%$search%'
   OR bk.room_no='$search' AND bk.check_in <= '$nextDay'  AND bk.check_in >= '$prevDay' 

LIMIT 10";

 

//$sql = "SELECT * FROM customer WHERE nid LIKE '%$search%' or personal_phn LIKE '%$search%'  or 	customer_name LIKE '%$search%' LIMIT 10";
$result = $conn->query($sql);

if ($result->num_rows == 0) {
    echo "<div style='padding:8px;'>No results...</div>";
    exit;
}

while ($row = $result->fetch_assoc()) {

    // JSON encode for sending full record in HTML attribute
    $customer_json = htmlspecialchars(json_encode($row), ENT_QUOTES, 'UTF-8');

    echo "
    <div class='suggest-item' data-customer='$customer_json'>
      {$row['id']} — {$row['personal_phn']} — {$row['customer_name']}
    </div>
    ";
}
?>
 
