<?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));

$shop_id = mysqli_real_escape_string($con, trim($request->shop_id));
$shop_code = mysqli_real_escape_string($con, trim($request->shop_code));

include 'token_check.php';

if($token_check)
{
    if($mode == "add")
    {
        if($dname == "" || $distance == "" || $deliverycharge == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            $sql = "INSERT INTO distance_db(shop_id,shop_code,name,distance,dcharge,status) VALUES ('$shop_id','$shop_code','$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' and shop_id='$shop_id' and shop_code='$shop_code'";
            $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' and shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Distance Deleted');
                echo json_encode($rows);
            }
        }
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}

?>
