 <?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);
 $data = json_decode(file_get_contents('php://input'), true);
 function siteURL()
 {
     $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
     $domainName = $_SERVER['HTTP_HOST'];
     return $protocol . $domainName;
 }

 if ($data['uid'] == '') {
     $returnArr = ["ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went Wrong!"];
 } else {
     $uid = $data['uid'];
     $store_charge = $data['store_charge'];
     $vcomissions = $lundry->query("select commission from service_details where id=" . $data['store_id'] . "")->fetch_assoc();
     $vcomission = $vcomissions['commission'];
     $vp = $lundry->query("select wallet from tbl_user where id=" . $uid . "")->fetch_assoc();
     if ($vp['wallet'] >= $data['wall_amt']) {
         $store_id = $data['store_id'];
         $p_method_id = $data['p_method_id'];
         $full_address = $data['full_address'];
         $d_charge = number_format((float) $data['d_charge'], 2, '.', '');
         $cou_id = $data['cou_id'];
         $landmark = $data['landmark'];
         $wall_amt = number_format((float) $data['wall_amt'], 2, '.', '');
         $cou_amt = number_format((float) $data['cou_amt'], 2, '.', '');
         $sdates = date("Y-m-d");
         $tslot = $data['tslot'];
         $dslot = $data['dslot'];
		 
		 $originalDate = $data['pick_date'];
// Use strtotime() to parse the input date
$timestamp = strtotime($originalDate);
$formattedDate = date('Y-m-d', $timestamp);
         $transaction_id = $data['transaction_id'];
         $product_total = number_format((float) $data['product_total'], 2, '.', '');
         $product_subtotal = number_format((float) $data['product_subtotal'], 2, '.', '');
         $a_note = mysqli_real_escape_string($lundry, $data['a_note']);
         $name = mysqli_real_escape_string($lundry, $data['name']);
         $mobile = mysqli_real_escape_string($lundry, $data['mobile']);
         $table = "tbl_normal_order";
         $field_values = [
             "uid",
             "commission",
             "store_charge",
             "store_id",
             "odate",
             "p_method_id",
             "address",
             "d_charge",
             "cou_id",
             "cou_amt",
             "o_total",
             "subtotal",
             "trans_id",
             "a_note",
             "wall_amt",
             "name",
             "mobile",
             "status",
             "landmark",
             "tslot",
             "dslot",
			 "pick_date"
         ];
         $data_values = [
             "$uid",
             "$vcomission",
             "$store_charge",
             "$store_id",
             "$sdates",
             "$p_method_id",
             "$full_address",
             "$d_charge",
             "$cou_id",
             "$cou_amt",
             "$product_total",
             "$product_subtotal",
             "$transaction_id",
             "$a_note",
             "$wall_amt",
             "$name",
             "$mobile",
             'Pending',
             "$landmark",
             "$tslot",
             "$dslot",
			 "$formattedDate"
         ];

         $h = new Crud($lundry);
         $oid = $h->lundryinsertdata_Api_Id($field_values, $data_values, $table);

         $ProductData = $data['ProductData'];
         for ($i = 0; $i < count($ProductData); $i++) {
             $title = mysqli_real_escape_string($lundry, $ProductData[$i]['title']);
             $type = mysqli_real_escape_string($lundry, $ProductData[$i]['variation']);
             $cost = $ProductData[$i]['price'];
             $qty = $ProductData[$i]['qty'];
             $discount = $ProductData[$i]['discount'];
             $image = $ProductData[$i]['image'];

             $table = "tbl_normal_order_product";
             $field_values = ["oid", "pquantity", "ptitle", "pdiscount", "pimg", "pprice", "ptype"];
             $data_values = ["$oid", "$qty", "$title", "$discount", "$image", "$cost", "$type"];

             $h = new Crud($lundry);
             $h->lundryinsertdata_Api($field_values, $data_values, $table);
         }
         if ($wall_amt != 0) {
             $vp = $lundry->query("select wallet from tbl_user where id=" . $uid . "")->fetch_assoc();
             $mt = intval($vp['wallet']) - intval($wall_amt);
             $table = "tbl_user";
             $field = ['wallet' => "$mt"];
             $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 = ["uid", "message", "status", "amt", "tdate"];
             $data_values = ["$uid", 'Wallet Used in Order Id#' . $oid, 'Debit', "$wall_amt", "$timestamp"];

             $h = new Crud($lundry);
             $checks = $h->lundryinsertdata_Api($field_values, $data_values, $table);
         }

         $udata = $lundry->query("select name from tbl_user where id=" . $uid . "")->fetch_assoc();
         $name = $udata['name'];

         $content = [
             "en" => $name . ', Your Order #' . $oid . ' Has Been Received.',
         ];
         $heading = [
             "en" => "Order Received!!",
         ];

         $fields = [
             'app_id' => $set['one_key'],
             'included_segments' => ["Active Users"],
             'data' => ["order_id" => $oid, "type" => 'normal'],
             'filters' => [['field' => 'tag', 'key' => 'userid', 'relation' => '=', 'value' => $uid]],
             'contents' => $content,
             'headings' => $heading,
             'big_picture' => siteURL() . '/order_process_img/received.png',
         ];
         $fields = json_encode($fields);

         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
         curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8', 'Authorization: Basic ' . $set['one_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_main = "Order Received!!";
         $description = $name . ', Your Order #' . $oid . ' Has Been Received.';

         $table = "tbl_notification";
         $field_values = ["uid", "datetime", "title", "description"];
         $data_values = ["$uid", "$timestamp", "$title_main", "$description"];

         $h = new Crud($lundry);
         $h->lundryinsertdata_Api($field_values, $data_values, $table);

         $content = [
             "en" => 'New Order #' . $oid . ' Has Been Received.',
         ];
         $heading = [
             "en" => "Order Received!!",
         ];

         $fields = [
             'app_id' => $set['s_key'],
             'included_segments' => ["Active Users"],
             'data' => ["order_id" => $oid, "type" => 'normal'],
             'filters' => [['field' => 'tag', 'key' => 'store_id', 'relation' => '=', 'value' => $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, ['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 = "Order Received!!";
         $descriptions = 'New Order #' . $oid . ' Has Been Received.';

         $table = "tbl_snoti";
         $field_values = ["sid", "datetime", "title", "description"];
         $data_values = ["$store_id", "$timestamp", "$title_mains", "$descriptions"];

         $h = new Crud($lundry);
         $h->lundryinsertdata_Api($field_values, $data_values, $table);

         $tbwallet = $lundry->query("select wallet from tbl_user where id=" . $uid . "")->fetch_assoc();
         $returnArr = ["ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "Order Placed Successfully!!!", "wallet" => $tbwallet['wallet']];
     } else {
         $tbwallet = $lundry->query("select wallet from tbl_user where id=" . $uid . "")->fetch_assoc();
         $returnArr = ["ResponseCode" => "200", "Result" => "false", "ResponseMsg" => "Wallet Balance Not There As Per Order Refresh One Time Screen!!!", "wallet" => $tbwallet['wallet']];
     }
 }

 echo json_encode($returnArr);

