<?php
include '../../conn.php';
$reqid=$_GET['requestid'];
$type=$_GET['selectappointmenttype'];

$resultrequest =$con->query("select * from `request` where  id='$reqid'");
 while($requestrow=mysqli_fetch_array($resultrequest))
    {
     $latitude=$requestrow['latitude'];
     $longitude=$requestrow['longitude'];
    }
if($type == 'clinic' || $type == 'home'){
    $ssqqlldistance="SELECT *, ( 3959 * acos( cos( radians('$latitude') ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians('$longitude') ) + sin( radians('$latitude') ) * sin(radians(`latitude`)) ) ) AS distance FROM doctors    HAVING distance < 12222200 ORDER BY distance ";
    $resultdatadistance =$con->query($ssqqlldistance);
 
    $result=array();
   while($rowdistance=mysqli_fetch_array($resultdatadistance))
    {
     $doctorid=null;   
     $distance=null;
     $distance1=null;
     $distance1=$rowdistance['distance'] * 1.609344;
     $doctorid=$rowdistance['id'];
     $distance=round($distance1, 2);


    $ssqqll="SELECT * FROM doctors where raduis >= $distance AND id='$doctorid' ORDER BY rating desc";
    
    $resultdata =$con->query($ssqqll);


   while($row=mysqli_fetch_array($resultdata))
    {
            
     $id=$row['id'];
     
     $doctorspecial =$con->query("select * from `doctorServices`  where doctorId='$id'");
    $veterinarySpecialtiesarr=null;
    while($rowdoctorspecial=mysqli_fetch_array($doctorspecial))
    {

     $idsp=$rowdoctorspecial['specialtiesId'];
    
    $veterinarySpecialties =$con->query("select * from `veterinarySpecialties`  where id='$idsp'");

    while($rowveterinarySpecialties=mysqli_fetch_array($veterinarySpecialties))
    {
    
            $veterinarySpecialtiesarr=$veterinarySpecialtiesarr.','.$rowveterinarySpecialties['name'];
     
    }
    }
  
  

  
   array_push($result,
       array(
            'id'=>$row['id'],
            'image'=>$row['image'],
            'name'=>$row['name'],
            'type'=>$row['type'],
            'rating'=>$row['rating'],
            'workingTime'=>$row['workingTime'],
            'qualification'=>$row['qualification'],
            'veterinarySpecialties'=>$veterinarySpecialtiesarr,
            'clinicVisit'=>$row['clinicVisit'],
            'homeVisit'=>$row['homeVisit'],
            'nightCharge'=>$row['nightCharge'],
            'distance'=>$distance,
             'partner'=>$row['partner'],    
           	));
    


}
    }
    echo json_encode(array("result"=>$result));
}
else{
     $ssqqll="SELECT *, ( 3959 * acos( cos( radians('$latitude') ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians('$longitude') ) + sin( radians('$latitude') ) * sin(radians(`latitude`)) ) ) AS distance FROM doctors    ORDER BY distance ";
    
    
    // $ssqqll="SELECT * FROM doctors  ORDER BY rating desc";


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

   while($row=mysqli_fetch_array($resultdata))
    {
        $distance1=$row['distance'] ;
    $distance1=$distance1 * 1.609344;
     $distance=round($distance1, 2);
     
     $id=$row['id'];
     
     $doctorspecial =$con->query("select * from `doctorServices`  where doctorId='$id'");
    $veterinarySpecialtiesarr=null;
    while($rowdoctorspecial=mysqli_fetch_array($doctorspecial))
    {

     $idsp=$rowdoctorspecial['specialtiesId'];
    
    $veterinarySpecialties =$con->query("select * from `veterinarySpecialties`  where id='$idsp'");

    while($rowveterinarySpecialties=mysqli_fetch_array($veterinarySpecialties))
    {
    
            $veterinarySpecialtiesarr=$veterinarySpecialtiesarr.','.$rowveterinarySpecialties['name'];
     
    }
    }
  
  
  
  
   array_push($result,
       array(
            'id'=>$row['id'],
            'image'=>$row['image'],
            'name'=>$row['name'],
            'type'=>$row['type'],
            'rating'=>$row['rating'],
            'workingTime'=>$row['workingTime'],
            'qualification'=>$row['qualification'],
            'veterinarySpecialties'=>$veterinarySpecialtiesarr,
            'clinicVisit'=>$row['clinicVisit'],
            'homeVisit'=>$row['homeVisit'],
            'nightCharge'=>$row['nightCharge'],
           'distance' => $distance,
              'partner'=>$row['partner'],    
           	));
    }

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