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

if (!$_SESSION["manu"]) {
    http_response_code(401);
    die();
}

$manu = $_SESSION["manu"];

//$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 i.nfc, i.serialno, i.status, i.details, i.jenama, i.model,
                i.attrib3, i.attrib4, i.attrib5, i.attrib6, i.attrib7, i.produk, i.sku, i.type, i.waranti, i.insuran, i.prod_year,
                (select c.certificate from coas c where c.manu = i.manu and c.nfc = i.nfc order by c.timestamp desc limit 1) as coa_cert,
                (select c.status from coas c where c.manu = i.manu and c.nfc = i.nfc order by c.timestamp desc limit 1) as coa_status
            FROM items i
            WHERE i.manu = '" .$manu. "' ORDER BY i.nfc" ;
$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'],
        //'photo2' => $row['photo2'],
        //'photo3' => $row['photo3'],
        //'photo4' => $row['photo4'],
        'status' => $row['status'],
        'details' => $row['details'],
        'jenama' => $row['jenama'],
        'model' => $row['model'],
        'attrib3' => $row['attrib3'],
        'attrib4' => $row['attrib4'],
        'attrib5' => $row['attrib5'],
        'attrib6' => $row['attrib6'],
        'attrib7' => $row['attrib7'],
        'produk' => $row['produk'],
        'sku' => $row['sku'],
        'type' => $row['type'],
        'waranti' => $row['waranti'],
        'insuran' => $row['insuran'],
        'prod_year' => $row['prod_year'],
        'coa_cert' => $row['coa_cert'],
        'coa_status' => $row['coa_status']
    );
}

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

echo $result;


?>