<?php
include '../../conn.php';

$userid=$_GET['userid'];

 $doctorsuserresult = $con->query("select * from `doctors`  where id='$userid'");
        
            while($rowuserdoctors=mysqli_fetch_array($doctorsuserresult))
            {
        
             $latitude=$rowuserdoctors['latitude'];
             $longitude=$rowuserdoctors['longitude'];
             $raduis=$rowuserdoctors['raduis'];
            }
 $ssqqll="SELECT *, ( 3959 * acos( cos( radians('$latitude') ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians('$longitude') ) + sin( radians('$latitude') ) * sin(radians(`latitude`)) ) ) AS distance FROM request where  status='1' HAVING distance < $raduis  order by id desc";

$resultdata =$con->query($ssqqll);


$result=array();

   while($row=mysqli_fetch_array($resultdata))
    {
     $distance=$row['distance'];
     $myAnimalId=$row['myAnimalId'];
     $animaltype=$row['animalType'];
      $senderId=$row['userId'];
         $phone=null;
         $username=null;
     
         $userresult = $con->query("select * from `users` where id='$senderId'");
        
            while($rowuser=mysqli_fetch_array($userresult))
            {
        
             $phone=$rowuser['phone'];
              $username=$rowuser['name'];
            }
            
            
             $myAnimalresult =$con->query("select * from `myAnimal`  where id='$myAnimalId'");
        
            while($rowmyAnimal=mysqli_fetch_array($myAnimalresult))
            {
        
             $animalname=$rowmyAnimal['name'];
            }
   
   
            $animaltyperesult =$con->query("select * from `animal`  where id='$animaltype'");
        
            while($rowanimaltyperesult=mysqli_fetch_array($animaltyperesult))
            {
            
                 $name=$rowanimaltyperesult['name'];
                 $image=$rowanimaltyperesult['image'];
             
            }
    
  
  
  $kmdistance=$row['distance'] * 1.60934;
  
  array_push($result,
      array(
            'id'=>$row['id'],
            'userId'=>$row['userId'],
            'username'=>$username,
            'userphone'=>$phone,
            'image'=>$image,
            'name'=>$animalname,
            'animaltype'=>$name,
            'gender'=>$row['gender'],
            'address'=>$row['address'],
            'description'=>$row['description'],
            'status' =>$row['status'],
            'age'=>$row['age'],
            'latitude'=>$row['latitude'],
            'longitude'=>$row['longitude'],
             'distance'=>number_format($kmdistance ,2)
                
          	));
    }

 echo json_encode(array("result"=>$result));

   
?>