<?php

header("Content-Type: application/json");

require_once("../config/database.php");

$db = getDB();

$order_number =
$_POST['order_number']
?? '';

$product_id =
$_POST['product_id']
?? '';

$name =
$_POST['name']
?? '';

$rating =
$_POST['rating']
?? 5;

$review =
$_POST['review']
?? '';

if(

empty($order_number) ||
empty($product_id) ||
empty($review)

){

echo json_encode([

"success" => false
]);

exit;
}

$order_number =
$db->real_escape_string(
$order_number
);

$check =
$db->query("SELECT *

FROM orders

WHERE order_number='$order_number'

AND status='Delivered'

LIMIT 1");

if($check->num_rows == 0){

echo json_encode([

"success" => false,

"message" =>
"Order not delivered"
]);

exit;
}

$order =
$check->fetch_assoc();

$order_id =
$order['id'];

$db->query("INSERT INTO reviews(

order_id,
product_id,
name,
rating,
review

) VALUES(

'$order_id',
'$product_id',
'$name',
'$rating',
'$review'
)");

echo json_encode([

"success" => true
]);