<?php
session_start();

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['user_id']) && isset($_POST['provider_id']))
{
require 'config.php';


function check_input($par) {
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}



$rating=check_input($_POST['rating']);
$user_id=check_input($_POST['user_id']);
$provider_id=check_input($_POST['provider_id']);
$review=check_input($_POST['review']);



$sql="SELECT * FROM users WHERE  user_id='$user_id'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if($count == 1)
{
$rows = mysqli_fetch_array($result);
$fname = $rows['firstname'];
$lname = $rows['lastname'];

$fulname = $fname." ".$lname;


date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y"); 
  $time_t = date("H:i"); 
  $date_t2 = date("d-m-Y");


  $sqlx=$con->query("INSERT INTO reviews  (user_id, provider_id, fulname, rating, review, date_t, time_t, date_t2, status) 
      VALUES ('$user_id', '$provider_id', '$fulname',  '$rating', '$review', '$date_t', '$time_t', '$date_t2', 'New')") or die("Error: ".mysqli_error($con));
      
      
      include  'src/cal_rating.php';

      
      if($sqlx)
     {
     $status = true;
     $suc = "Successful";
     }
     else
     {
   
   $status = false;
   $suc = "Not Successful";
  
     }
}
else
{
   $status = false;
   $suc = "Invalide User ID";
      
}

$con->close();

$output = array('status' => $status, 'success'=>$suc, 'user_id' => $user_id, 'provider_id' => $provider_id, 'rating' => $rating, 'review' => $review);
echo json_encode($output); //output JSON data
}
?>
