<?php

include 'load.php';
$res_list = mysqli_query($conn, "SELECT * FROM pages");
$usergroups = mysqli_query($conn, "SELECT * FROM user_groups");
$pages = mysqli_query($conn, "SELECT * FROM pages");
function permissionStatus($conn, $groupid, $pageid){
	if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM user_permissions WHERE user_group_id='$groupid' AND page_id='$pageid'"))==1){
		return true;
	}else{
		return false;
	}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Eat More Resturant   | Pos & Invetory System | Dashboard

">
<meta name="keywords" content="Eat More Resturant   | Pos & Invetory System | Dashboard

">
<meta name="author" content="Eat More Resturant   | Pos & Invetory System | Dashboard

">
<meta name="robots" content="noindex, nofollow">

<title>Home | Eat More Resturant   | Pos & Invetory System | Dashboard
</title>


<?php
require_once 'headercss.php';
?>
</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>User Permission list</h4>
<h6>View/Search User Permission</h6>
</div>

</div>

<div class="card">
<div class="card-body">
<div class="table-top">
<div class="search-set">
<div class="search-path">
<a class="btn btn-filter" id="filter_search">
<img src="assets/img/icons/filter.svg" alt="img">
<span><img src="assets/img/icons/closes.svg" alt="img"></span>
</a>
</div>
<div class="search-input">
<a class="btn btn-searchset"><img src="assets/img/icons/search-white.svg" alt="img"></a>
</div>
</div>
<div class="wordset">
<ul>
<li>
<a data-bs-toggle="tooltip" data-bs-placement="top" title="pdf"><img src="assets/img/icons/pdf.svg" alt="img"></a>
</li>
<li>
<a data-bs-toggle="tooltip" data-bs-placement="top" title="excel"><img src="assets/img/icons/excel.svg" alt="img"></a>
</li>
<li>
<a data-bs-toggle="tooltip" data-bs-placement="top" title="print"><img src="assets/img/icons/printer.svg" alt="img"></a>
</li>
</ul>
</div>
</div>

<div class="table-responsive">
<table class="table dtable" id="dtable">
<thead>
<tr>
                                    <th>Module</th>
                                    <th>Page</th>
                                    <th>Action</th>
                                    <?php
                                     $pageidarr = array();
                                     foreach ($usergroups as $usergroups) {
                                       echo '<th>'.$usergroups['group_name'].'</th>'; 
                                       array_push($pageidarr, $usergroups['group_id']);
                                     }
     
                                    ?>
</tr>
</thead>
<tbody> 
<?php
                                        foreach ($pages as $key => $value){ ?>
                                            <tr>
                                                <td><?php echo $value['module_name']; ?></td>
                                                <td><?php echo $value['page_name']; ?></td>
                                                <td><?php echo $value['action']; ?></td>
                                                <?php foreach($pageidarr as $item) {
                                                    if(permissionStatus($conn, $item, $value['page_id'])){
                                                        $check = "checked";
                                                    }else{
                                                        $check = "";
                                                    } ?>
                                                    <td><input type="checkbox" group-id="<?php echo $item; ?>" page-id="<?php echo $value['page_id']; ?>" class="permissioncheckbox" name="permissioncheckbox" value="1" <?php echo $check; ?>></td>
                                                <?php } ?>
                                             </tr>
                                      <?php } ?>                              

</tbody>
</table>
</div>
</div>
</div>

</div>
</div>
</div>


<script src="assets/js/jquery-3.6.0.min.js"></script>

<script src="assets/js/feather.min.js"></script>

<script src="assets/js/jquery.slimscroll.min.js"></script>

<script src="assets/js/jquery.dataTables.min.js"></script>
<script src="assets/js/dataTables.bootstrap4.min.js"></script>

<script src="assets/js/bootstrap.bundle.min.js"></script>

<script src="assets/plugins/select2/js/select2.min.js"></script>

<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="assets/js/script.js"></script>
<script>

    $( document ).ready(function() {
		//$('#dtable').DataTable();

		const table = $('#dtable').DataTable({
    drawCallback: function(settings) {
		$('.permissioncheckbox').change(function() {
	  		var groupid = $(this).attr("group-id");
	  		var pageid = $(this).attr("page-id");
	  		var check = 0;
	  		if(this.checked) {
	  			check=1;
	  			$.ajax({
			  		type: "POST",
					url: "permissions-update.php",
					data:{groupid:groupid,pageid:pageid,check:check},
					success: function(data){
                        toastr.success('permission updated!');
						 
			  		}
				});
	  		}else{
	  			check=0;
	  			$.ajax({
			  		type: "POST",
					url: "permissions-update.php",
					data:{groupid:groupid,pageid:pageid,check:check},
					success: function(data){
                        toastr.success('permission updated!');
						   
			  		}
				});
	  		}
	  });
    }
});


	 
});
</script>




</body>
</html>