<?php
include("modules/db.php");
include("modules/cart.php");
include("modules/order.php");
@session_start();
$cart = new Cart();

if(isset($_SESSION['cart'])){
	$carts = json_decode($_SESSION['cart']);
}else{
	exit();
}

if(isset($_POST['cus_name']) and isset($_POST['cus_address']) and isset($_POST['cus_phone'])){
	$cus_name = $_POST['cus_name'];
	$cus_address = $_POST['cus_address'];
	$cus_phone = $_POST['cus_phone'];
}else{
	exit();
}

$order_total_price = 0;

$orderModel = new OrderModel();
	$orderModel->cus_name = $cus_name;
	$orderModel->cus_address = $cus_address;
	$orderModel->cus_phone = $cus_phone;

foreach ($carts as $key => $cart) {
	$orderDetailModel = new OrderDetailModel();
	$orderDetailModel->product_id = $cart->product_id;
	$orderDetailModel->product_name = $cart->product_name;
	$orderDetailModel->quantity = $cart->product_qty;
	$orderDetailModel->product_price = $cart->product_price;
	$orderDetailModel->product_total_price = $cart->product_qty*$cart->product_price;
	$orderDetailModel->cus_name = $cus_name;
	$orderDetailModel->cus_address = $cus_address;
	$orderDetailModel->cus_phone = $cus_phone;


	$orderModel->order_total_price+= $orderDetailModel->product_total_price;
	array_push($orderModel->order_detail, $orderDetailModel);

}

$orders = new Orders();
$res = $orders->create_order($orderModel);
if($res!=0){
	header('Location:payment.php?order_id='.$res);
	exit();
}



?>