<?php

/*
 * Following code will list all the products
 */
// array for JSON response
$response = array();

// include db connect class
include("db_connect.php");

// connecting to db
$con = new DB_CONNECT();
$connection = $con->connect();

$connection->query("SET NAMES 'utf8'");

$connection->query("SET CHARACTER SET utf8");

$connection->query("SET SESSION collation_connection = 'utf8_unicode_ci'");

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

header('Content-Type: application/json');

// get all products from products table
// Start Functionality  
$postdata = file_get_contents("php://input");

if (isset($postdata) && !empty($postdata)) {
    $Req = json_decode($postdata, TRUE);


    $client_id = $Req["client_id"];
    $device_token = $Req['device_token'];
    $type = $Req['type'];

    $check_token = check_token($device_token, $client_id);
    if ($check_token > 0) {
        $devicesArray = "SELECT device_token.id as device_token_id  from `device_token` left join devices on devices.device_token_id =device_token.id  WHERE device_token.device_token='$device_token' and devices.client_id='{$client_id}'";
        $query = $connection->query($devicesArray);
        $query_devicesArray = mysqli_fetch_array($query);
        $device_token_id = $query_devicesArray['device_token_id'];

        $result_zero = $connection->query("UPDATE devices SET `login`='1',`date_added`='" . date("Y-m-d H:i:s") . "' WHERE `device_token_id`='$device_token_id' and `client_id`='$client_id'");
    } else {
        $result_one = $connection->query("INSERT INTO device_token(device_token,type,date_added) 
	VALUES('$device_token','$type','" . date("Y-m-d H:i:s") . "')");
        $device_token_id = mysqli_insert_id($connection);
        $result_two = $connection->query("INSERT INTO devices(device_token_id,client_id,login,date_added) 
	VALUES('$device_token_id','$client_id','1','" . date("Y-m-d H:i:s") . "')");
    }
    // success

    $response["device_token_id"] = $device_token_id;
    $response["success"] = 1;


    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";

    // echo no clients JSON
    echo json_encode($response);
}
?>