<?php 
include 'head.php';

include('xcrud/xcrud.php');
$xcrud = Xcrud::get_instance();
echo Xcrud::load_css(); 

include 'nav.php';
?>
<div class="content-wrapper">
	<section class="content-header">
		<h1>Admin</h1>
    </section>

    <section class="content">      
		<div class="row">
			<div class="col-lg-12 col-xs-12">
			  <?php
				$xcrud->table('admin');
				$xcrud->unset_title();
				$xcrud->validation_required("username,password,email,type");
				$xcrud->change_type('password', 'password', 'sha256', 16);
				//$xcrud->disabled('password','edit');
				//$xcrud->unset_edit(true,'username','=','killer'); 
				$xcrud->unset_remove(true,'id','=','1');
				$xcrud->button('javascript:void()','User Permission','fa fa-lock','',array('onclick'=>'userPermission({id})','data-toggle'=>'modal','data-target'=>'#userPermissionModal'));
				echo $xcrud->render();
			  ?>
			</div>
		</div>
    </section>
	
</div>

<div class="modal fade" id="userPermissionModal" role="dialog">
	<div class="modal-dialog modal-sm">
	  <!-- Modal content-->
	  <div class="modal-content">
		<div class="modal-header">
		  <button type="button" class="close" data-dismiss="modal">&times;</button>
		  <h4 class="modal-title">User Permission</h4>
		</div>
		<div class="modal-body" >
			<form method="post">
				<table class='table table-striped'>
					<thead class="thead-dark"><tr><th scope="col">Page</th><th scope="col">#</th></tr></thead>
					<tbody id="permission_div"></tbody>
					<tfoot><tr><td><button type="submit"  name="save_permission" class="btn btn-info">Save</button></td></tr></tfoot>
				</table>
			</form>
		</div>
		<div class="modal-footer">
		  <button type="buuton" class="btn btn-default" data-dismiss="modal">Close</button>
		</div>
	  </div>
	</div>
</div>
<?php
if(isset($_POST['save_permission'])){
	$user_id=$_POST['hidden_user_id_for_permission'];
	$bdd=new DB();
	$values=array($user_id);
	$page_permission_rows=$bdd->getAll("SELECT permission_pages_id FROM `admin_permission` WHERE ticl_admin_id=?",$values);
	$page_array=array();
	foreach($page_permission_rows as $page_permission_row){
		array_push($page_array,$page_permission_row['permission_pages_id']);
	}
	$values=array();
	$page_rows=$bdd->getAll("SELECT * FROM permission_pages",$values);
	
	/*foreach($page_rows as $page_row){
		if(in_array($page_row['id'],$_POST['page_name'])){
			if(!in_array($page_row['id'],$page_permission)){
				$pp_value=array($user_id,$page_row['id']);
				$bdd->execute("INSERT INTO admin_permission (`ticl_admin_id`, `permission_pages_id`) VALUES (?,?)",$pp_value);
			}else{
				/*$pp_value=array($user_id,$page_row['id']);
				$bdd->execute("DELETE FROM admin_permission WHERE ticl_admin_id=? AND permission_pages_id=?)",$pp_value);
			}
		}
	}*/
	if(!empty($_POST['page_name'])){
		foreach($_POST['page_name'] as $selected){
			if(!in_array($selected,$page_array)){
				$pp_value=array($user_id,$selected);
				$bdd->execute("INSERT INTO admin_permission (`ticl_admin_id`, `permission_pages_id`) VALUES (?,?)",$pp_value);
			}else{
				/*$pp_value=array();
				$bdd->execute("DELETE FROM admin_permission WHERE ticl_admin_id=$user_id AND permission_pages_id=$selected",$pp_value);*/
			}
		}
		foreach($page_array as $page){
			if(!in_array($page,$_POST['page_name'])){
				$pp_value=array();
				$bdd->execute("DELETE FROM admin_permission WHERE ticl_admin_id=$user_id AND permission_pages_id=$page",$pp_value);
			}
		}
	}else{
		if(!empty($page_permission_rows)){
			foreach($page_permission_rows as $page_permission_row){
				$pp_value=array();
				$bdd->execute("DELETE FROM admin_permission WHERE ticl_admin_id=$user_id AND permission_pages_id=".$page_permission_row['permission_pages_id'],$pp_value);
			}
		}
	}
	$bdd->disconnect();
}
include 'footer.php';
echo Xcrud::load_js();
?>
<script>
function userPermission(id){
	$.ajax({
		type:'POST',
		data:'action=userPermission&id='+id,
		url:'ajax_data.php',
		success:function(data){
			$("#permission_div").html(data)
		}
	});
}
</script>
