<?php

require 'db.php';

$data = json_decode(file_get_contents('php://input'), true);
if($data['shop_id'] == '' || $data['shop_code'] == '')
{ 
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    $shop_id =  strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code =  strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    $orderid =  strip_tags(mysqli_real_escape_string($con,$data['order_id']));
    $s_status =  strip_tags(mysqli_real_escape_string($con,$data['status']));
    
    $sql = "SELECT * FROM orders where order_id = '$orderid' and shop_id = '$shop_id' and shop_code = '$shop_code'";
    $result = mysqli_query($con,$sql);
    if($rows = mysqli_fetch_assoc($result))
    {
    	$status = $rows['status'];
    	$userid = $rows['uid'];
    }
    
    if($status == "pending" && $s_status == "accept")
    {
        $num = mt_rand(1000,9999); 
        $sql = "UPDATE orders set status = 'preparing', r_otp = '$num' , o_status = '1' where order_id = '$orderid' and shop_id = '$shop_id' and shop_code = '$shop_code'";
        $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.'//mesaj burasi
            );
            $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("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Accepted");
            echo json_encode($rows);
        }
        else
        {
            $rows = array("ResponseCode"=>"201","Result"=>"false","ResponseMsg"=>"Order Missing");
            echo json_encode($rows);
        }
    }
    
    else if($status == "pending" && $s_status != "accept")
    {
        $sql = "UPDATE orders set status = 'cancelled', o_status = '-1' ,comment= '$s_status' where order_id = '$orderid' and shop_id = '$shop_id' and shop_code = '$shop_code'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Rejected");
            echo json_encode($rows);
        }
        else
        {
            $rows = array("ResponseCode"=>"201","Result"=>"false","ResponseMsg"=>"Order Missing");
            echo json_encode($rows);
        }
    }
}

?>