<?php

include("../db.php");

$target_user = $_POST["target_uid"];
$target_name=$_POST["target_name"];
$target_number=$_POST["target_number"];
$target_type = $_POST["target_type"];

$sender_uid=$_POST["sender_uid"];
$sender_name = $_POST["sender_name"];
$sender_phone = $_POST["sender_phone"];
$sender_type = $_POST["sender_type"];

$msg = $_POST["msg"];

if(!isset($myObj)) { $myObj = new stdClass(); }

$allowed = 0;
$notification_controls = $db->getRow("SELECT * FROM notification_control WHERE id = '1' ");
if ($notification_controls != "" && isset($notification_controls['chat'])){
    $allowed = $notification_controls['chat'];
}

if ($allowed == 0){

    $sql = "";
    if($target_type=='S'){
        $sql = "SELECT name,photo,fcm_token FROM student WHERE idno='$target_user'";
    }elseif ($target_type=='T'){
        $sql = "SELECT name,photo,fcm_token FROM staff WHERE idno='$target_user'";
    }elseif ($target_type=='A'){
        $sql = "SELECT name,photo,fcm_token FROM admin WHERE id='$target_user'";
    }


//$sql = "SELECT name,firebase_device_token,img FROM user_master WHERE firebase_id='$target_user'";
// $retval = mysql_query($sql);

    $retval = $db->getRows($sql);

    if($retval){

        $fb_token="";
        $target_user_name="";
        $user_image = "";

        // while($row = mysql_fetch_assoc($retval)) {
        foreach($retval as $row){
            $fb_token = $row["fcm_token"];
            $target_user_name = $row["name"];
            $user_image = $row["photo"];
            //}

            // Enabling error reporting
            error_reporting(-1);
            ini_set('display_errors', 'On');


            require_once  __DIR__.'/firebase/firebase.php';
            require_once  __DIR__.'/firebase/push.php';

            $firebase = new Firebase();
            $push = new Push();

            // optional payload
            $payload = array();


            $payload['target_uid'] = $target_user;
            $payload['target_name'] = $target_name;
            $payload['target_number'] = $target_number;

            $payload['sender_uid'] = $sender_uid;
            $payload['sender_name'] = $sender_name;
            $payload['sender_phone'] = $sender_phone;

            $payload['user_image'] = "$user_image";

            $payload['chat_type'] = "0";

            //send type
            $payload['target_type'] = $target_type;
            $payload['sender_type'] = $sender_type;



            // whether to include to image or not
            $include_image = TRUE;

            $push->setTitle($sender_name);
            $push->setMessage($msg);
            if ($include_image) {
                //$push->setImage('https://api.androidhive.info/images/minion.jpg');
                $push->setImage('1');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);


            $json = '';
            $response = '';

            /*if ($push_type == 'topic') {
                $json = $push->getPush();
                $response = $firebase->sendToTopic('global', $json);
            } else if ($push_type == 'individual') {
                $json = $push->getPush();
                $regId = isset($_GET['regId']) ? $_GET['regId'] : '';
                $response = $firebase->send($regId, $json);
            }*/


            $json = $push->getPush();
            // $response = $firebase->send($fb_token, $json);




            $fields = array(
                'to' => $fb_token,
                'data' => $json,
            );



            $SERVER_KEY=$db->getVal("select firebase_server_key from tbl_info where id='1'");


            // Set POST variables
            $url = 'https://fcm.googleapis.com/fcm/send';

            $headers = array(
                'Authorization: key=' . $SERVER_KEY,
                'Content-Type: application/json'
            );
            // Open connection
            $ch = curl_init();

            // Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // Disabling SSL Certificate support temporarly
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

            // Execute post
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);

            // return $result;

































            $myObj->status = "1";
            $myObj->res = json_decode($result);

        }
    }else{
        $myObj->status = "0";
    }

    echo json_encode($myObj);

}