<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmePermissioninfo($db,'');
include_once "includes/pagepermission.php";

//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
//check permission - END

$id=$_REQUEST['id'];
if($id!="")
{
	
//check edit permission - START	
if(trim($res_modm_prm['EditPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END	

$operation="Edit";
$act="update";
$btn='Update';

$edit_id = base64_decode($id);

$resrole_name = $db->get_a_line("select RoleName from a_roles where 1=1 and RoleId='".$edit_id."' ");

//$str_ed = "select group_concat(`ModuleId`) as modulelist from a_modulemenus where 1=1 and `IsActive`=1 and `MenuId` =".$edit_id."  ";
//$res_ed = $db->get_a_line($str_ed);

//$module_listall = $res_ed['modulelist'];
//$module_listarray = explode(",",$module_listall); 
}
else
{
	
//check edit permission - START	
if(trim($res_modm_prm['AddPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END

	$operation="Add";
	$act="insert";
	$btn='Save';
}
?>  





<div class="wrapper">
    <div class="container">
        <!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-12">										
                <h4 class="page-title"><?php echo $operation; ?> Permissions</h4>				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
            <div class="col-sm-12">
				<form id="jvalidate" name="frmPermission" role="form" class="form-horizontal" action="#" method="post" >
				<input type="hidden" name="action" value="<?php echo $act; ?>" />
				<input type="hidden" name="edit_id" value="<?php echo $edit_id; ?>"  />
				
				<div class="card-box">					
					<div class="form-group row">
                        <label class="col-sm-2 form-control-label">Role Name</label>
                        <div class="col-md-10">
                            <input disabled id="txtRolename" class="form-control jsrequired" type="text" value="<?php echo $resrole_name['RoleName']; ?>" name="txtRolename">
                        </div>
                    </div>
					
					
					<div class="form-group row">	
										<label class="col-sm-2 form-control-label">&nbsp;</label>
										<div class="col-md-10">							
											<div class="box box-default">
												<div class="box-header with-border">													
													<!--<h3 class="box-title">Permission List</h3>-->
												</div>
											</div>	
											<div class="box-body">								
												<?php
													$mainmenu_list = $db->get_rsltset("select t1.MenuId,t2.MenuName from a_modulemenus t1 inner join a_menus t2 on t1.MenuId = t2.MenuId and t2.IsActive =1 where 1=1 and t1.IsActive = 1 group by t1.MenuId");
													foreach($mainmenu_list as $mainmenu_list_S)
													{
												?>													
													<h4 class="page-title"><?php echo $mainmenu_list_S['MenuName']; ?></h4> 
													<table class="table table-striped">
													<thead>
														<tr>
															<th style="width:30%;">Page Name</th>
															<th style="width:15%;"><input type="checkbox" id="AdPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" /> Add </th>
															<th style="width:15%;"><input type="checkbox" id="EdPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" /> Edit</th>
															<th style="width:15%;"><input type="checkbox" id="DePrm_<?php echo $mainmenu_list_S['MenuId']; ?>" /> Delete</th>
															<th style="width:15%;"><input type="checkbox" id="ViPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" /> View</th>
														</tr>										
													</thead>
													<tbody>
													<?php 
													 
													 if($edit_id == 1){
														$page_list =$db->get_rsltset("select t1.*,t2.MenuName,t3.ModuleName,t3.Description,t3.ModulePath from a_modulemenus t1 
														inner join a_menus t2 on t1.MenuId = t2.MenuId and t2.IsActive =1 
														inner join a_modules t3 on t1.ModuleId = t3.ModuleId and t3.IsActive =1 
														where 1=1 and  t1.IsActive =1 and t1.MenuId='".$mainmenu_list_S['MenuId']."' order by t1.moduleId asc ");
													 }
													 else{
														$page_list =$db->get_rsltset("select t1.*,t2.MenuName,t3.ModuleName,t3.Description,t3.ModulePath from a_modulemenus t1 
														inner join a_menus t2 on t1.MenuId = t2.MenuId and t2.IsActive =1 
														inner join a_modules t3 on t1.ModuleId = t3.ModuleId and t3.IsActive =1 
														where 1=1 and  t1.IsActive =1 and t1.MenuId='".$mainmenu_list_S['MenuId']."' and t1.ModuleId NOT IN(1,2,3) order by t1.moduleId asc ");
													 }	
													 
													 foreach($page_list as $page_list_S)
													 {
													   $pagepermission_all = $db->get_a_line("select * from a_user_acl where 1=1 and IsActive =1 and RoleId='".$edit_id."' and  ModuleMenuId='".$page_list_S['ModuleMenuId']."' ");									   
													 ?>	 
													   
													 <tr>
														<td> <?php echo $page_list_S['Description']; ?></td>
														<td>
																<div class="icheck">
																	<label>
																	<input class="AdPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" type="checkbox" name="AddPrm_<?php echo $page_list_S['ModuleMenuId']; ?>" <?php if($pagepermission_all['AddPrm'] == 1 ) echo "checked"; ?>  />
													
																	</label>
																</div>
														</td>
														<td>
																<div class="icheck">
																	<label>
																		<input class="EdPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" type="checkbox" name="EditPrm_<?php echo $page_list_S['ModuleMenuId']; ?>"  <?php if($pagepermission_all['EditPrm'] == 1 ) echo "checked"; ?>  />
													
																	</label>
																</div>
														</td>
														<td>
																<div class="icheck">
																	<label>
																		<input class="DePrm_<?php echo $mainmenu_list_S['MenuId']; ?>" type="checkbox" name="DeletePrm_<?php echo $page_list_S['ModuleMenuId']; ?>" <?php if($pagepermission_all['DeletePrm'] == 1 ) echo "checked"; ?>  />
													
																	</label>
																</div>
														</td>
														<td>
																<div class="icheck">
																	<label>
																		<input class="ViPrm_<?php echo $mainmenu_list_S['MenuId']; ?>" type="checkbox" name="ViewPrm_<?php echo $page_list_S['ModuleMenuId']; ?>"   <?php if($pagepermission_all['ViewPrm'] == 1 ) echo "checked"; ?> />
													
																	</label>
																</div>
														</td>
													 </tr>
													 
													<?php	 
													 }
													?>
													 </tbody>
													</table>	
													<div class="clearfix"></div>
												<?php											
													}
												?>	
											</div>	
											
										</div>
					</div>
					
					
					
					
					
					
					<div class="form-group row">
						<div class="col-md-2"></div>
						<div class="col-md-10">														
							<button class="btn btn-primary waves-effect waves-light" type="button" onClick="javascript: funSubmt('frmPermission','permissioninfo_actions.php','jvalidate','permissioninfo','permissioninfo_mng.php');" ><span id="spSubmit"><?php echo $btn; ?></span></button>	
										 
							<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmPermission','jvalidate','modulemenu','permissioninfo_mng.php');" >Cancel</button>							
							
						</div>
					</div>
					
				</div>
				
					
				</form>
            </div>
        </div> <!-- end row -->		

<?php include "includes/footer.php";  ?> 
	</div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 

<script type="text/javascript">  
$(function(){
	
   $(".iCheck-helper").click(function () {
	    var chkid = $(this).prev('input').attr('id');				
		if (undefined != chkid)
		{
			if ($("#"+chkid).is(':checked')){
			  $("."+chkid).prop('checked', 'checked');
              $("."+chkid).parent('div').addClass('checked');  
			}
			else{
			  $("."+chkid).prop('checked', false);
              $("."+chkid).parent('div').removeClass('checked');  
			} 
		}	
	});
});

</script>

</body>
</html>