<?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));
$areaname = mysqli_real_escape_string($con, trim($request->form_value->areaname));
$distance = mysqli_real_escape_string($con, trim($request->form_value->distance));
$pincode = mysqli_real_escape_string($con, trim($request->form_value->pincode));
$deliverycharge = mysqli_real_escape_string($con, trim($request->form_value->deliverycharge));

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

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

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

}

?>
