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

$officer= $_GET['o'];
/*
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 reports WHERE officer = '" .$ofc. "' ORDER BY rpt_txn ";
//$query = "SELECT id, rpt_txn, DATE_FORMAT(rpt_txn, '%Y%m%d') as 'rpt_date', status, type, category, priority, location FROM reports WHERE status <> '0' AND status <> 'C' AND officer = '" .$officer. "' ORDER BY rpt_txn ";

//fuad:20211108
$query = "SELECT id, rpt_txn, DATE_FORMAT(rpt_txn, '%Y%m%d') as 'rpt_date', status, type, category, priority, location, notes, remark1, remark2, product_category, product_subcategory, product_detail, product_name, product_manufacturer, (select action from reports_actions where id=r.id order by rpt_txn desc limit 1) as action FROM reports r WHERE status <> '0' AND status <> 'C' AND officer = '" .$officer. "' ORDER BY rpt_txn ";
$dbresult = $mysqli->query($query);

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

$mysqli->close();

$data = [];

$sort = 0;
while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){
    $sort++;
    $dt = date_create($row['rpt_date']);
    $data[] = array(
        'sort' => $sort,
        'id' => $row['id'],
        'rpt_txn' => $row['rpt_txn'],
        'rpt_date' => $row['rpt_date'],
        'dt' => date_format($dt,"M d"),
        'status' => $row['status'],
        'action' => $row['action'],
        
        'type' => $row['type'],
        'cat' => $row['category'],
        'pri' => $row['priority'],
        'loc' => $row['location'],
        
        'notes' => $row['notes'],
        'remark1' => $row['remark1'],
        'remark2' => $row['remark2'],
        'pcategory' => $row['product_category'],
        'psubcategory' => $row['product_subcategory'],
        'pdetail' => $row['product_detail'],
        'pname' => $row['product_name'],
        'pmanufacturer' => $row['product_manufacturer'],
    );
}

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

echo $result;

?>