<?php
session_start();
//Ajax Init

include "ajax-init.php";

if(isset($_GET['q'])){

    $search = strip_tags(trim($_GET['q']));
	//countryCode LIKE '$_SESSION['userCountry']%' AND

    $q = mysqli_query($dbc,"SELECT * FROM customer_tbl WHERE (companyName LIKE '%$search%' OR phoneNum  LIKE '%$search%') AND (accountType = 4 AND countryCode LIKE '$_SESSION[userCountry]%') ");

	//OR $rows['countryCode'] == $_SESSION['userCountry']."1"
	
    if(mysqli_num_rows($q)){
        $data = array();
        while($rows = mysqli_fetch_assoc($q)){
            if($_SESSION['userCountry'] == $rows['countryCode'] OR $rows['countryCode'] == $_SESSION['userCountry']."1" OR $_SESSION['userCountry'] == 'all') {
                $data[] = array(
                    'id' => $rows['id'],
                    'text' => $rows['companyName'] . ' (' . $rows['phoneNum'] . ')'
                );
            }
        }

        header('Content-type: application/json');
        echo json_encode($data);
        mysqli_close($dbc);
    }

}