<?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']))
{
require 'config.php';


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


$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['user_id']);
$comment_id=check_input($_POST['comment_id']);
$post_id=check_input($_POST['post_id']);
$post_by=check_input($_POST['post_by']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$comment_id=mysqli_escape_string($con, $comment_id);
$post_id=mysqli_escape_string($con, $post_id);
$post_by=mysqli_escape_string($con, $post_by);




$sql="SELECT * FROM users WHERE user_id = '$user_id' AND auth = '$auth'";
$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);
$phone = $rows['phone'];
$email = $rows['email'];
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];
//$posted_by = $firstname." ".$lastname;

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  



$sql2="SELECT * FROM comments WHERE post_id = '$post_id' AND user_id = '$user_id' AND comment_id = '$comment_id'";
$result2=$con->query($sql2) or die ("error: Server error ".mysqli_error($con));
$count2=mysqli_num_rows($result2);

if ($count2 == 1)
{
$rows2 = mysqli_fetch_array($result2);
$likes = $rows2['likes'];
$comments = $rows2['comments'];
//$views = $rows2['views'];
//$views = $views+5;
$comments = $comments-1;
$notify = $firstname." ".$lastname." commented on your post.";
$firstnamex = $rows2['firstname'];
$lastnamex = $rows2['lastname'];
$name = $firstnamex." ".$lastnamex;
$pro_pic= $p_url."/profiles/".$user_id.".png?time=".time();


$sqlx="DELETE  FROM comments WHERE post_id = '$post_id' AND user_id = '$user_id' AND comment_id = '$comment_id'";
$resultx=$con->query($sqlx) or die ("error: Server error ".mysqli_error($con));

$sqlz=$con->query("UPDATE timeline SET  comments = '$comments' WHERE post_id = '$post_id' AND user_id = '$posted_by'");


      
  
if($sqlx)
{

$suc = "Successful"; 
$status = true; 

}
}
else
{
         $suc = "Comment Does Not Exist"; 
         $status = false; 


}

}
else
{
         $suc = "User Does Not Exist"; 
         $status = false; 


}

$con->close();

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