<?php 
include '../../conn.php';
include 'message.php'; 
        $userid=$_GET['userid'];
        
        $myanimalselect=$_GET['myanimalselect'];
        $petname=$_GET['petname'];
        $myanimalselecttype=$_GET['myanimalselecttype'];
        $description=$_GET['description'];
        $age=$_GET['age'];
        $gender=$_GET['gender'];
        
        $latitude=$_GET['latitude'];
        $longitude=$_GET['longitude'];
        $address=$_GET['address'];
        $selectappointmenttype=$_GET['selectappointmenttype'];
        $ownername=$_GET['ownername'];
        $owneraddress=$_GET['owneraddress'];
             
        $lat1= str_replace('{"latitude":',"",$latitude);	
        $lat2= str_replace('}',"",$lat1);
        
        $lng1= str_replace('{"longitude":',"",$longitude);	
        $lng2= str_replace('}',"",$lng1);
        
        
        $resultdata =$con->query("INSERT INTO `request`( `userId`, `myAnimalId`, `animalName`,`animalType`, `gender`,`age`,`description`, `address`, `latitude`, `longitude`,`status`,`bookingType`,`time`,`ownername`,`owneraddress`) VALUES
                                                        ('$userid','$myanimalselect','$petname','$myanimalselecttype','$gender','$age','$description','$address','$lat2','$lng2','1','$selectappointmenttype',NOW(),'$ownername','$owneraddress')");
if($resultdata > 0){
    $resultrequest =$con->query("select * from `request` where  userId='$userid' order by id desc limit 1");
 while($requestrow=mysqli_fetch_array($resultrequest))
    {
     $reqid=$requestrow['id'];
     
    }
    
$ssqqll="SELECT *  FROM doctors";
$resultdatafcm =$con->query($ssqqll);
while($row=mysqli_fetch_array($resultdatafcm))
    {
     $registrationIds=$row['fcmToken'];
      $body=array("code"=> $codefcm,"message"=>$addrequestfcm);
      $data = array("to" => $registrationIds,
                  "notification" => array( "title" => "$titlefcm", "body" => $addrequestfcm,"icon" => $logofcm),
                  "data"=>array("code"=> $codefcm, "key1"=> $reqid,"key2"=> $addrequestfcm )
                  );                                                                    
            $data_string = json_encode($data); 
            
             "The Json Data : ".$data_string; 
            
            $headers = array
            (
                 'Authorization: key=' . API_ACCESS_KEY, 
                 'Content-Type: application/json'
            );                                                                                 
                                                                                                                                 
            $ch = curl_init();  
            
            curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );                                                                  
            curl_setopt( $ch,CURLOPT_POST, true );  
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, $data_string);                                                                  
                                                                                                                                 
            $result = curl_exec($ch);
    }
            
$j->status  = "101";
$j->message = "Success";
$j->requestid=$reqid;
}
else{
$j->status  = "104";
$j->message = "wrong";
}


echo json_encode($j);
?>