<?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'])){
	$catogary_id=mysqli_real_escape_string($connation,$_GET['remove']);
	if(mysqli_query($connation,"DELETE FROM catogary_list WHERE catogary_id='$catogary_id'")){
	echo "<script>window.location='category-manage';</script>";
	}
}


if(isset($_POST['add_cat'])){
	$catogary_text=mysqli_real_escape_string($connation,$_POST['catogary_text']);
	if(empty($_POST['catogary_pin_to_pos'])){$catogary_pin_to_pos=0;}else{$catogary_pin_to_pos=1;}
	mysqli_query($connation,"INSERT INTO catogary_list(catogary_text,catogary_pin_to_pos) VALUES ('$catogary_text','$catogary_pin_to_pos')");
}

if(isset($_POST['update_bt'])){
	$catogary_id=mysqli_real_escape_string($connation,$_GET['catogary_id']);
	$catogary_text=mysqli_real_escape_string($connation,$_POST['catogary_text']);
	if(empty($_POST['catogary_pin_to_pos'])){$catogary_pin_to_pos=0;}else{$catogary_pin_to_pos=1;}
	mysqli_query($connation,"UPDATE catogary_list SET catogary_text='$catogary_text',catogary_pin_to_pos='$catogary_pin_to_pos' WHERE catogary_id='$catogary_id'");
}

if(isset($_GET['catogary_id'])){
$catogary_id=mysqli_real_escape_string($connation,$_GET['catogary_id']);
$edit_qury=mysqli_query($connation,"SELECT * FROM catogary_list WHERE catogary_id='$catogary_id'");
$edit_expresalt=mysqli_fetch_array($edit_qury);
}
?>
<!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>

<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>Category Manage</strong>
	
<div style="text-align: right"><button type="button" class="btn btn-dark btn-sm" onClick="JavaScript:window.location='product';"><i class="fa fa-backward"></i> Back Product</button></div>
	
<form method="post">
	
<table>
  <tbody>
    <tr>
      <td>Category Name<input name="catogary_text" type="text" class="form-control form-control-sm" id="catogary_text" required value="<?php if(isset($_GET['catogary_id'])){echo $edit_expresalt['catogary_text'];} ?>"></td>
      <td class="pt-2 pl-2 pr-2"><br><label class="form-control form-control-sm"><input name="catogary_pin_to_pos" type="checkbox" id="catogary_pin_to_pos" <?php if(isset($_GET['catogary_id'])){ if($edit_expresalt['catogary_pin_to_pos']==1){echo "checked";} } ?>> Pin to POS</label></td>
      <td><br><button type="submit" name="<?php if(isset($_GET['catogary_id'])){echo "update_bt";}else{echo "add_cat";} ?>" class="btn btn-success btn-sm"><i class="fa fa-check"></i> <?php if(isset($_GET['catogary_id'])){echo "Edit Category";}else{echo "Add New Category";} ?></button></td>
    </tr>
  </tbody>
</table>

</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>Category</th>
				<th>Pin to POS</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$exp_qury=mysqli_query($connation,"SELECT * FROM catogary_list ORDER BY catogary_text");
			while($exp_resalt=mysqli_fetch_array($exp_qury)){
			$count_no++;
			?>
            <tr>
                <td style="width: 100px;"><?php echo number_format($count_no,0); ?></td>
				<td style="white-space: nowrap; width: 50px;">
				<a href="category-manage?catogary_id=<?php echo $exp_resalt['catogary_id']; ?>" class="badge badge-info"><i class="fa fa-edit"></i></a>
				<a href="category-manage?remove=<?php echo $exp_resalt['catogary_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this expenses?');"><i class="fa fa-trash"></i></a>
				</td>
				<td><?php echo $exp_resalt['catogary_text']; ?></td>
				<td>
				<?php if($exp_resalt['catogary_pin_to_pos']==1){ ?><i class="fa fa-check"></i> Yes<?php } ?>
				<?php if($exp_resalt['catogary_pin_to_pos']==0){ ?><i class="fa fa-close"></i> No<?php } ?>
				</td>
            </tr>
            <?php
			}
			?>
        </tbody>
    </table>

	
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
		
</div>	
	

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