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


if (isset($_GET['client_id'])) {
    $client_id = $_GET['client_id'];
    $result = $connection->query("SELECT * FROM `clients` WHERE `client_id`='$client_id' limit 1")  or die(mysqli_error($connection));
    $response["product"] = array();

    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node

        while ($row = mysqli_fetch_array($result)) {
            // temp client array
            $product = array();
            $product["exist"] = 1;
            // push single product into final response array
            array_push($response["product"], $product);
        }
        // success
        $response["success"] = 1;


        // echoing JSON response
        echo json_encode($response);
    } else {
        $product = array();
        $product["exist"] = 0;
        // push single product into final response array
        array_push($response["product"], $product);
        // failed to insert row
        $response["success"] = 1;

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

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