<?php 
require 'db.php';
header( 'Content-Type: text/html; charset=utf-8' ); 
$data = json_decode(file_get_contents('php://input'), true);

$oid = $data['order_id'];
$status = $data['status'];
$rid = $data['deli_code'];

if ($oid =='' or $status =='' or $rid == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
}

else 
{
    $oid = strip_tags(mysqli_real_escape_string($con,$oid));
    $rid = strip_tags(mysqli_real_escape_string($con,$rid));
    $status = strip_tags(mysqli_real_escape_string($con,$status));
    $check = $con->query("select *  from orders where rid='$rid' and order_id='$oid'")->num_rows;
    if($check != 0)
    {
        if($status == 'accept')
        {
        	$con->query("update orders set r_status='accepted' where order_id='$oid'");
        	$con->query("update assist_settings set accepted_orders = accepted_orders+1 where deli_code='$rid'");
        	$checks = $con->query("select * from orders where order_id=".$oid."")->fetch_assoc();
        	
        	$shop_id = $checks['shop_id'];
        	$shop_code = $checks['shop_code'];
        	
        	$sel = $con->query("select one_hash,one_key from shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
            if($row = $sel->fetch_assoc())
            {
                $onehash = $row['one_hash'];
                $onekey = $row['one_key'];
            }
            
            $usr = $con->query("select * from user where id=".$checks['uid']." and shop_id='$shop_id'")->fetch_assoc();
            $title = "Dear ".$usr['name'].",";
        	
        	$content = array(
                "en" => 'Your Delivery Assist has Assigned'
            );
            
            $fields = array(
            'app_id' => $onekey,
            'included_segments' =>  array("Active Users"),
            'filters' => array(array('field' => 'tag', 'key' => 'userId', 'relation' => '=', 'value' => $checks['uid'])),
            'headings' => array("en"=>"$title"),
            'contents' => $content
            );
            
            $fields = json_encode($fields);
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array('Content-Type: application/json; charset=utf-8',
            'Authorization: Basic '.$onehash));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        
            $response = curl_exec($ch);
            curl_close($ch);
            
            $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Accepted!");    
        	
        }
        else if($status == 'reject')
        {
        	$con->query("update orders set r_status='rejected', rid=0 where order_id='$oid'");
        	$con->query("update assist_settings set rejected_orders = rejected_orders+1 where deli_code='$rid'");
        	$returnArr = array("ResponseCode"=>"200","Result"=>"false","ResponseMsg"=>"Order Rejected!");    
        }
        else if($status == 'taken')
        {
        	$con->query("update orders set r_reach='1',status='processing' where order_id='$oid'");
        	$checks = $con->query("select * from orders where order_id=".$oid."")->fetch_assoc();
        	
        	$shop_id = $checks['shop_id'];
        	$shop_code = $checks['shop_code'];
        	
        	$sel = $con->query("select one_hash,one_key from shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
            if($row = $sel->fetch_assoc())
            {
                $onehash = $row['one_hash'];
                $onekey = $row['one_key'];
            }
            
            $usr = $con->query("select * from user where id=".$checks['uid']." and shop_id='$shop_id'")->fetch_assoc();
            $title = "Dear ".$usr['name'].",";
        	
        	$content = array(
                "en" => 'Your Food is On the Way'
            );
            
            $fields = array(
            'app_id' => $onekey,
            'included_segments' =>  array("Active Users"),
            'filters' => array(array('field' => 'tag', 'key' => 'userId', 'relation' => '=', 'value' => $checks['uid'])),
            'headings' => array("en"=>"$title"),
            'contents' => $content
            );
            
            $fields = json_encode($fields);
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array('Content-Type: application/json; charset=utf-8',
            'Authorization: Basic '.$onehash));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        
            $response = curl_exec($ch);
            curl_close($ch);
            
        	$returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Complete Order Quickly");
        }
        else if($status == 'cancel')
        {
        	$comment = $data['comment'];
        	$con->query("update orders set r_status='undelivered',status='processing',comment='$comment' where order_id='$oid'");
        	
        	$returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Cancelled!");
        }
        else if($status == 'complete')
        {
            $ddate = $data['d_date'];
            $dtime = $data['d_time'];
        	$con->query("update orders set r_status='delivered',o_status='4', status='completed', d_date='$ddate',d_time='$dtime' where order_id='$oid'");
        	$con->query("update assist_settings set completed_orders = completed_orders + 1 where deli_code='$rid'");
        	
            $checks = $con->query("select * from orders where order_id=".$oid."")->fetch_assoc();
        	
        	$shop_id = $checks['shop_id'];
        	$shop_code = $checks['shop_code'];
        	
        	$sel = $con->query("select one_hash,one_key from shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
            if($row = $sel->fetch_assoc())
            {
                $onehash = $row['one_hash'];
                $onekey = $row['one_key'];
            }
            
            $usr = $con->query("select * from user where id=".$checks['uid']." and shop_id='$shop_id'")->fetch_assoc();
            $title = "Dear ".$usr['name'].",";
            
        	$content = array(
                "en" => 'Your Order Has Been Delivered.'
            );
            
            $fields = array(
            'app_id' => $onekey,
            'included_segments' =>  array("Active Users"),
            'filters' => array(array('field' => 'tag', 'key' => 'userId', 'relation' => '=', 'value' => $checks['uid'])),
            'headings' => array("en"=>"$title"),
            'contents' => $content
            );
            $fields = json_encode($fields);
            
             
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array('Content-Type: application/json; charset=utf-8',
            'Authorization: Basic '.$onehash));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
             
            $response = curl_exec($ch);
            curl_close($ch);
        
        	$returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Completed!");    
    }
        else 
        {
        	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
        }
    }
    else 
    {
    	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Sorry This Order Assign to Other Rider Or Cancelled!");
    }
}
echo json_encode($returnArr);
mysqli_close($con);
?>