<?php session_start();
?>

<?php 
include('conn.php');


if(isset($_POST['submit'])){
 
 
 $name=addslashes($_POST['fname']);
  $number=$_POST['contact'];
    $receiver_number=$_POST['receiver_contact'];
    $address=$_POST['address'];
 $payment=$_POST['payment'];
  $orderid=$_POST['orderid'];
  $order_id=$_POST['order_id'];
  $dates=$_POST['dates'];
  $item_total=$_POST['item_total'];
  $food_name=$_POST['food_name'];
  $quantity=$_POST['quantity'];
  $price=$_POST['price'];
  $new_price=$_POST['new_price'];
  $total=$_POST['total'];
  $location=$_POST['location'];
  $by=$_POST['by'];
  $table=$_POST['table'];
  $time=$_POST['time'];
  $discount=$_POST['discount'];
  $note=$_POST['note'];
  $item_id=$_POST['item_id'];
  $order_type=$_POST['order_type'];
$date=date("F j, Y");
$month = $_POST['month'];
}

if ($order_type==1) {
  $href='online_carts';
}
else{
  $href='carts';
}

if ($table==0) {
 $order_type=1;
}

if ($payment=='nun') {
 echo"<script>alert('Payment Method must be selected'); window.location.href='$href'</script>";
}
else{


  if($payment==0){
    $payment='Cash';
   }
   elseif($payment==1){
    $payment='Momo';
   }
  
if ($order_type==1) {
  $payment_status=0;
  $table=0;
}
else{
  $payment_status=1;
}

$sql = "SELECT `number` FROM `customers` WHERE `number`='".$number."'";
$result = $conn->query($sql);

if($result->num_rows >= 1) {


} else {
// Image db insert sql
$sql = "INSERT INTO `customers`(`fname`, `number`, `date`, `by`) VALUES ('$name','$number','$date','$by')";
if(mysqli_query($conn,$sql)){


}
else{
  echo 'Error: '.mysqli_error($conn);
}

}

$sql1 = "INSERT INTO `orders_details`( `orderid`, `costomer_name`, `contact`, `amount`, `payment_method`, `payment_status`,`table`,`order_type`,`date`,`time`,`by`)
VALUES ('$order_id','$name','$number','$total','$payment','$payment_status','$table','$order_type','$date','$time','$by')";
if(mysqli_query($conn,$sql1)){ 
$total=count($_POST['item_id']);
for ($i=0; $i <$total ; $i++) { 
$sql2="INSERT INTO `orders`(`orderid`,`item_id`,`item_name`,`quantity`, `price`, `total`,  `location`, `date`, `month`) VALUES('$orderid[$i]','$item_id[$i]','$food_name[$i]','$quantity[$i]','$price[$i]','$item_total[$i]','$location[$i]','$dates[$i]','$month[$i]')";
if(mysqli_query($conn,$sql2)){
unset($_SESSION['cart']);

if ($order_type==1 && $table==0) {

$sql = "INSERT INTO `delivery_details`(`orderid`, `receiver`, `address`) VALUES ('$order_id','$receiver_number','$address')";
if(mysqli_query($conn,$sql)){


}
else{
  echo 'Error: '.mysqli_error($conn);
}
  if ($payment=='Momo') {
    $urlid=urlencode(base64_encode($order_id));
    echo"<script>alert('Complete Payment '); window.location.href='online_payment?item=$urlid'</script>";
  }
 else{
 echo"<script>alert('Order Submitted successful'); window.location.href='track_order'</script>";
 }
}
else{
  header("location:print_receipt?p=place_order&item=".urlencode(base64_encode($order_id)));
}


}
else{
echo 'Error: '.mysqli_error($conn);
}


}}
else{
  echo 'Error: '.mysqli_error($conn);
  }
}






