<?php
session_start();
require("php_con.php");
//require("check_user.php");
require("function.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']);
	mysqli_query($connation,"DELETE FROM stock_count WHERE sc_id='$remove'");
	mysqli_query($connation,"DELETE FROM stock_count_list WHERE scl_details_id='$remove'");
	echo "<script>window.location='stock_count';</script>";
}

if(isset($_POST['create_btn'])){
	$sc_date=mysqli_real_escape_string($connation,$_POST['sc_date']);
	$sc_select_cat=mysqli_real_escape_string($connation,$_POST['sc_select_cat']);
	if(mysqli_query($connation,"INSERT INTO
	stock_count (sc_id, sc_date, sc_select_cat, sc_user, sc_status, sc_c_date, sc_c_time)
	VALUES (NULL, '$sc_date', '$sc_select_cat', '$_COOKIE[user_id]', '0', '$current_date', '$current_time')")){
		
	$scl_details_id=mysqli_insert_id($connation);
		
	if($sc_select_cat=="all"){
		$qury=mysqli_query($connation,"SELECT * FROM product_details ORDER BY product_name ASC");
	}
	else{
		$qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_catagory='$sc_select_cat' ORDER BY product_name ASC");
	}
	while($p_resalt=mysqli_fetch_assoc($qury)){
	$scl_system_qty=availbel_qty($p_resalt['product_systemid']);
	mysqli_query($connation,"INSERT INTO
	stock_count_list (scl_id, scl_details_id, scl_product, scl_system_qty, scl_count_qty)
	VALUES (NULL, '$scl_details_id', '$p_resalt[product_id]', '$scl_system_qty', '0')");
	}
		
	}
	echo "<script>window.location='stock_count';</script>";
}

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="alert/sweetalert2.min.css">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<script src="alert/sweetalert2.min.js"></script>
</head>

<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">
	
<div class="row">
	
<div class="col-lg-5 col-sm-12">
	
<a href="stock?type=1" class="btn btn-sm btn-dark mb-2">Back to Stock</a><br>
	
<strong>Create Count Report</strong><br><br>

<form method="post">
<table>
	<tbody>
		<tr>
			<td>Valid Date: <input name="sc_date" type="date" required="required" class="form-control form-control-sm" id="sc_date" value="<?php echo date("Y-m-d"); ?>">
			</td>
			<td>Catogary:
				<select name="sc_select_cat" required="required" class="form-control form-control-sm" id="sc_select_cat">
					<option value="" hidden="yes">Select Category</option>
					<option value="all">All Category</option>
					<?php 
					$supplier_qury=mysqli_query($connation,"SELECT * FROM catogary_list ORDER BY catogary_text");
					while($supplier_resalt=mysqli_fetch_array($supplier_qury)){
					?>
					<option value="<?php echo $supplier_resalt['catogary_id']; ?>">
					<?php echo $supplier_resalt['catogary_text']; ?>
					</option>
					<?php } ?>
				</select>
			</td>
			<td><br><button name="create_btn" type="submit" class="btn btn-sm btn-dark">Crreate</button>
			</td>
		</tr>
	</tbody>
</table>
</form>
	
</div>
	
<table class="table-sm table table-bordered mt-4">
  <tbody>
    <tr class="table-active">
      <td>Valid Date</td>
      <td>Report ID</td>
      <td>Status</td>
      <td>Total Product</td>
      <td>Create</td>
      <td>&nbsp;</td>
    </tr>
<?php 
$qury_str="SELECT sc.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS u_name,COUNT(*) AS p_count
FROM stock_count sc INNER JOIN user_details ud ON sc.sc_user=ud.user_id
RIGHT JOIN stock_count_list scl ON sc.sc_id=scl.scl_details_id
GROUP BY sc.sc_id
ORDER BY sc.sc_id DESC";			
$list_qury=mysqli_query($connation,"$qury_str LIMIT $start_page,$per_page");
while($list_resalt=mysqli_fetch_array($list_qury)){	  
?>
    <tr>
      <td><?php echo $list_resalt['sc_date']; ?></td>
      <td><?php echo $list_resalt['sc_id']; ?></td>
      <td>Status</td>
      <td><?php echo number_format((float)$list_resalt['p_count']); ?></td>
      <td><?php echo $list_resalt['u_name']; ?><br><?php echo date("Y-m-d h:i:s A",strtotime($list_resalt['sc_c_date']." ".$list_resalt['sc_c_time'])); ?></td>
      <td align="center">
		
		  <a href="stock_count_update.php?scl_details_id=<?php echo $list_resalt['sc_id']; ?>&view" class="btn btn-sm btn-secondary"><i class="fa fa-eye"></i></a>
		  
		<?php if($list_resalt['sc_status']==0){ ?><a href="stock_count_print.php?scl_details_id=<?php echo $list_resalt['sc_id']; ?>" target="list_print" class="btn btn-sm btn-info"><i class="fa fa-print"></i></a><?php } ?>
		<?php if($list_resalt['sc_status']==1){ ?><a href="#" class="btn btn-sm btn-light border-dark"><i class="fa fa-print"></i></a><?php } ?>
		  
		  <?php if($list_resalt['sc_status']==0){ ?><a href="stock_count_update?scl_details_id=<?php echo $list_resalt['sc_id']; ?>" class="btn btn-sm btn-dark"><i class="fa fa-tasks"></i></a><?php } ?>
		  <?php if($list_resalt['sc_status']==1){ ?><a href="#" class="btn btn-sm btn-light border-dark"><i class="fa fa-tasks"></i></a><?php } ?>
		  
		  <?php if($list_resalt['sc_status']==0){ ?><a href="stock_count?remove=<?php echo $list_resalt['sc_id']; ?>" class="btn btn-sm btn-danger" onClick="return confirm('Are your sure?');"><i class="fa fa-trash"></i></a><?php } ?>
		  <?php if($list_resalt['sc_status']==1){ ?><a href="#" class="btn btn-sm btn-light border-dark"><i class="fa fa-trash"></i></a><?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="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>	

<iframe name="list_print" style="display: none;"></iframe>
	
</body>
</html>
<?php @mysqli_close($connation); ?>