<?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");
$Req = json_decode($postdata, TRUE);

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


    $lat = trim($Req['lat_loca']);
    $long = trim($Req['long_loca']);
    $region_id = trim($Req['region_id']);
    $block = trim($Req['block']);
    $road = trim($Req['road']);
    $building = trim($Req['building']);
    $flat_number = trim($Req['flat_number']);
    $client_phone = trim($Req['client_phone']);
    $region_name = trim($Req['region_name']);
    $note = trim($Req['note']);
    $client_id = trim($Req['client_id']);

    $result = $connection->query("INSERT INTO client_addresses(lat_loca,long_loca,region,block,road,building,flat_number,client_phone,note,client_id,date) 
	VALUES('$lat','$long','$region_id','$block','$road','$building','$flat_number','$client_phone','$note','$client_id','" . date("Y-m-d") . "')");

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

    // temp user array
    $product = array();
    $product["client_address_id"] = mysqli_insert_id($connection);

    // 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'] = "Address Added Successfully";
        }
        // echoing JSON response
        echo json_encode($response, JSON_UNESCAPED_UNICODE);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "عفوا لقد حدث خطأ.";

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