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


$requestid= $_GET['requestid'];     
$medicinename= $_GET['medicinename'];
$days= $_GET['days'];
$dailydose= $_GET['dailydose'];
$note= $_GET['note'];
$nextAppointment=$_GET['nextappointment'];

$resultdata =$con->query("INSERT INTO `perception`(`requestid`, `medicineName`, `dailyDose`, `days`, `note`,`nextAppointment`, `time`) VALUES ('$requestid','$medicinename','$dailydose','$days','$note','$nextAppointment',NOW())");

if($resultdata > 0){

$resultrequest =$con->query("select userId from `request` where id=$requestid");
while($rowrequest = mysqli_fetch_array($resultrequest)){
    $userId=$rowrequest['userId'];    
$resultdata =$con->query("select fcmtoken from `users` where id=$userId");
while($row = mysqli_fetch_array($resultdata)){
    $fcmtoken=$row['fcmtoken'];
    

    
$data = array("to" => $fcmtoken,
                  "notification" => array( "code"=> $perceptioncodefcm,"title" => "$titlefcm", "body" => $perceptionmsg ,"icon" => $logofcm),
                  "data"=>array("code"=> $perceptioncodefcm, "key1"=> $requestid,"key2"=> $perceptionmsg )
                  );                                                                    
            $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";
}
else{
$j->status  = "104";
$j->message = "wrong";
}


echo json_encode($j);
?>
?>