<!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 'header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include 'sidebar.php'; ?>
                <!--/span-->
                <div class="span9" id="content">

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Activity Logs</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   <div class="table-toolbar">
                                   
                                      <div class="btn-group pull-right">
                                         <button data-toggle="dropdown" class="btn dropdown-toggle">Tools <span class="caret"></span></button>
                                         <ul class="dropdown-menu">
                                            <li><a href="#">Print</a></li>
                                            <li><a href="#">Save as PDF</a></li>
                                            <li><a href="#">Export to Excel</a></li>
                                         </ul>
                                      </div>
                                   </div>
                                   <?php
include 'connection.php';
                                   // Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM sales where branch_id='$branch' order by id DESC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    


                                   ?>
                                    
                                    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example2">
                                        <thead>
                                            <tr>
                                                <th>order Id</th>
                                                <th width="60%">
                                                    
                                                Details

                                                </th>
                                                <th width="20%">date/Time</th>
                                                

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
while($row = mysqli_fetch_assoc($result)) {
	
        ?>
<tr class="gradeU">
                                                <td><?php echo $row['id'];
$idd=$row['id'];
                                                 ?></td>
                                                <td>
                                                    
<!--new table showing details of each sale -->
<?php
//include 'connection.php';
                                   // Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql2 = "SELECT id, (select product_name from products where id=sales_details.product_id)as product_name, quantity, (select price from products where id=sales_details.product_id) as price FROM sales_details where order_id='$idd'";
$result2 = mysqli_query($con, $sql2);

if (mysqli_num_rows($result2) > 0) {
    // output data of each row
    


                                   ?>


<table class="table table-striped" width="100">
                                        <tbody>

                                        <thead>
                                            <tr>
                                                <th>id</th>
                                                <th>Product</th>
                                                <th>Quantity</th>
                                                <th>Unit Price</th>
                                                <th>Total Price</th>
                                            </tr>
                                        </thead>
                                        

                                        <?php
                                            while($row2 = mysqli_fetch_assoc($result2)) {
    
        ?>
<tr class="gradeU">
                                                <td><?php echo $row2['id']; ?></td>
                                                <td><?php echo $row2['product_name']; ?></td>
                                                <td><?php echo $row2['quantity']; ?></td>
                                                <td><?php echo $row2['price']; ?></td>
                                                <td><?php echo $row2['price']*$row2['quantity']; ?></td>

           </tr>
                                            
                                            <?php


  }
} else {
    echo "0 results";
}

mysqli_close($con);
?>

                                        </tbody>
                                    </table>

<!--new table showing details of each sale -->





                                                </td>
                                                
                                                <td class="center"><?php echo $row['date_time']; ?></td>
                                                
                                             
                                            </tr>
                                            
                                            <?php


  }
} else {
    echo "0 results";
}

mysqli_close($conn);


                                        ?>
                                            
                                        </tbody>
                                    </table>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </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>

<?php


if(isset($_POST['add'])){
	include 'connection.php';
$product_name=$_POST['name'];
$quantity=$_POST['quantity'];
$unit=$_POST['unit'];
	// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO products (product_name, quantity, price)
VALUES ('$product_name', '$quantity', '$unit')";

if (mysqli_query($conn, $sql)) {


    $id_user=$_SESSION['id'];
  $activity="Added a new product ".$product_name;
  include 'store_logs.php';
	?>


    <meta http-equiv="refresh" content="0;URL='savepurchase.php?product=<?php echo $product_name; ?>&quantity=<?php echo $quantity; ?>&price=<?php echo $unit; ?>'" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
}

?>
 
</html>
