<?php 
 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);

$uid = $data['uid'];
$hno = $data['hno'];
$street = mysqli_real_escape_string($con,$data['street']);
$address = $data['address'];
$area = $data['area'];
$pincode = $data['pincode'];
$landmark = mysqli_real_escape_string($con,$data['landmark']);
$type = $data['type'];
$distance = $data['distance'];
$latitude = $data['latitude'];
$longitude = $data['longitude'];
$name = '';

if($area == '' or $hno == '' or $street == '' or $address == '' or $uid == '' or $type == '' or $latitude == '' or $distance == '' or $longitude == '' or $pincode == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
}
else 
{
    $sel = $con->query("select * from user where id='$uid' and status = 1");
    while($row = $sel->fetch_assoc())
    {
        $name = $row['name'];
    }
    if($name != '')
    {
        if($distance >= 1 && $distance < 3.6)
        {
            $new_dis = $distance + $distance * 20 / 100;
        }
        else if($distance >= 3.6 && $distance < 6)
        {
            $new_dis = $distance + $distance * 15 / 100;
        }
        else if($distance >= 6 && $distance < 7)
        {
            $new_dis = $distance + $distance * 12 / 100;
        }
        else if($distance >= 7 && $distance < 8.8)
        {
            $new_dis = $distance + $distance * 9.8 / 100;
        }
        else if($distance >= 8.8 && $distance < 10)
        {
            $new_dis = $distance + $distance * 8 / 100;
        }
        else
        {
            $new_dis = $distance;
        }

        $sql = "insert into address(`uid`, `hno`, `street`, `area`, `pincode`, `landmark`, `type`, `name`, `cur_location`, `distance`, `shop_distance`, `latitude`, `longitude`)values
        ('$uid','$hno','$street','$area','$pincode','$landmark','$type','$name','$address','$distance','$new_dis','$latitude','$longitude')";
        $con->query($sql);
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Address Saved Successfully!");
    }
    else
    {
        $returnArr = array("ResponseCode"=>$sql,"Result"=>"false","ResponseMsg"=>"User Deactivated!");
    }
}
echo json_encode($returnArr);
mysqli_close($con);
?> 