<?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');


$required_parametrs = array("lang", "client_id");
$errors = array();


$Req = $_GET;


foreach ($Req as $key => $value) {
    if (in_array($key, $required_parametrs)) {
        $key = array_search($key, $required_parametrs);
        unset($required_parametrs[$key]);
    } else {
        $errors[] = "Parameter field is required:";
    }
}
$lang = $Req['lang'];

if (count($required_parametrs) > 0) {
    //some parameters required
    $response["success"] = 0;
    if ($lang == "ar") {
        $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";
    } else {
        $response["message"] = "error in inputs";
    }

    $response["required_parameters"] = $required_parametrs;
} else {

    $lang = mysqli_real_escape_string($connection,$Req['lang']);

    $id = mysqli_real_escape_string($connection,$Req['client_id']);



    $result = $connection->query("SELECT * FROM `devices` WHERE `client_id`='$id' ")  or die(mysqli_error($connection));
    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $device_token_id = $row['device_token_id'];
        
         $connection->query("DELETE FROM `device_token` WHERE `id`='$device_token_id' ");
    }
    
    $result =  $connection->query("DELETE FROM `devices` WHERE `client_id`='$id' ");

    if ($result) {

        $response["success"] = 1;
        if ($lang == "en") {
            $response["message"] = "Logedout Successfully  ";
        } else {
            $response["message"] = "تم الخروج بنجاح";
        }
    } else {

        $response["success"] = 0;
        if ($lang == "ar") {
            $response["message"] = "لقد حدث خطأ";
        } else {
            $response["message"] = "error";
        }
    }
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>
