<?php 
require dirname(dirname(__FILE__)) . '/inc/Config.php';
require dirname(dirname(__FILE__)) . '/inc/Crud.php';
header('Content-type: text/json');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function siteURL() {
  $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || 
    $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  $domainName = $_SERVER['HTTP_HOST'];
  return $protocol.$domainName;
}
$data = json_decode(file_get_contents('php://input'), true);
if($data['uid'] == '' or $data['order_id'] == '')
{
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
	
	 
	 $order_id = $data['order_id'];
 $uid =  $data['uid'];
 $comment_reject = $lundry->real_escape_string($data['comment_reject']);
 
 $fetch = $lundry->query("select * from tbl_normal_order where id=".$order_id." and uid=".$uid."")->fetch_assoc();
 if(empty($fetch['p_method_id']))
 {
	 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Order Not Found!!");
 }
 else 
 {
 $check_status = $lundry->query("select * from tbl_normal_order  where uid=".$uid." and id=".$order_id." and status='Cancelled'")->num_rows;
 if($check_status != 0)
 {
	  $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Order Already Cancelled!!");    
 }
 else 
 {
 if($fetch['p_method_id'] == '5' and $fetch['wall_amt'] == 0)
	{
		
	}
	else if($fetch['p_method_id'] == '5' and $fetch['wall_amt'] != 0)
	{
		$wallet = $fetch['wall_amt'];
		 
		 $vp = $lundry->query("select wallet from tbl_user where id=".$uid."")->fetch_assoc();
		 $table="tbl_user";
  $field = array('wallet'=>$vp['wallet']+$wallet);
  $where = "where id=".$uid."";
$h = new Crud($lundry);
	  $check = $h->lundryupdateData_Api($field,$table,$where);
	  
		
		 
		 $timestamp = date("Y-m-d H:i:s");
	   
	   $table="wallet_report";
  $field_values=array("uid","message","status","amt","tdate");
  $data_values=array("$uid",'Refund Order '.$order_id.' Amount To Wallet!!','Credit',"$wallet","$timestamp");
   
      $h = new Crud($lundry);
	  $checks = $h->lundryinsertdata_Api($field_values,$data_values,$table);
	}
	else 
	{
		$wallet = $fetch['o_total'];
		$vp = $lundry->query("select wallet from tbl_user where id=".$uid."")->fetch_assoc();
		 $table="tbl_user";
  $field = array('wallet'=>$vp['wallet']+$wallet);
  $where = "where id=".$uid."";
$h = new Crud($lundry);
	  $check = $h->lundryupdateData_Api($field,$table,$where);
	  
		
		 
		 $timestamp = date("Y-m-d H:i:s");
	   
	   $table="wallet_report";
  $field_values=array("uid","message","status","amt","tdate");
  $data_values=array("$uid",'Refund Order '.$order_id.' Amount To Wallet!!','Credit',"$wallet","$timestamp");
   
      $h = new Crud($lundry);
	  $checks = $h->lundryinsertdata_Api($field_values,$data_values,$table);
	}
	
 $table="tbl_normal_order";
  $field = array('status'=>'Cancelled','comment_reject'=>$comment_reject);
  $where = "where uid=".$uid." and id=".$order_id."";
$h = new Crud($lundry);
	  $check = $h->lundryupdateData_Api($field,$table,$where);
	  
	  
	  $checksp = $lundry->query("select store_id from tbl_normal_order where id=".$order_id."")->fetch_assoc(); 
	  $store_id = $checksp['store_id'];
$content = array(
       "en" => 'Customer Cancelled Order id #'.$order_id
   );
$heading = array(
   "en" => $comment_reject
);

$fields = array(
'app_id' => $set['s_key'],
'included_segments' =>  array("Active Users"),
'data' => array("order_id" =>$order_id),
'filters' => array(array('field' => 'tag', 'key' => 'store_id', 'relation' => '=', 'value' => $checksp['store_id'])),
'contents' => $content,
'headings' => $heading
);

$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 '.$set['s_hash']));
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);

$timestamp = date("Y-m-d H:i:s");

 $title_mains = $comment_reject;
$descriptions = 'Customer Cancelled Order id #'.$order_id;

	   $table="tbl_snoti";
  $field_values=array("sid","datetime","title","description");
  $data_values=array("$store_id","$timestamp","$title_mains","$descriptions");
  
    $h = new Crud($lundry);
	   $h->lundryinsertdata_Api($field_values,$data_values,$table);

 $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order  Cancelled successfully!");
 }
 }
}
echo json_encode($returnArr);
?>