<?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 = "Admin User Access Permissions";
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=admin_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_module WHERE admin_id = $id ");

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

$id = $_REQUEST['id'];
$query = "SELECT m.id,m.module,am.module_id FROM module m LEFT JOIN admin_module am ON (am.module_id = m.id AND am.admin_id=$id)";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

?>
<td width="100%" align="center" valign="top" bgcolor="" class="red">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
	<tr>
		<td valign="middle" height="10" align="center">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td height="10" align="center" class="head_ing"><? echo $page_header; ?></td>
				<!--<td width="368"  align="right" class="red">&nbsp;<? include("paging/no_of_records.inc.php"); ?></td>-->
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td bordercolor="#FFFFFF" valign="top" align="center">
		<table width="100%" border="0" cellspacing="4" cellpadding="0">
			<? if(isset($_REQUEST['message']) && $_REQUEST['message']!=''){ ?>
			<tr>
				<td align="center" class="red"><?=$_REQUEST['message'];?></td>
			</tr>
			<? }?>
			<tr>
				<td align="center" valign="top">
				<form action="index?page=view_permission&action=update_permissins&id=<?=$id?>" method="post" name="form1" id="form1">
				<input name="add" type="submit" value="Update" />
				<table width="40%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
					<? if($result>0){?>
					<!-- Start  records header -->
					<tr align="center" bgcolor="#000000">
						<td width="80%" class="white">Module Name</td>
						<td width="20%" class="white">Permission</td>
					</tr>
					<!-- End records header -->
					<?
					while($row =mysqli_fetch_array($sql))
					{ ?>
					<!-- Start data -->
					<tr align="center" bgcolor="#000000">
						<td align="left" bgcolor="#FFFFFF"><?php echo ucfirst($row['module']); ?></td>
						<td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="modules[]" value="<?php echo $row['id'];?>" <?php echo ($row['module_id']) ? "checked" : ""; ?>></td>
					</tr>
					<!-- End data -->
					<?
					}
					}else{?>
					<tr bgcolor="#000000">
						<td height="33" colspan="7" align="center" bgcolor="#000000"></td>
					</tr>
					<tr bgcolor="#ffffff">
						<td height="33" colspan="7" align="center" bgcolor="#ffffff">No	module found...</td>
					</tr>
					<?php }?>
					<tr bgcolor="#000000">
						<!--  <td height="25%" align="right" valign="top"  >&nbsp;</td> -->
						<td colspan="20" height="33"  align="center" bgcolor="#000000">
						<input name="back" type="submit" value="Back" />
						<input name="add" type="submit" value="Update" /> </td>
					</tr>
					<!-- Paging starts here  -->
					<?
					$next_page_condition .="&page=agent_view";
					include("paging/paging_row.inc.php") ?>
					<!-- Paging ends starts here  -->

				</table>
				</form>
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>
</td>
