<?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");
    if($row1 = $sel->fetch_assoc())
    {
        $name = $row1['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;
        }
        
        $new_distance = number_format($new_dis, 2);

        $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_distance','$latitude','$longitude')";
        $con->query($sql);
        
        $add = array();
    	$d = $con->query("select * from address where uid = '$uid'");
        while ($row = $d->fetch_assoc()) {

            $addd['latitude'] = $row['latitude'];
            $addd['longitude'] = $row['longitude'];

            $addd['delivery_charge'] = 0;
            $addd['distance'] = $row['distance'];
            $addd['shop_distance'] = $row['shop_distance'];

            $addd['id'] = $row['id'];
            $addd['hno'] = $row['hno'];
            $addd['area'] = $row['area'];
            $addd['landmark'] = $row['landmark'];
            $addd['name'] = $row['name'];
            $addd['pincode'] = $row['pincode'];
            $addd['street'] = $row['street'];
            $addd['type'] = $row['type'];
            $addd['uid'] = $row['uid'];
            $add[] = $addd;
        }
        
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Address Saved Successfully!","ResultData"=>$add);
    }
    else
    {
        $returnArr = array("ResponseCode"=>$sql,"Result"=>"false","ResponseMsg"=>"User Deactivated!");
    }
}
echo json_encode($returnArr);
mysqli_close($con);
?> 