<?
header('Access-Control-Allow-Origin: *');
//header('Content-Type: application/json');

$id = $_GET['id'];
/*
if ($id){
    echo $id;
}
else {
    echo 'All Items';
}
*/


//$mysqli = new mysqli("localhost", "afxtech1_myori", "myori2017", "afxtech1_myori");
include_once("db_connect.php");
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 


//$id = '003';
//$query = "SELECT * FROM items WHERE nfc = '" .$id. "' ";
$query = "SELECT nfc, photo, status, jenama, model, attrib3, attrib4, attrib5, attrib6, attrib7, remark, type, manu as 'manuid',
            (SELECT a.name FROM manufacturer a WHERE a.id = manu) as 'manu',
            (SELECT a.telephone FROM manufacturer a WHERE a.id = manu) as 'telephone',
            (SELECT CONCAT_WS(' ', a.address, a.postcode, a.town, a.state) FROM manufacturer a WHERE a.id = manu) as 'addr',
            (SELECT a.url FROM manufacturer a WHERE a.id = manu) as 'url'
            FROM items
            WHERE nfc = '" .$id. "' ";

//            (SELECT a.address + ' ' + a.postcode + ' ' + a.town + ' ' + a.state FROM manufacturer a WHERE a.id = manu) as 'addr',
            
            
$query1 = "SELECT nfc, photo, status, jenama, model, attrib3, attrib4, attrib5, attrib6, attrib7,
            (SELECT a.name FROM manufacturer a WHERE a.id = manu) as 'manu'
            FROM items
            WHERE nfc = '" .$id. "' ";            
            
$dbresult = $mysqli->query($query);

//echo 'Hello...here1';
//die();

$mysqli->close();

$data = [];

while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){
    $data[] = array(
        'nfc' => $row['nfc'],
        'serialno' => $row['serialno'],
        'photo' => $row['photo'],
        'status' => $row['status'],
        'attrib1' => $row['jenama'],
        'attrib2' => $row['model'],
        'attrib3' => $row['attrib3'],
        'attrib4' => $row['attrib4'],
        'attrib5' => $row['attrib5'],
        'attrib6' => $row['attrib6'],
        'attrib7' => $row['attrib7'],
        'manu' => $row['manu'],
        'addr' => $row['addr'],
        'url' => $row['url'],
        'telephone' => $row['telephone'],
        'remark' => $row['remark'],
        'type' => $row['type'],
        'manuid' => $row['manuid'],
    );
}

if($dbresult){
    $result = json_encode($data);              
}
else {
    $result = "{success:false}";
}

echo $result;

?>