<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once 'vendor/autoload.php';

// Takes raw data from the request
$json = file_get_contents('php://input');
if(empty($json))
{
    $data = array('success'=>false,"message"=>"Please enter Serial Number");
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data);
    die;
}
// Converts it into a PHP object
$data = json_decode($json);
if(empty($data->serialno))
{
    $data = array('success'=>false,"message"=>"Please enter Serial Number");
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data);
    die;
}

//1. get the pproperty and user from the hardware collection
/*$client = new MongoDB\Client(
    'mongodb+srv://keyurrigic:GDBKMY7KWnyrtydM@cluster0.s70wwrj.mongodb.net/?retryWrites=true&w=majority'
);*/
/* live */
$client = new MongoDB\Client(
    'mongodb+srv://dov:7F76fgn4qK1b6qGS@clusterdeletmvp.avbxw.mongodb.net/delet-mvp'
);

$db = $client->{'delet-mvp'};
$collection = $db->hardwares;
//1. find the hardware 
$hardware = $collection->findOne(['serial' => $data->serialno]);
//http://18.116.112.179/
// get the user 

$user=$hardware->user;
$property=$hardware->property;

/*echo "<pre>";
print_r($user);
print_r($property);*/
if(empty($user)){
    $data = array('success'=>false,"message"=>"User assigned to this tablet");
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data);
}
if(empty($property))
{
    // get kit info 
    $kit=$hardware->kit;
    echo "<pre>";
    print_r($kit);
    die;
    $data = array('success'=>false,"message"=>"Property not assigned to this tablet");
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data);
    die;
}
//now get the property and user data 
//$arr[] = new MongoDB\BSON\ObjectId($json);
$collection = $db->users;
//1. find the hardware 
$users = $collection->findOne(['_id' => $user]);

$collection = $db->properties;
//1. find the hardware 
$properties = $collection->findOne(['_id' => $property]);

$data=array(
    'success'=>true,
    'user'=>$users,
    'property'=>$properties
);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
die;