<?php
session_start();
require("php_con.php");
//require("check_user.php");
if(isset($_COOKIE['user_id'])){
}
else{
	echo "<script>window.location='logout';</script>";
}

if(isset($_GET['remove'])){
	$remove=mysqli_real_escape_string($connation,$_GET['remove']);
	
	$check_qury=mysqli_query($connation,"SELECT pd.product_systemid FROM sale_list sl LEFT JOIN product_details pd ON sl.sale_barcode=pd.product_barcode WHERE pd.product_systemid='$remove'");
	if(mysqli_num_rows($check_qury)>0){
		echo "<script>window.location='product?remove_fail';</script>";
	}
	else{
	mysqli_query($connation,"DELETE FROM product_details WHERE product_systemid='$remove'");
	mysqli_query($connation,"DELETE FROM price_details WHERE price_systemid='$remove'");
	mysqli_query($connation,"DELETE FROM stock_details WHERE stock_systemid='$remove'");		
	echo "<script>window.location='product?remove_success';</script>";		
	}
}

if(isset($_POST['search_text'])){
	$_SESSION['search_text']=mysqli_real_escape_string($connation,$_POST['search_text']);
	echo "<script>window.location='product';</script>";
}

if(isset($_GET['clear'])){
	unset($_SESSION['search_text']);
	unset($_SESSION['page']);
	echo "<script>window.location='product';</script>";
}

if(isset($_GET['page'])){
	$_SESSION['page']=mysqli_real_escape_string($connation,$_GET['page']);
}

if(isset($_SESSION['page'])){
$page=$_SESSION['page'];
}
else{
$page=1;
}
$per_page=50;
$start_page=($page-1)*$per_page;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>
<style>
	.table tr td{
		font-size: 14px;
		padding: 5px;
	}
	.table tr th{
		font-size: 14px;
		padding: 5px;
	}
</style>
<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">

	
<strong>Product Details</strong>
	
<?php require("sub_menu/product.php"); ?>
	
<div class="row">
	
<div class="table-responsive">
	
<form method="post" class="mb-2">
	<input name="search_text" type="text" autofocus="autofocus" class="form-control form-control-sm" id="search_text" placeholder="Search Product" list="product_list">
</form>
	
