<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$mode = mysqli_real_escape_string($con, trim($request->mode));
$areaid = mysqli_real_escape_string($con, trim($request->area_id));
$dname = mysqli_real_escape_string($con, trim($request->form_value->dname));
$distance = mysqli_real_escape_string($con, trim($request->form_value->distance));
$deliverycharge = mysqli_real_escape_string($con, trim($request->form_value->deliverycharge));

if($mode == "add")
{
    if($dname == "" || $distance == "" || $deliverycharge == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $sql = "INSERT INTO distance_db(name,distance,dcharge,status) VALUES ('$dname','$distance','$deliverycharge','1')";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Distance Added');
            echo json_encode($rows);
        }
    }
}

else if($mode == "edit")
{
    if($dname == "" || $distance == "" || $deliverycharge == "" || $areaid == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $sql = "UPDATE distance_db set name='$dname',distance='$distance',dcharge = '$deliverycharge' where id ='$areaid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Distance Updated');
            echo json_encode($rows);
        }
    }
}

else if($mode == "delete")
{
    if($areaid == "")
    {
        $rows = array('status'=>false,'message' => 'Distance Error');
        echo json_encode($rows);
    }
    else
    {
        $sql = "DELETE FROM distance_db where id ='$areaid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Distance Deleted');
            echo json_encode($rows);
        }
    }
}

}

?>
