<?php
$where 					= "";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}

###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "admin";
$paging_table_name	= $table_name;
$paging_query		= $where;
$page_header = "Access Permissions For : ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################

################################################## START BACK ##################################################
if(isset($_REQUEST['back']) && $_REQUEST['back'] == 'Back'){	
	header("Location: $file_name?page=view&message=Permission updated successfully");	
}

################################################## END BACK##################################################

################################################## START UPDATE PERMISSION ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update_permissins'){
    $id = $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM admin_group WHERE group_id = $id ");

	if(count($_REQUEST['modules'])){
	
		foreach($_REQUEST['modules'] as $module){
			$group_id = $id;
			$updated_by = $_SESSION['user_id'];
			$updated_on = utility::setFormatedDate(date($config->dateFormat));
			$sql = "INSERT INTO admin_group(group_id, module_id, updated_by, updated_on) VALUES('$group_id','$module','$updated_by','$updated_on')";
			mysqli_query($conn,$sql);
		}
	}	
	header("Location: $file_name?page=view&message=Permission updated successfully");	
}
################################################# END INSERT #################################################
$id = $_REQUEST['id'];

$show_modules ="";
if($_SESSION['is_super_admin'] != 1){
$show_modules = "AND m.display = '1'";
}
$query = "SELECT m.id,m.module,m.parent_id,m.display_name,m.display, am.module_id FROM module m LEFT JOIN admin_group am ON (am.module_id = m.id AND am.group_id=$id) WHERE m.parent_id =0  $show_modules ORDER BY m.ord";

$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

?>
<!--<section class="content-header">
  <h1>
	<?// echo $page_header; echo "<span style='color:red'>". utility::getTableFieldById("groups","group_name","id",$id)."</span>"; ?>
  </h1>
</section>-->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header; echo "<span style='color:red'>". utility::getTableFieldById("groups","group_name","id",$id)."</span>";?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
				<!----------------------- Message ------------------------------->
				<? if(isset($_SESSION['info_message'])){ ?>
				  <div class="alert alert-success alert-dismissable">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
				  </div>
				  <? } ?>
				<!----------------------- End Message ------------------------------->
					<div class="card-body">
					<!-- search form -->
					<?php //include_once('category_search_options.inc.php');?>
					<!-- /.search form -->
						<form action="index?page=view_permission&action=update_permissins&id=<?=$id?>" method="post" name="form1" id="form1">
						<div class="card-footer">
						   <button type="submit" name="add" class="btn btn-primary">Update</button>
						</div>
						<? if($result>0){?>
						 <table id="example1" class="table table-bordered table-striped">
							<thead>
							  <tr>
								<th>Name</th>
								<th>Module Name</th>
								<th>Permission</th>
							  </tr>
							</thead>
							<tbody>
								<?
									while($row =mysqli_fetch_array($sql)){ 
								?>
							  <tr>
								<td><b><?php echo $row['module']; ?><b></td>
								<td><?php echo ucfirst($row['display_module']); ?></td>
								<td><input type="checkbox" name="modules[]" value="<?php echo $row['id'];?>" <?php echo ($row['module_id']) ? "checked" : ""; ?>></td>
							  </tr>
								<? 
									$indentation="";
									$parent_id = $row['id'];
									utility::subModuleList($parent_id,$id,$indentation);
									} 
								?>
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=view";
									include("paging/paging_row.inc.php") ?>
								<!-- Paging ends starts here  -->
							</tbody>
						 </table>
						 <? }else{ ?>
							No module found...
						 <? } ?>
					</div>
					<div class="card-footer">
					   <button type="submit" name="back" class="btn btn-primary">Back</button>
					   <button type="submit" name="add" class="btn btn-primary">Update</button>
					</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>