<table id="example" class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th>No.</th>
				<th>&nbsp;</th>
				<th>Bar./Pro. ID</th>
                <th>Product Name</th>
				<th>Supplier</th>
                <th>Buy Price</th>
                <th>Sale Price</th>
				<th>Dis. Price</th>
				<th>QTY</th>
                <th>Mini. Level</th>
				<th>Dis. On/Off</th>
				<th>Add Time</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			
			if(isset($_SESSION['search_text'])){
			$qury_str="SELECT pd.*,sd.supplier_name
			FROM product_details pd LEFT JOIN supplier_details sd ON pd.product_supplier=sd.supplier_id
			WHERE product_barcode LIKE '%$_SESSION[search_text]%' OR product_productid LIKE '%$_SESSION[search_text]%' OR product_name LIKE '%$_SESSION[search_text]%' OR product_name_sinhala LIKE '%$_SESSION[search_text]%'
			ORDER BY pd.product_barcode";
			}
			else{
			$qury_str="SELECT pd.*,sd.supplier_name
			FROM product_details pd LEFT JOIN supplier_details sd ON pd.product_supplier=sd.supplier_id
			ORDER BY pd.product_barcode";
			}
			
			$list_qury=mysqli_query($connation,"$qury_str LIMIT $start_page,$per_page");
			while($list_resalt=mysqli_fetch_array($list_qury)){
			$count_no++;
				
			$price_qury=mysqli_query($connation,"SELECT * FROM price_details WHERE price_systemid='$list_resalt[product_systemid]' ORDER BY price_id DESC");
			$price_resalt=mysqli_fetch_array($price_qury);
			
			$in=0;
			$out=0;
			$stock_qury=mysqli_query($connation,"SELECT * FROM stock_details WHERE stock_systemid='$list_resalt[product_systemid]' ORDER BY stock_id DESC");
			while($stock_resalt=mysqli_fetch_array($stock_qury)){

			if($stock_resalt['stock_type']==1){
			$in=$in+$stock_resalt['stock_qty'];
			}
			if($stock_resalt['stock_type']==0){
			$out=$out+$stock_resalt['stock_qty'];
			}

			}
				
			$availbel_stock=$in-$out;
			?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
				<td style="white-space: nowrap; text-align: center">
				<a href="edit-product?product_id=<?php echo $list_resalt['product_id']; ?>" class="badge badge-primary" title="Edit Product"><i class="fa fa-edit"></i></a>
				<a href="product?remove=<?php echo $list_resalt['product_systemid']; ?>" class="badge badge-danger" title="Remove Product" onClick="JavaScript:return confirm('Are your sure remove this product?');"><i class="fa fa-trash"></i></a>
				<a href="price-manage?price_systemid=<?php echo $list_resalt['product_systemid']; ?>" class="badge badge-dark" title="Price Manage"><i class="fa fa-tasks"></i></a>					
				<a href="barcode-create?product_barcode=<?php echo $list_resalt['product_barcode']; ?>" class="badge badge-info"><i class="fa fa-barcode"></i></a>					
				</td>
                <td><?php echo $list_resalt['product_barcode']; ?><br><em class="text-secondary"><?php echo $list_resalt['product_productid']; ?></em></td>
                <td><?php echo $list_resalt['product_name']; ?><br><?php echo $list_resalt['product_name_sinhala']; ?></td>
				<td><?php echo $list_resalt['supplier_name']; ?></td>
                <td align="right"><?php echo number_format($price_resalt['price_buy'],2); ?></td>
                <td align="right"><b><?php echo number_format($price_resalt['price_sale'],2); ?></b></td>
				<td align="right"><?php echo number_format($price_resalt['price_discount'],2); ?></td>
				<td align="right"><?php echo number_format($availbel_stock,0); ?></td>
                <td align="right"><?php echo number_format($list_resalt['product_alertqty'],0); ?></td>
				<td align="center">
					<?php if($list_resalt['product_add_discount']==0){ ?>
					<span class="badge badge-danger">Off</span>
					<?php } ?>
					
					<?php if($list_resalt['product_add_discount']==1){ ?>
					<span class="badge badge-success">On</span>
					<?php } ?>
				</td>
				<td><?php echo date_format(date_create($list_resalt['product_addtime']),"M d, Y - h:i:s A"); ?></td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
	
<?php 
$total_record_qury=mysqli_query($connation,"$qury_str");
$total_record=mysqli_num_rows($total_record_qury);
$page_count=ceil($total_record/$per_page);
?>
<div class="text-right">
<span class="mr-2"><?php echo "Showing ".number_format((float)($start_page+1))."/".number_format((float)($start_page+$per_page))." page of ".number_format((float)$total_record)." entries"; ?></span>
<a href="product?page=1" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>First</a>
<a href="product?page=<?php echo $page-1; ?>" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>Previous</a>
<a href="product?page=<?php echo $page+1; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Next</a>
<a href="product?page=<?php echo $page_count; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Last</a>	
</div>
	
</div>			
	
</div>
</div>	
	
	
<datalist id="product_list">
	<?php 
	$sug_list_qury=mysqli_query($connation,"SELECT * FROM product_details ORDER BY product_name");
	while($sug_list_resalt=mysqli_fetch_array($sug_list_qury)){
	?>
	<option value="<?php echo $sug_list_resalt['product_barcode']; ?>"><?php echo $sug_list_resalt['product_productid']." - ".$sug_list_resalt['product_name']." | ".$sug_list_resalt['product_name_sinhala']; ?></option>
	<?php } ?>
</datalist>
	
</body>
</html>
<?php @mysqli_close($connation); ?>