<?php
session_start();
require_once 'includes/config.php';

if (!isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in'] != TRUE) {
    header('Location:login.php');
}



if(isset($_GET['del_id']) && $_GET['del_id']!=''){
	$deleteEmployee=mysqli_query($con,"DELETE FROM products WHERE id=".$_GET['del_id']);
	if($deleteEmployee){
		$del_stat=1;
		$message='Products deleted successfully.';
	}
}
$users=mysqli_query($con,'SELECT * FROM products where user_id=0 ORDER BY id DESC');

require_once 'includes/header.php';
?>




<?php
if (isset($del_stat) && $del_stat == 1) {
	echo '<div class="alert alert-info">'.$message.'</div>';
	}
?>
<div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
			<a href="add_product.php" class="btn btn-primary btn-round">Add Product <div class="ripple-container"></div></a>

  
<div class="card">
                <div class="card-header card-header-primary">
                  <h4 class="card-title ">All Products</h4>
                  <p class="card-category"> </p>
                </div>
                <div class="card-body">
                  <div class="table-responsive">
	  <table class="table ">
		<tr>
		  <th>No.</th>
		 
		  <th>Category Name</th> 		  
		  <th> Product Name</th> 		  
		  <th> Price</th> 		  
		  		  
		  <th> Product Image</th> 		  
		  
		  	  
		  <th>Action</th>
		  
		</tr>
		<?php
		if(mysqli_num_rows($users)>0){	
		$i=1;
		while($user=mysqli_fetch_array($users)){?>
		<tr>
		<td><?php echo $i;?></td>
		
		<td><?php $cat=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM categories WHERE id=".$user['cate_id'])); echo $cat['name'];?></td>
		<td><?php echo $user['name'];?></td>
		<td><?php echo $user['price'];?></td>
		
		<?php $img=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM product_img WHERE product_id=".$user['id']." LIMIT 1")); ?>
		<td><img src="<?php echo $img['image'];?>" width="100" height="100" alt=""/></td>
		
		<td>
		<a href="edit_product.php?id=<?php echo $user['id'];?>" title="Edit Product" class="btn btn-success"><i class="fa fa-edit"></i></a>
		<a href="products.php?del_id=<?php echo $user['id'];?>" class="btn btn-danger delete_btn" title="Products"><i class="fa fa-trash"></i></a>
		</td>
		
		</tr>	
		<?php $i++;}
		}
		?>   
	  </table>
 </div>
                </div>
              </div>
            </div>
            
          </div>
        </div>
      </div>
<?php include_once 'includes/footer.php'; ?>

<script type="text/javascript">
    $(document).ready(function () {
        $('.delete_btn').click(function () {
            var r = confirm("Are you sure?")
            if (r == true) {
                return true;
            } else {
                return false;
            }
        });
    });
</script> 