<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['uid'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    $shop_id =  $data['shop_id'];
    $shop_code =  $data['shop_code'];
    
    $version =  $data['version'];
    $brand =  $data['brand'];
    $model =  $data['model'];
    $order_id =  $data['order_id'];
    $uid =  $data['uid'];
    $datesloat = $data['date'];
    $timesloat = $data['time'];
    
    $itemtotal = $data['item_total'];
    $dcharge = $data['d_charge'];
    $tax = $data['tax'];
    $tax_amount = $data['tax_amount'];
    $deli_tip =  $data['deli_tip'];
    $discount_amount = $data['discount_amount'];
    $couponcode = $data['coupon_code'];
    $coins =  $data['coins'];
    $coins_earn =  $data['coins_earn'];
    $coins_use =  $data['coins_use'];
    $total = number_format((float)$data['total'], 2, '.', '');
    
    $p_method = $data['p_method'];
    $pay_status = $data['pay_status'];
    $tid = $data['tid'];
    $address_id = $data['address_id'];
    $address = $data['address'];
    $cook = $data['cooking'];
    $distance =  $data['distance'];
    $shopdistance =  $data['shop_distance'];
    $latitude =  $data['latitude'];
    $longitude =  $data['longitude'];
    
    $pname = $data['pname'];

    if($p_method != 'Pay on Delivery' && $pay_status == 0)
    {
        $status = 'cancelled';
        $o_status = '0';
    }
    else
    {
        $status = 'pending';
        $o_status = '1';
    }
    
    $oid ='#'.uniqid();
    
    $timestamp = date("Y-m-d");
    
    $e= array();
    $p = array();
    $w=array();
    $pp = array();
    $q = array();
    
    for($i=0;$i<count($pname);$i++)
    {
        $e[] = $pname[$i]['title'];
        $p[] = $pname[$i]['pid'];
        $w[] = $pname[$i]['weight'];
        $pp[] = $pname[$i]['cost'];
        $q[] = $pname[$i]['qty'];
    }
    
    $pname = implode('$;',$e);
    $pid = implode('$;',$p);
    $ptype = implode('$;',$w);
    $pprice = implode('$;',$pp);
    $qty = implode('$;',$q);
    
    $sql_qu ="select * from orders where order_id = '$order_id'";
    $status1 = $con->query($sql_qu)->num_rows;
    
    if($status1 == 0)
    {
        $sql = "insert into orders(`shop_id`,`shop_code`,`order_id`,`oid`,`uid`,`pname`,`pid`,`ptype`,`pprice`,`qty`,`datesloat`,`timesloat`,`order_date`,`o_status`,`status`,`item_total`,`d_charge`,`tax`,`tax_amount`,`deli_tip`,
        `discount_amount`,`coupon_code`,`coins`,`coins_earn`,`coins_use`,`total`,`p_method`,`pay_status`,`tid`,`address_id`,`address`,`cooking_ins`,`distance`,`shop_distance`,`latitude`,`longitude`,`version`,`brand`,`model`) 
        values('$shop_id','$shop_code','$order_id','$oid','$uid','$pname','$pid','$ptype','$pprice','$qty','$datesloat','$timesloat','$timestamp','$o_status','$status','$itemtotal','$dcharge','$tax','$tax_amount','$deli_tip',
        '$discount_amount','$couponcode','$coins','$coins_earn','$coins_use','$total','$p_method','$pay_status','$tid','$address_id','$address','$cook','$distance','$shopdistance','$latitude','$longitude','$version','$brand','$model')";
        $con->query($sql);
        
        // $sql_log = "insert into order_details(`uid`,`order_id`,`status`,`order_date`,`order_time`)values('$uid','$order_id','$status','$datesloat','$timesloat')";
        // $con->query($sql_log);
        
        // $user = $con->query("select * from user where id = '$uid'");
        // if($row_user = $user->fetch_assoc()) {
        //     $u_coins = $row_user['coins'];
        // }
            
        // if($status == 'pending')
        // {
        //     if($coins_use > 0)
        //     {
        //         $transaction_name = "Redeem for #".$order_id;
        //         $sql_coins = "insert into coin_transactions(`uid`,`name`,`coins`,`coin_type`,`c_date`,`c_time`)values('$uid','$transaction_name','$coins_use','0','$datesloat','$timesloat')";
        //         $con->query($sql_coins);
        //     }
            
        //     if($coins_earn > 0)
        //     {
        //         $transaction_name = "Earn from #".$order_id;
        //         $sql_coin = "insert into coin_transactions(`uid`,`name`,`coins`,`coin_type`,`c_date`,`c_time`)values('$uid','$transaction_name','$coins_earn','1','$datesloat','$timesloat')";
        //         $con->query($sql_coin);
        //     }
            
        //     if($u_coins != 0)
        //     {
        //         $u_coins = ($u_coins - $coins_use) + $coins_earn;
        //     }
            
        //     $con->query("update user set coins='$u_coins' where id = '$uid'");
        // }
    
        $returnArr = array("ResponseCode"=>$sql,"Result"=>"true","ResponseMsg"=>"Order Placed Successfully!");
    }
    else
    {
        $returnArr = array("ResponseCode"=>$status,"Result"=>"false","ResponseMsg"=>"Retry Place Order");
    }

}

echo json_encode($returnArr);