<?php 
include("side_nav.php");
include("top_nav.php"); 
?>

        <!-- page content -->
        <div class="right_col" role="main">
         
            <div class="clearfix"></div>

            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Stock <small></small></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <p class="text-muted font-13 m-b-30">
                      Manage your stock level with the table below.
                    </p>
					<table id="datatable-buttons" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th class="col-md-3 col-sm-3 col-xs-3" >Product Name</th>
                          <th class="col-md-2 col-sm- col-xs-2">Minimum Stock Level</th>
                          <th class="col-md-2 col-sm- col-xs-2">Maximum Stock Level</th>
                          <th class="col-md-2 col-sm- col-xs-2">Current Stock Level</th>
                          <th>Qty to add</th>
                          <th class="col-md-2 col-sm-2 col-xs-2">Action</th>
                        </tr>
                      </thead>

                      
                    <tbody>
					    <?php 
						  $sql = mysqli_query($con, "SELECT * FROM product");
						  while($row = mysqli_fetch_array($sql)): 
						?>
                        <tr>
							<td><?php echo $row['product_name']; ?></td>
							<td><input type="number" id="min_stock_level-<?php echo $row['product_id'] ?>" value="<?php echo $row['min_stock_level']; ?>" /></td>
							<td><input type="number" id="max_stock_level-<?php echo $row['product_id'] ?>" value="<?php echo $row['max_stock_level']; ?>" /></td>
							<td><?php echo $row['current_stock_level']; ?></td>
							<td><input type="number" id="qty_to_add-<?php echo $row['product_id'] ?>" /></td>
							<td>
								<div class="btn-group">
									<button prdId="<?php echo $row['product_id'] ?>" id="submit" class="btn btn-primary">Submit</button>
								</div>
							</td>
                        </tr>
					  <?php endwhile; ?>
                    </tbody>
					  
                    </table>
                    
				  </div>
                </div>
              </div>
         </div>
        </div>
        </div>
		</div>
    <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    
    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>
	</body>
</html>
<script>
$("body").delegate("#submit","click",function(event){
    var product_id = $(this).attr('prdId');
    var minStockLevel = $('#min_stock_level-'+product_id).val();
    var maxStockLevel = $('#max_stock_level-'+product_id).val();
    var qtyToAdd = $('#qty_to_add-'+product_id).val();
    
	$.ajax({
		url	:	"process.php",
		type:	"POST",
		data	:	{
			query_stock:1, 
			product_id : product_id,
			minStockLevel : minStockLevel,
			maxStockLevel: maxStockLevel,
			qtyToAdd : qtyToAdd,
		},
		success	:	function(response){ 
			window.location = "update-stock";
		}
	})
})
</script>