<?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", "phone");
$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["required_parameters"] = "error in inputs";
    }

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

    $lang = $Req['lang'];

    $phone = trim($Req['phone']);

    $check_phone = check_phone_exist($phone); //client_id returend value 



    if ($check_phone) {

        $response["success"] = 1;
        if ($lang == "ar") {
            $response["message"] = "";
        } else {
            $response["message"] = "";
        }

        $response["client_id"] = $check_phone;
    } else {
        $response["success"] = 0;
        if ($lang == "ar") {
            $response["message"] = "ألرقم غير مسجل من قبل";
        } else {
            $response["message"] = "Number Not exist";
        }
    }
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);
?>
