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

    $lang = $Req['lang'];

    $lat = $Req['lat_loca'];
    $long = $Req['long_loca'];

    $block = mysqli_real_escape_string($connection,$Req['block']);
    $road = mysqli_real_escape_string($connection,$Req['road']);
    $building = mysqli_real_escape_string($connection,$Req['building']);
    $flat_number = mysqli_real_escape_string($connection,$Req['flat_number']);
    $note = mysqli_real_escape_string($connection,$Req['note']);

    $region = $Req['region_id'];
    $client_phone = mysqli_real_escape_string($connection,$Req['client_phone']);
    $client_id = $Req['client_id'];
    $client_address_id = $Req['client_address_id'];

    $result = $connection->query("UPDATE `client_addresses` SET `lat_loca`='$lat',`long_loca`='$long',`region`='$region',`block`='$block',`road`='$road',
	`building`='$building',`flat_number`='$flat_number',`client_phone`='$client_phone',`note`='$note' WHERE `client_id`='$client_id' 
	AND `client_address_id`='$client_address_id'");


    $response["product"] = array();

    // temp user array
    $product = array();


    // push single product into final response array
    array_push($response["product"], $product);


    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        if ($lang == "ar") {
            $response["message"] = "تم تحديث العنوان بنجاح.";
        } else {
            $response["message"] = "Updated Successfully   .";
        }

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        if ($lang == "ar") {
            $response["message"] = "عفوا لقد حدث خطأ.";
        } else {
            $response["message"] = " Error   .";
        }
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";

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