<!DOCTYPE html>
<html>
    <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>
    <head>

		<?php
		include("../includes/header-links.php");
		?>
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
		
            <div class="row-fluid">
                <?php include '../includes/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">Products ouf of stock</div>
                            </div>
                            <div class="block-content collapse in">
							
                                <div class="span12">
<div class="row">
	<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

	</div>
	<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
	</div>		 
</div>
                                   <div class="table-toolbar">
                                  
                                      

                                   
                                   <?php
include '../includes/connection.php';
if( $_SESSION['role']=="Super Admin"){                                     // Create connection
$sql = mysqli_query($con,"SELECT * FROM products where quantity<=0 && status=1 && business_id='$business'");
}else{
  $sql = mysqli_query($con,"SELECT * FROM products where quantity=0 && status=1 && branch_id='$branch'");
}
//$result = mysqli_query($con, $sql);

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


                                   ?>
        </div>
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Category</th>
                                                <th>Product Name</th>
                                                <th>Product Code</th>
                                                <th>Quantity</th>
                                                <th>Price</th>
                                                <?php if($_SESSION['role']=="admin"){ ?>
                                                <th>Action</th>
                                                <?php } ?>

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
while($row = mysqli_fetch_assoc($sql)) {
	
        ?>
<tr class="gradeU">
                                                <td><?php echo $row['id']; ?></td>
                                               <?php
                                                 $categ=$row['category']; 
                                                $sqlc=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM product_categories where category_id='$categ'"));
                                                 ?>
                                                <td><?php echo $sqlc['category_name']; ?></td>
                                                <td><?php echo $row['product_name']; ?></td>
                                                <td><?php echo $row['product_code']; ?></td>
                                               <!-- <td><img src="<?php// echo $row['ProductImage'];?>" alt="no image" width="100px" height="100px"/></td>-->
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td class="center"><?php echo number_format($row['price']); ?>/=</td>
                                               
                                               
                                            </tr>
                                            
      <?php


    }
} else {
    echo "0 results";
}

mysqli_close($con);


                                        ?>
                                            
                                        </tbody>
                                    </table>
                                  </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
		<?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       
 

<?php


if(isset($_POST['add'])){
	include '../includes/connection.php';
$category=mysqli_real_escape_string($con,ucwords($_POST['category']));
$product_name=mysqli_real_escape_string($con,ucwords($_POST['name']));
$pcode=mysqli_real_escape_string($con,$_POST['pcode']);
$sql0 = mysqli_query($con,"select * from products where product_name='$product_name'");
if(mysqli_num_rows($sql0)>0){
	echo"<script>alert('product already exists')</script>";
}
else{
	
//Image upload starts			
			$file=$_FILES['image']['tmp_name'];
			$photo_name= addslashes($_FILES['image']['name']);
			move_uploaded_file($_FILES["image"]["tmp_name"],"../uploads/products/". $_FILES["image"]["name"]);
			$url="../uploads/products/". $_FILES["image"]["name"];
		//image upload ends
$id_user=$_SESSION['id'];
  
$unit=$_POST['units'];
 $bad_symbols = array(",", ".");

$unitprice= mysqli_real_escape_string($con,$_POST['unitprice']);
$unitprice= str_replace($bad_symbols, "", $unitprice);
$sql = mysqli_query($con,"INSERT INTO products (category,product_name,product_code,ProductImage,Units, price,AddedBy)
VALUES ('$category','$product_name','$pcode','$url', '$unit','$unitprice','$id_user')");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Added a new product ".$product_name;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity) VALUES 
('$id_user','$activity')");

if ($log_sql) {

  ?>
    <meta http-equiv="refresh" content="0;URL='defaultdata.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
<script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>
    </body>
</html>
