<?php
include "../connection/config.php";
include "config.php";
extract($_REQUEST);
if(rows(query("select sn from users where mobile='$user' and session ='$session'")) == 0){
    $data['msg'] = "You are not authrized to use this";
   
    $data['active'] = "0";
    
    echo json_encode($data);
    return;
} else {
          
    $dd = query("select active from users where mobile='$user'");
    $d = fetch($dd);
    $data['active'] = "1";
}

if(rows(query("select sn from users where mobile='$user' and transfer_points_status ='0'")) > 0){
    $data['msg'] = "You are not authrized to use this";
   
    $data['active'] = "0";
    
    echo json_encode($data);
    return;
}

if(rows(query("select sn from users where mobile='$mobile'"))>0){
  
  $check = query("select wallet from users where mobile='$user' AND wallet >= $amount");

  if(rows($check) > 0){
    
    query("UPDATE users set wallet=wallet-$amount where mobile='$user'");
    query("UPDATE users set wallet=wallet+$amount where mobile='$mobile'");
    query("INSERT INTO `transactions`(`user`, `amount`, `type`, `remark`, `owner`, `created_at`, `game_id`, `batch_id`) VALUES ('$user','$amount','0','Transfer to $mobile','user','$stamp','0','0')");
    query("INSERT INTO `transactions`(`user`, `amount`, `type`, `remark`, `owner`, `created_at`, `game_id`, `batch_id`) VALUES ('$mobile','$amount','0','Received from $user','user','$stamp','0','0')");
     $data['success']  = "1";
 $data['msg']  = "Transfer complete";
    
    
    $user_details=mysqli_query($con,"select sn,tocken from users where mobile='$mobile'");
        if(mysqli_num_rows($user_details)>0)
        {
            $row=mysqli_fetch_assoc($user_details);
            if($row['tocken']!='')
            {
                $sn=$row['sn'];
                $msgsend='Congratulation receive '.$amount.' point';
                $msgsend.=' 🤑🤩🥳😍';
                $_REQUEST['token']=$row['tocken'];
      
              $msg = array
              (
        	   'body' 	=> $msgsend,
        	   'title'	=> 'PLUS LIFE POINT',
        	   'vibrate' => 1,
        	   'sound' => 1,
        	   'image'=>'https://www.digitalkee.com/online_test/application_admin/assets/product/2096198.jpg'
               );
            	$fields = array
            			(
            				'to'		=> $_REQUEST['token'],
            				'notification'	=> $msg,
            				
            			);
            	$headers = array
            			(
            				//'Authorization: key=' . API_ACCESS_KEY,
            				'Authorization: key= AAAAj2wUFTg:APA91bF7grm_Se13eOkBKGEHa1_bECRlGolYgYLQWW_of1bKvB_gvnFfSdPVwIWmTRKrxhKoiOjtMDQZuo-oDHbjN8Uou9vgQnMeIfFH_c-rk7l8vc9nc-nGL9DTor_Imt6hJs-VgeOZ',
            				'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_SSL_VERIFYPEER, false );
            		curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            		$result = curl_exec($ch );
                	//echo $result; die();
            		
            		curl_close( $ch );
            }
        }
        
    $get_bal = fetch(query("select wallet from users where mobile='$mobile'"));
    
    $data['wallet'] = $get_bal['wallet'];
  } else {
    
    $data['success']  = "0";
 $data['msg']  = "You don't have enough wallet balance"; 
  }
  
} else {
 $data['success']  = "0";
 $data['msg']  = "Mobile number is not registered with us";
  
}

echo json_encode($data);