<?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));
$deli_id = mysqli_real_escape_string($con, trim($request->deli_id));
$name = mysqli_real_escape_string($con, trim($request->form_value->deliveryboyname));
$email = mysqli_real_escape_string($con, trim($request->form_value->emailid));
$mobile = mysqli_real_escape_string($con, trim($request->form_value->mobileno));

// db name = rider
$aid = 1;
$status =1;

if($mode == "add")
{
    if($name == "" || $mobile == "" || $email == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $sql = "INSERT INTO rider(name,mobile,email,aid,address,status,password) VALUES ('$name','$mobile','$email','$aid','','$status','arabian')";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Rider Added');
            echo json_encode($rows);
        }
    }
}

else if($mode == "edit")
{
    if($deli_id == "" || $name == "" || $mobile == "" || $email == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $sql = "UPDATE rider set name = '$name',mobile='$mobile',email='$email' where id = '$deli_id'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Rider Updated');
            echo json_encode($rows);
        }
    }
}

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

?>
