<?php session_start(); ?>
<?php 
	include "database.php";
	include "includes/operations.php";
	if(!isset($_SESSION['admin_id'])){
		echo '<script> window.location.href = "login.php"; </script>';
		header("location:login.php"); exit();
	}
	
	function CheckAccess() {
		
    $result = (isset($_SESSION['role']) &&  $_SESSION['role'] != 'Super Admin' &&  $_SESSION['role'] != 'User');
		return $result;
	}
	
	if (CheckAccess()) {
		header("location:login.php");
			echo '<script> window.location.href = "login.php"; </script>';
			exit();
	}
	
	$admin_id = $_SESSION['admin_id'];
	
		
	$order_id = 0;
	if(isset($_GET['oid'])){
		$order_id = intval($_GET['oid']);
	}
	
	$order_result = mysqli_query($con, "SELECT * FROM orders WHERE id=$order_id");
	if (mysqli_num_rows($order_result)==0) {
		header("Location:index.php");
		exit();
	}
	
	$order_row = mysqli_fetch_array($order_result);
	//$user_id = $order_row['user_id'];
	//$pickup = $order_row['pickup'];
	
	
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include "includes/headerlinks.php";?>
		<style>
		b {
			font-weight:600 !important;	
		}
		.form-horizontal label {
			color:black !important;
			font-weight:600 !important;	
		}
		.form-material .form-control {
			color:black !important;
		}
	</style>
	
	<link href="assets/plugins/morrisjs/morris.css" rel="stylesheet">
	</head>
	<body class="fix-header card-no-border">
			<?php include "includes/loader.php";?>
		<div id="main-wrapper">
			<?php 
				include "includes/header.php";
				include "includes/sidebar.php";
			?>
			<div class="page-wrapper">
				<div class="row page-titles">
					<div class="col-md-5 align-self-center">
						<h3 class="text-themecolor">Order Details</h3>
					</div>
				</div>
				<div class="container-fluid">
					<div class="card">
						<div style="padding:10px">
						<?php if ($order_row['order_complete']==1)  { ?>
							<h3><b><i class="fa fa-check" style="color:green"></i> &nbsp; Order Completed</b></h3>
						<?php } else { ?>
							<a href="includes/operations.php?complete_order=<?php echo $order_id ?>"><button class="btn btn-success btn-lg" style="float:left;margin-top: 5px;">Complete Order</button></a>
						<?php } ?>
						</div>
						<div class="row">
							<div class="col-md-7">
							
								<h3 class="text-center" style="background:#000;color:#fff;padding:8px">Products</h3>
									<table class="table stylish-table table-responsive">
										<tr>
											<th>Image</th>
											<th>Product Name</th>
											<th class="text-center">Price</th>
											<th class="text-center">Quantity</th>
											<th class="text-center">Total</th>
										</tr>
										<?php
											$presult = mysqli_query($con, "SELECT * FROM order_details INNER JOIN product ON order_details.product_id = product.product_id WHERE order_id=$order_id");
											$total_price = 0;
											$count = 0;
											while ($prow = mysqli_fetch_array($presult)) {
												$total_price = $total_price + ($prow['product_price']*$prow['product_quantity']);
												$count++;
												$pid = $prow['product_id'];
												//$shipping_cost = $prow['product_shipping_cost'];
											
										?>
										
											<tr>
												<td style="width:50px;"><img src="../images/product_images/<?php echo $prow['product_image'] ?>" class="img-responsive img-thumbnail"></td>
												<td>
													<?php echo $prow['product_title'] ?>
												</td>
												<td class="text-center">$<?php echo $prow['product_price'] ?></td>
												<td class="text-center"><?php echo $prow['product_quantity'] ?></td>
												<td class="text-center"><span>$<span id="list_product_total_<?php echo $count; ?>"><?php echo $prow['product_price']*$prow['product_quantity'] ?></span> </span></td>
											</tr>
										<?php
											}
										?>
										<tr>
											<th style="font-size:14px;text-align:right;" colspan="4">Subtotal:</th>
											<th style="font-size:14px" colspan="4"><span>$<span id="total_price_table"><?php echo number_format($total_price,2);?></span></span></th>
										</tr>
										<tr class="">
											<th style="font-size:14px;text-align:right;" colspan="4">>Estimated Tax (7%)</th>
											<th style="font-size:14px" colspan="4">
												$<?php
													$tax = 7/100;
													echo $value_tax = $total_price * $tax;
												?>
											</th>
										</tr>
										<tr>
											<th style="font-size:14px;text-align:right;" colspan="4">Shipping Cost:</th>
											<th style="font-size:14px" colspan="4"><span>$<span id="total_price_table"><?php echo $order_row['shipping_cost']?></span></span></th>
										</tr>
										<?php
											$shipping_cost = $order_row['shipping_cost'];
											$final_total = $total_price + $shipping_cost + $value_tax;
										?>
										<tr>
											<th style="font-size:18px;text-align:right;" colspan="4">Total:</th>
											<th style="font-size:18px" colspan="4"><span>$<span id="total_price_table"><?php echo number_format($final_total,2);?></span></span></th>
										</tr>
									</table>
									
							</div>
							<div class="col-md-5">
								<h3 class="text-center" style="background:#000;color:#fff;padding:8px">Order Details</h3>
								<table class="table product-list-table">
										<tr>
											<th>Order Created</th>
											<td><?php echo date("M-d-Y", strtotime($order_row['order_date']));?></td>
										</tr>
										<tr>
											<th>Order No.</th>
											<td><?php echo $order_row['order_id']; ?></td>
										</tr>
										<tr>
											<th>Customer Name</th>
											<td><?php echo $order_row['customer_name']; ?></td>
										</tr>
										<tr>
											<th>Email</th>
											<td><?php echo $order_row['customer_email']; ?></td>
										</tr>
										<tr>
											<th>Phone</th>
											<td><?php echo $order_row['customer_phone']; ?></td>
										</tr>
										<tr>
											<th>State</th>
											<td><?php echo $order_row['user_state']; ?></td>
										</tr>
										<tr>
											<th>City</th>
											<td><?php echo $order_row['user_city']; ?></td>
										</tr>
										<tr>
											<th>Postal Code</th>
											<td><?php echo $order_row['user_postalcode']; ?></td>
										</tr>
										<tr>
											<th>Shipping Address</th>
											<td><?php echo $order_row['order_address']; ?></td>
										</tr>
									</table>
							</div>
						</div>
						<!-- Column -->
					</div>
				</div>
				<?php 
					include "includes/footer.php";
					include "includes/theme.php";
				?>
			</div>
		</div>
		<script src="assets/plugins/jquery/jquery.min.js"></script>
		<script src="assets/plugins/bootstrap/js/popper.min.js"></script>
		<script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
		<script src="js/jquery.slimscroll.js"></script>
		<script src="js/waves.js"></script>
		<script src="js/sidebarmenu.js"></script>
		<script src="assets/plugins/sticky-kit-master/dist/sticky-kit.min.js"></script>
		<script src="assets/plugins/sparkline/jquery.sparkline.min.js"></script>
		<script src="js/custom.min.js"></script>
		<script src="assets/plugins/styleswitcher/jQuery.style.switcher.js"></script>
		<script src="assets/plugins/morrisjs/morris.js"></script>
		<script src="js/morris-data.js"></script>
	</body>
    <script>
    $(document).ready(function() {
        $('#myTable').DataTable();
        $(document).ready(function() {
            var table = $('#example').DataTable({
                "columnDefs": [{
                    "visible": false,
                    "targets": 2
                }],
                "order": [
                    [2, 'asc']
                ],
                "displayLength": 25,
                "drawCallback": function(settings) {
                    var api = this.api();
                    var rows = api.rows({
                        page: 'current'
                    }).nodes();
                    var last = null;
                    api.column(2, {
                        page: 'current'
                    }).data().each(function(group, i) {
                        if (last !== group) {
                            $(rows).eq(i).before('<tr class="group"><td colspan="5">' + group + '</td></tr>');
                            last = group;
                        }
                    });
                }
            });
            // Order by the grouping
            $('#example tbody').on('click', 'tr.group', function() {
                var currentOrder = table.order()[0];
                if (currentOrder[0] === 2 && currentOrder[1] === 'asc') {
                    table.order([2, 'desc']).draw();
                } else {
                    table.order([2, 'asc']).draw();
                }
            });
        });
		$('.summernote').summernote({
            height: 150, // set editor height
            minHeight: null, // set minimum height of editor
            maxHeight: null, // set maximum height of editor
            focus: false // set focus to editable area after initializing summernote
        });

        $('.inline-editor').summernote({
            airMode: true
		});
		window.edit = function() {
			$(".click2edit").summernote()
		},
		window.save = function() {
			$(".click2edit").summernote('destroy');
		}
    });
	$('.select2').select2()
    $('#example23').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    });
	
    </script>
</html>
