<?php
ob_start();
include("../connect.php");
$response = array();
if(isset($_POST['transaction_id']) && isset($_POST['transaction_status']))
{  
   $t_id=$_POST['transaction_id'];
   $status=$_POST['transaction_status'];
   
   date_default_timezone_set('Asia/Calcutta');				
   $cdate = date("Y-m-d");
   $ctime = date('H:i:sa');
   
   $txn_query = mysqli_query($con,"select * from tbl_transaction where transaction_id='$t_id'");
   $fetch_data_txn= mysqli_fetch_assoc($txn_query);
   $bill_no=$fetch_data_txn['customer_transaction_id'];
   $customer_id=$fetch_data_txn['customer_id'];

   $sel_cust1=mysqli_query($con,"select * from tbl_master_customer where customer_id='$customer_id'");
   $fetch_data_cust1 = mysqli_fetch_assoc($sel_cust1);
   $customer_name = $fetch_data_cust1['customer_name'];

   if($status=='Accept')
   {
	 $update_status = mysqli_query($con,"update tbl_transaction set transaction_status='Accept',accept_date='$cdate',accept_time='$ctime' where transaction_id='$t_id'");
	 
	 $message="Dear, $customer_name Your Order No. $bill_no Has Been Successfully Placed.";
	 $success="Order No. $bill_no Has Been Successfully Accepted.";
   }
   else
   {
	 $update_status = mysqli_query($con,"update tbl_transaction set transaction_status='Reject', cancel_date='$cdate', cancel_time='$ctime' where transaction_id='$t_id'");
	 
	 $message=" Dear, $customer_name Your Order No. $bill_no Has Been Rejected Contact To Tarkashcorp For Inquiry.";
	 
	 $success="Order No. $bill_no Has Been Rejected.";
   }
   if($update_status)
   {  
      
	$title="Tarkashcorp Management";

	$flag=0;
	$FIREBASE_API_KEY="AAAAeuYrumI:APA91bFlnBgFQvjGUN0qe_iDJyJN5U8TX_yuQOkkoA0Lqdwfea-Y3kJrOAFH0SkD6zv74xnVrGUgQW47F9m6xW631f6dkdbKmoqO4yYyX5p4nQuGmV203LTMoeL-ECINmvPfRnoJruZ8";
	$url = 'https://fcm.googleapis.com/fcm/send';
	$headers = array( 'Authorization: key=' . $FIREBASE_API_KEY,'Content-Type: application/json');
						
	$sel_cust=mysqli_query($con,"select * from tbl_master_customer where customer_id='$customer_id'");
	$ch = curl_init ();
	while($row=mysqli_fetch_array($sel_cust))
	{
		$key=$row['device_id'];
		
		$fields = array('to' => $key, 'notification' =>array('title'=>$title,'body'=>$message));                         //print_r($fields);
		$payload = json_encode ( $fields );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		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_IPRESOLVE,CURL_IPRESOLVE_V4);
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $payload);
	    $result = curl_exec ( $ch );
		$flag=1;
	}
	
	if($flag=='1'){
	$response['success']=1;	
	$response['details']=$success; }
   }
  else
  {
	  $response['success']=0;	
	  $response['details']="Data Fail"; 	
  }
}	
else
{	
	$response['success']=0;
	$response['details']="Required fields missing";
}
echo json_encode($response);
?>
