<?php    
ob_start();
include("connect.php");
session_start();
$ver = $_SESSION["user_id"];
$ver = $_SESSION['distributor_user_id'];
$user_role= $_SESSION['user_role'];

$id=$_GET['id']; 
$status=$_GET['status']; 

date_default_timezone_set('Asia/Calcutta');
$cdate  = date("Y-m-d");

$txn_query = mysqli_query($con,"select * from tbl_transaction where transaction_id='$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')
{
	$uq1=mysqli_query($con,"UPDATE tbl_transaction SET transaction_status='Accept',accept_date='$cdate' where transaction_id='$id'");
	
	$message=" Dear, $customer_name Your Order No. $bill_no Has Been Successfully Placed.";
}
else
{
	$uq1=mysqli_query($con,"UPDATE tbl_transaction SET transaction_status='Reject',cancel_date='$cdate' where transaction_id='$id'");
	
	$message=" Dear, $customer_name Your Order No. $bill_no Has Been Rejected Contact To Tarkashcorp For Inquiry.";
}
if($uq1)
{  

   $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'");
	$num=mysql_num_rows($sel_cust);
	$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_SSL_VERIFYHOST,0);
		curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $payload);
		echo $result = curl_exec ( $ch );
		$flag=1;
	}
}

if($uq1)
{
	header('location:pending-orders.php'); 
}
?>