<?php
session_start();
require("php_con.php");
require("check_user.php");

if(isset($_GET['clear'])){
	unset($_SESSION['s_date']);
	unset($_SESSION['e_date']);
	$product_systemid=mysqli_real_escape_string($connation,$_GET['product_systemid']);
	echo "<script>window.location='full-stock-summery?product_systemid=$product_systemid';</script>";
}

if(isset($_POST['qty_update'])){
	$stock_id=mysqli_real_escape_string($connation,$_POST['stock_id']);
	$stock_qty=mysqli_real_escape_string($connation,$_POST['stock_qty']);
	$product_systemid=mysqli_real_escape_string($connation,$_GET['product_systemid']);
	if(mysqli_query($connation,"UPDATE stock_details SET stock_qty='$stock_qty' WHERE stock_id='$stock_id'")){
		echo "<script>window.location='full-stock-summery?product_systemid=$product_systemid&update';</script>";
	}
}

if(isset($_GET['remove'])){
	$stock_id=mysqli_real_escape_string($connation,$_GET['remove']);
	$product_systemid=mysqli_real_escape_string($connation,$_GET['product_systemid']);
	if(mysqli_query($connation,"DELETE FROM stock_details WHERE stock_id='$stock_id'")){
		echo "<script>window.location='full-stock-summery?product_systemid=$product_systemid&removed';</script>";
	}
}

if(isset($_POST['f_btn'])){
	$_SESSION['s_date']=mysqli_real_escape_string($connation,$_POST['s_date']);
	$_SESSION['e_date']=mysqli_real_escape_string($connation,$_POST['e_date']);
}

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">
<link rel="icon" href="favicon.ico">
<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;">Please Wait...</div></div>
	
	
<div class="contaner mt-4">
	

<div class="row">
	
<div class="table-responsive">
	
<form method="post" class="mb-2">
	<table class="table-sm table-borderless">
  <tbody>
    <tr>
      <td><input name="s_date" type="datetime-local" required="required" class="form-control form-control-sm" id="s_date" value="<?php if(isset($_POST['s_date'])){echo $_POST['s_date'];} ?>"></td>
      <td><input name="e_date" type="datetime-local" required="required" class="form-control form-control-sm" id="e_date" value="<?php if(isset($_POST['e_date'])){echo $_POST['e_date'];} ?>"></td>
      <td><button name="f_btn" type="submit" class="btn btn-sm btn-dark">Filter</button></td>
		<td><a href="full-stock-summery?product_systemid=<?php echo $_GET['product_systemid']; ?>&clear" class="btn btn-sm btn-dark">All Data</a></td>
    </tr>
  </tbody>
</table>
</form>
	
<table id="example" class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
<tr>
	<td></td>
<td>Time</td>
<td>Info</td>
<td>QTY</td>
<td>Type</td>
</tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;			
			if(isset($_SESSION['s_date'])&&isset($_SESSION['e_date'])){
			$qury_str="SELECT *
			FROM stock_details sd
			WHERE sd.stock_systemid='$_GET[product_systemid]' AND sd.stock_addtime BETWEEN '$_SESSION[s_date]' AND '$_SESSION[e_date]'
            ORDER BY sd.stock_id DESC";
			}
			else{
			$qury_str="SELECT *
			FROM stock_details sd
			WHERE sd.stock_systemid='$_GET[product_systemid]' ORDER BY sd.stock_id DESC";
			}
			
			$list_qury=mysqli_query($connation,"$qury_str LIMIT $start_page,$per_page");
			while($list_resalt=mysqli_fetch_array($list_qury)){
			$count_no++;
			?>
            <tr>
<td align="center"><a href="full-stock-summery?product_systemid=<?php echo $_GET['product_systemid']; ?>&remove=<?php echo $list_resalt['stock_id']; ?>" onClick="return confirm('Are you sure?');"><i class="fa fa-trash text-danger"></i></a></td>
<td><?php echo date_format(date_create($list_resalt['stock_addtime']),"M d, Y - h:i:s A") ?></td>
<td><?php echo $list_resalt['stock_info']; ?></td>
<td>
				
<form method="post" autocomplete="off">
<input name="stock_id" type="hidden" id="stock_id" value="<?php echo $list_resalt['stock_id']; ?>">
<input name="stock_qty" type="number" required="required" class="form-control form-control-sm" id="stock_qty" step="0.001" value="<?php echo $list_resalt['stock_qty']; ?>">
<button type="submit" name="qty_update" hidden="yes"></button>
</form>
	
</td>
<td align="center">
<?php if($list_resalt['stock_type']==1){ ?>
<span class="badge badge-success" style="font-weight: normal;">Stock In</span>
<?php } ?>
	
<?php if($list_resalt['stock_type']==0){ ?>
<span class="badge badge-danger" style="font-weight: normal;">Stock Out</span>
<?php } ?>
</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="full-stock-summery?page=1&product_systemid=<?php echo $_GET['product_systemid']; ?>" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>First</a>
<a href="full-stock-summery?page=<?php echo $page-1; ?>&product_systemid=<?php echo $_GET['product_systemid']; ?>" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>Previous</a>
<a href="full-stock-summery?page=<?php echo $page+1; ?>&product_systemid=<?php echo $_GET['product_systemid']; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Next</a>
<a href="full-stock-summery?page=<?php echo $page_count; ?>&product_systemid=<?php echo $_GET['product_systemid']; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Last</a>	
</div>
	
</div>		

		
	
</div>
</div>	
	

	
</body>
</html>
<?php @mysqli_close($connation); ?>