<?php
header("Access-Control-Allow-Origin: *");
//header("Content-Type: application/json; charset=UTF-8");
include 'dbconfig.php';
include '../email.php';
if(isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
}

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$json = json_decode(file_get_contents('php://input'), true);
$products=$json['products'];
$address_id=$json['address_id'];
$user_id= $json['user_id'];
$vendor_id= $json['vendor_id'];
$date= $json['date'];
$time= $json['time'];
$payment_mode= $json['payment_mode'];
$serviceid= $json['serviceid'];
$subid=$json['subid'];
$name="";
$email="";
$phone="";
$street="";
$address="";
$address_type="";
$prefix="";
$created_date  = date('Y-m-d H:i:s');
$order_date  = date('Y-m-d');
$sql="SELECT * FROM address_book WHERE id='$address_id'";
$result = $conn->query($sql);
if(mysqli_num_rows($result)!=0){
 while($row=mysqli_fetch_array($result)){
  $name=$row['name'];	 
  $street=$row['street'];
  $address=$row['address'];
  $address_type=$row['address_type'];
  $prefix=$row['prefix'];
 }	 
}

$sql="SELECT * FROM user_details WHERE user_id='$user_id'";
$result = $conn->query($sql);
if(mysqli_num_rows($result)!=0){
  while($row=mysqli_fetch_array($result)){
  $email=$row['email'];	 
  $phone=$row['phone'];
 }	 
}
$created_time=time();
$ins="INSERT INTO save_order SET order_date='$order_date',user_id='$user_id',service_id='$serviceid',subservice_id='$subid',vendor_id='$vendor_id',name='$name',email='$email',phone='$phone',street='$street',address='$address',address_type='$address_type',prefix='$prefix',job_date='$date',job_time='$time',payment_mode='cod',created='$created_date',created_time='$created_time'";
$ins_result=$conn->query($ins);
$order_id="";
if($ins_result){
$order_id=mysqli_insert_id($conn);
}	
$total=0;
$com_total=0;
foreach($products as $product){
$service_id=$product['id']; 
$sqlcat="SELECT * FROM serviceprice WHERE id='$service_id'";
$resultcat = $conn->query($sqlcat);
$catrow=mysqli_fetch_array($resultcat);
$cat_id=isset($catrow['service_id']) ? $catrow['service_id'] :'';
$subcat_id=isset($catrow['subservice_id']) ? $catrow['subservice_id'] :'';
$child_id=isset($catrow['child_id']) ? $catrow['child_id'] :'';
$service_name=isset($product['product_name']) ? $product['product_name']: '';
$qty=$product['qty'];
$price=$product['price'];
$amount=$product['amount'];
$total+=$product['amount'];
$commission=$conn->query("SELECT commission FROM subservices WHERE service_id='$cat_id' and id='$subcat_id'");
$commrow=mysqli_fetch_array($commission);
$totalcommission=$commrow['commission'];
$percentage=0;
if(!empty($totalcommission)){
$percentage=(($product['amount'] * $totalcommission)/100);
}
$com_total+=$percentage;
$sql="INSERT INTO order_item(order_id,child_id,cat_id,subcat_id,service_id,service_name,service_qty,service_price,service_total,created)VALUES('$order_id','$child_id','$cat_id','$subcat_id','$service_id','$service_name','$qty','$price','$amount','$created_date')";
$result = $conn->query($sql);     
}
$update=$conn->query("UPDATE save_order SET grand_total='$total',commission='$com_total' WHERE id='$order_id'");	
$html=' <h1>Hi '.$name.',</h1> 
        <h3>Your Order created successfully.</h3>';
sendemail($email,"New Order #$order_id",$html);

$getvendor=$conn->query("SELECT * FROM user_details WHERE user_id='$vendor_id'");
$getrow=mysqli_fetch_array($getvendor);
$html=' <h1>Hi '.$getrow['name'].',</h1> 
            <p>New order has been created Order ID #'.$order_id.'.Please check your account.</p>';
sendemail($getrow['email'],"New Order #$order_id",$html);
$responseData = array('order_id'=>$order_id,'success'=>'1','message'=>"success");
$data = array('data' => $responseData );
echo json_encode($data);      
?>