<?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  
$Req = $_GET;

if (isset($Req) && !empty($Req)) {

    $client_phone = $Req['phone'];
    $lang = $Req['lang'];

    $check_register_phone = check_register_phone($client_phone);
    if ($check_register_phone == $client_phone) {

// failed to insert row
        $response["success"] = 1;
        $response["exist"] = 1;
        $response["client_id"] = get_client_id_from_phone($client_phone);
        
        if ($lang == "ar") {
            $response["message"] = "هذ الرقم مسجل من قبل!";
        } else {
            $response["message"] = "This number is already registered!";
        }
// echoing JSON response
        echo json_encode($response);
    } else {

        // failed to insert row
        $response["success"] = 1;
        $response["exist"] = 0;
        $response["client_id"] = get_client_id_from_phone($client_phone);
        if ($lang == "ar") {
            $response["message"] = "هذ الرقم غير مسجل من قبل!";
        } else {
            $response["message"] = "This number is not registered!";
        }
// echoing JSON response
        echo json_encode($response);
    }
} else {
// required field is missing
    $response["success"] = 0;
    if ($lang == "ar") {
        $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";
    } else {
        $response["message"] = "Missing data Please review your data";
    }
// echoing JSON response
    echo json_encode($response);
}
?>