<!DOCTYPE html>
<html>
    
    <head>
        <title>Tables</title>
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php';
         $total=0; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
           <div class="span5" id="content">

                        <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Orders</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <?php
                                    include '../includes/connection.php';
                                    $sql = mysqli_query($con,"SELECT id, (select product_name from products where id=temp.product_id)as product_name, quantity, (select price from products where id=temp.product_id)as unit from temp");
                                    if (mysqli_num_rows($sql) > 0) {
                                       ?>
  									<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
										<thead>
											<tr>
												<th>Id</th>
												<th>Product Name</th>
												<th>Quantity</th>
												<th>Unit Price</th>
												<th>Total Price</th>
                                                <th>Action</th>
											</tr>
										</thead>
										<tbody>
                                        <?php
                                        
                                         // output data of each row
                while($row = mysqli_fetch_assoc($sql)) {
                        $total=$total+$row['unit']*$row['quantity'];
                    ?>
                    <tr class="odd gradeX">

        
                                                <td><?php echo $row['id']; ?></td>
                                                <td><?php echo $row['product_name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td class="center"> <?php echo $row['unit']; ?></td>
                                                <td class="center"><?php echo $row['unit']*$row['quantity']; ?></td>
                                                <td class="center">
                                                    <a href="edit_order.php?id=<?php echo $row['id']; ?>"><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                    <a href="deleteorder.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                </td>
                                            </tr>
                                            <?php
    }
} else {
    echo "0 results";
}

//mysqli_close($conn);
?>
											
											
										</tbody>
									</table>
                                    <h3><center>TOTAL: Ush. <?php echo $total; ?> </center></h3>
                                    <div class="muted pull-right">

                                <a href="processorder.php?total=<?php echo $total; ?>"><button class="btn btn-primary"> Process Order</button></a></div>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

         
                </div>
                <div class="span4">
                                     <form class="form-horizontal" action="" method="POST"> 
                                      <fieldset>
                                        <legend>Add new Order</legend>
                                        <div class="control-group">
                                          <label class="control-label" for="selectError">Product</label>
                                          <div class="controls">
                                            <select id=selectError name="product" required>
                                            <option>Select Product</option>
                                              <?php include 'listproducts.php'; ?>
                                            
                                            </select>
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput">Quantity</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="quantity" id="focusedInput" type="number" value="Enter Quantity">
                                          </div>
                                        </div>
                                       
                                        
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="add"><i class="icon-plus "></i>Add</button>
                                          <button type="reset" class="btn">Cancel</button>
                                        </div>
                                      </fieldset>
                                    </form>
                                    <?php
                                        include 'includes/connection.php';

                                        if(isset($_POST['add'])){
                                            $product=$_POST['product'];
                                            $quantity=$_POST['quantity'];
                                        $sql2 = mysqli_query($con,"INSERT INTO temp (product_id, quantity)
                                        VALUES ('$product', '$quantity')");

                                        if ($sql2) {
                                           ?><meta http-equiv="refresh" content="0;URL='new_order.php'" /><?php
                                        } else {
                                            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                                        }

                                        mysqli_close($con);
                                        }
                                        
                                        ?>

                                </div>
            </div>

<div class="row-fluid">
    
</div>

            <hr>
            <footer>
                <p>&copy; Vincent Gabriel 2013</p>
            </footer>
        </div>
        <!--/.fluid-container-->

        <script src="vendors/jquery-1.9.1.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
        <script src="vendors/datatables/js/jquery.dataTables.min.js"></script>


        <script src="assets/scripts.js"></script>
        <script src="assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>