<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{

$orderid = mysqli_real_escape_string($con, trim($request->value));
$delicode = mysqli_real_escape_string($con, trim($request->status));
$admin = mysqli_real_escape_string($con, trim($request->admin));

$sql = "SELECT * FROM orders where order_id = '$orderid'";
$result = mysqli_query($con,$sql);
if($rows = mysqli_fetch_assoc($result))
{
	$status = $rows['status'];
	$userid = $rows['uid'];
}

if($status == "preparing" && $delicode != null)
{
    if($delicode != null && $orderid != null)
    {
        $sql = "UPDATE orders set admin = '$admin', status = 'processing', o_status='3' , rid='$delicode' , r_status = 'pending'  where order_id = '$orderid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $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='$userid' and shop_id='$shop_id'")->fetch_assoc();
            $title = "Dear ".$usr['name'].",";
        	
        	$content = array(
                "en" => 'Your Order Has Been Accepted.'
            );
            
            $fields = array(
            'app_id' => $onekey,
            'included_segments' =>  array("Active Users"),
            'filters' => array(array('field' => 'tag', 'key' => 'userId', 'relation' => '=', 'value' => $userid)),
            '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);
            
            $rows = array('status'=>true,'message' => 'Assist Assigned');
            echo json_encode($rows);
        }
        else
        {
            $rows = array('status'=>false,'message' => 'Order Missing');
            echo json_encode($rows);
        }
    }
    else
    {
        $rows = array('status'=>false,'message' => 'Assist Missing');
        echo json_encode($rows);  
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Order Status Error');
    echo json_encode($rows); 
}
}

?>