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

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

//get query strings
//
$carian = $_GET['carian'];
$param1 = $_GET['param1'];
$param2 = $_GET['param2'];
$param3 = $_GET['param3'];


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


$clause = " id is NULL ";

if ($carian==1) {
    $clause = " rpt_txn >= '" .$param1. " 00:00:00' AND rpt_txn <= '" .$param2. " 23:59:59' ";
}

if ($carian==2) {
    $clause = " id = '" .$param1. "' ";
}

if ($carian==3) {
    $clause = " location like '%" .$param1. "%' OR area like '%" .$param1. "%' ";
}

if ($carian==4) {
    $clause = " type = '" .$param1. "' ";
}

if ($carian==5) {
    $clause = " manu in (SELECT id FROM manufacturer WHERE name like '%" .$param1. "%') ";
}

if ($carian==5) {
    $clause = " manu in (SELECT id FROM manufacturer WHERE name like '%" .$param1. "%') ";
}
    



$clause = 




//$id = '003';
//$query = "SELECT * FROM reports WHERE status = '0' ORDER BY rpt_txn ";
$query = "SELECT id, rpt_txn, status, DATE(rpt_txn) as 'rpt_date' 
            FROM reports 
            WHERE "
            .$clause.
            " ORDER BY rpt_txn ";
$dbresult = $mysqli->query($query);

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

$mysqli->close();

$data = [];

while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){
    $data[] = array(
        'id' => $row['id'],
        'rpt_txn' => $row['rpt_txn'],
        'status' => $row['status'],
        'rpt_date' => $row['rpt_date']
    );
}

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

echo $result;

?>