
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!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>
<?php
    include("../includes/header-links.php");
    
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['nav_products'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    ?>
    <head>

    
    <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'; ?>
        <?php
                                                $checkarrangement=mysqli_fetch_array(mysqli_query($conn,"SELECT * from receipt_settings where branch_id='$branch'"));
                                                $arrange_products=$checkarrangement['arrange_products'];

                                                
                                                
                                                   ?>
        <div class="container-fluid">
    
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">
          

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
            
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Services </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="btn-group">
                                       <a href="products.php" ><button class="btn btn-warning"> <i class="icon-indent-left"></i> Products</button></a>
                                     </div>
                                    
                                      

                               
                                   
                                   <?php
include '../includes/connection.php';
       // if($_SESSION['role']=="Super Admin"){
if($permissions['business_id'] != 0 && $permissions['branch_id']==0){

               if ($arrange_products=="Aphabetical Order") {
       
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=0 && business_id='$business' order by product_name");
             
             }else if ($arrange_products=="By Category") {
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=0 && business_id='$business' order by category");
        
     
              }else if ($arrange_products=="By Quantity") {
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=0 && business_id='$business' order by quantity DESC");

                }else{
                  $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=0 && business_id='$business' ");

                }
        
      }else{

               if ($arrange_products=="Aphabetical Order") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=0 && branch_id='$branch' order by product_name");
               

             }else if ($arrange_products=="By Category") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=0 && branch_id='$branch' order by category");
     
              }else if ($arrange_products=="By Quantity") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=0 && branch_id='$branch' order by quantity DESC");

                }else{
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=0 && branch_id='$branch'");
                 
                }
        
      }                // Create connection

//$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>Price</th>
                                                <?php if($business_type==1){ ?>
                                                <th>Wholesale price</th>
                                                <?php } ?>
                                                <?php 
                                                //if($_SESSION['role']=="admin"){ 
                                                // if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                if( $permissions['edit_product'] == 1){
                                                ?>
                                                <th>Action</th>
                                                <?php } ?>

                                             </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $no=0;
while($row = mysqli_fetch_assoc($sql)) {
   $no++;

        ?>
<tr class="gradeU">
                                                <td><?php echo $no; ?></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 class="center"><?php echo number_format($row['price']); ?></td>
                                                <?php if($business_type==1){ ?>
                                                <td class="center"><?php echo number_format($row['price_whole']); ?></td>
                                                <?php } ?>
                                                <?php
                                                // if($_SESSION['role']=="admin"){
                                                if( $permissions['edit_product'] == 1){
                                                 ?>
                                                <td class="center">
                                                <?php $newid=$row['id'];
                                                    $prodname=$row['product_name'];?>
                                                
                                                  <a href="editproduct.php?id=<?php echo $row['id']; ?>"><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                  <a href="deleteconfirm.php?id=<?php echo base64_encode($row['id']); ?>&prodname=<?php echo base64_encode($prodname); ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                  
                  <div id="myAlert" class="modal hide">
                      <div class="modal-header">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Warning !</h3>
                      </div>
                      <div class="modal-body">
                        <p>Are you sure you want to delete this product?</p>
                      </div>
                      <div class="modal-footer">
                        <a href="deleteproduct.php?id=<?php echo $newid; ?>"><button class="btn btn-primary"> Yes</button></a>
                        <a data-dismiss="modal" class="btn" href="#">No</a>
                      </div>
                    </div>
                                            
                                                </td>
                                                <?php 
                                              } 
                                              ?>
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "No products added yet!";
}

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======-->
       
 


<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>
