<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

include 'db/config.php';


$start = $_POST['start'];
$finish = $_POST['finish'];

$msg_id=$_POST['msg_id'];
$user_id=$_POST['user_id'];
$auth = $_POST['auth'];




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


$start=stripslashes($start);
$start=mysqli_escape_string($con, $start);

$finish=stripslashes($finish);
$finish=mysqli_escape_string($con, $finish);


$msg_id=stripslashes($msg_id);
$msg_id=mysqli_escape_string($con, $msg_id);

$user_id=stripslashes($user_id);
$user_id=mysqli_escape_string($con, $user_id);


$auth=stripslashes($auth);
$auth = mysqli_escape_string($con, $auth);




$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 == 0)
{
        $suc = "User Does Not Exist"; 
         $status = false; 

}
else
{

$data = array();
$sql=$con->query("SELECT * FROM messages WHERE msg_id='$msg_id' AND recipient_id = '$user_id' OR msg_id='$msg_id' AND sender_id = '$user_id'  ORDER BY id ASC") or die("Error2 : ". mysqli_error($con));


  while($rows=mysqli_fetch_assoc($sql))
   {

 $data[] = [ 
        'id' => $rows['id'],
        'message_id' => $rows['msg_id'],
        'recipient' => $rows['recipient'],
        'recipient_id' => $rows['recipient_id'],
        'sender' => $rows['sender'],
        'sender_id' => $rows['sender_id'],
        'message' => $rows['message'],
        'date_t' => $rows['date_t'],
        'time_t' => $rows['time_t'],

       
    ];
    $suc="Success";
 
   }
   if($data == null)
   {
      $suc="No Messages";
       $data = ''; 
   }

$sqlx="UPDATE messages SET status = 'Read'  WHERE msg_id = '$msg_id' AND recipient_id = '$user_id'";
$resultx=$con->query($sqlx) or die ("error2: Server error ".mysqli_error($con));

}

$con->close();

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




/*session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

include 'db/config.php';


$start = $_POST['start'];
$finish = $_POST['finish'];

$msg_id=$_POST['msg_id'];
$user_id=$_POST['user_id'];
$auth = $_POST['auth'];




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


$start=stripslashes($start);
$start=mysqli_escape_string($con, $start);

$finish=stripslashes($finish);
$finish=mysqli_escape_string($con, $finish);


$msg_id=stripslashes($msg_id);
$msg_id=mysqli_escape_string($con, $msg_id);

$user_id=stripslashes($user_id);
$user_id=mysqli_escape_string($con, $user_id);


$auth=stripslashes($auth);
$auth = mysqli_escape_string($con, $auth);




$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 == 0)
{
        $suc = "User Does Not Exist"; 
         $status = false; 

}
else
{

$data = array();
$sql=$con->query("SELECT * FROM messages WHERE msg_id='$msg_id' AND recipient_id = '$user_id' OR msg_id='$msg_id' AND sender_id = '$user_id'  ORDER BY id ASC") or die("Error2 : ". mysqli_error($con));


  while($rows=mysqli_fetch_assoc($sql))
   {

 $data[] = [ 
        'id' => $rows['id'],
        'message_id' => $rows['msg_id'],
        'recipient' => $rows['recipient'],
        'recipient_id' => $rows['recipient_id'],
        'sender' => $rows['sender'],
        'sender_id' => $rows['sender_id'],
        'message' => $rows['message'],
        'date_t' => $rows['date_t'],
        'time_t' => $rows['time_t'],

       
    ];
    $suc="Success";
 
   }
   if($data == null)
   {
      $suc="No Messages";
       $data = ''; 
   }

$sqlx="UPDATE messages SET status = 'Read'  WHERE msg_id = '$msg_id' AND recipient_id = '$user_id'";
$resultx=$con->query($sqlx) or die ("error2: Server error ".mysqli_error($con));

}

$con->close();

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


*/



?>
