<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeModulemenu($db,'');
include_once "includes/pagepermission.php";
include "common/dpselect-functions.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);

$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; ?> ModuleMenu</h4>				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
            <div class="col-sm-12">
				<form id="jvalidate" name="frmModuleMenu" 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-md-2 form-control-label">Menu Name</label>
                        <div class="col-md-8">
							<?php 
								echo getSelectBox_menulist($db,'dpMenuId','jsrequired','disabled',$edit_id);
							?>
                        </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">Module List</h3>
								</div>
							</div>	
							<div class="box-body">
								<div class="form-group row">
								<?php 
									$module_list = $db->get_rsltset("select ModuleId,ModuleName from a_modules where 1=1 and IsActive = 1");
									foreach($module_list as $module_list_S)
									{
										$chek='';
										if (in_array($module_list_S['ModuleId'], $module_listarray)) {
											$chek = 'checked';
										}
								?>
										<div class="col-sm-3">
											<label>
											<input type="checkbox" name="modulecheck_list[]" value="<?php echo $module_list_S['ModuleId']; ?>" <?php echo $chek; ?> />
											<?php echo $module_list_S['ModuleName']; ?>
											</label>
										</div>
								<?php	
									}
								?>	
								</div>
							</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: return validate_all();" ><span id="spSubmit"><?php echo $btn; ?></span></button>	
										 
							<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmModuleMenu','jvalidate','modulemenu','modulemenu_mng.php');" >Cancel</button>							
							
						</div>
					</div>
					
				</div>
				
					
				</form>
            </div>
        </div> <!-- end row -->		
		
		
		<div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">                         
                    <table id="tblresult_modulesorting" class="table table-striped table-bordered">
                        <thead>
                            <tr>
								<th>Menu Name</th>
								<th>Module Name</th>
								<th>Sorting Order</th>
                            </tr>
                        </thead> 
						
						<tbody>
							<?php
							$modulesorting_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='".$edit_id."' order by t1.SortingOrder asc ");
							foreach($modulesorting_list as $modulesorting_list_S)
							{							
							?>
							<tr>
								<td><?php echo $modulesorting_list_S['MenuName']; ?></td>
								<td><?php echo $modulesorting_list_S['Description']; ?></td>
								<td>
									<input type="text" value="<?php echo $modulesorting_list_S['SortingOrder']; ?>" class="form-control"  onkeypress="return isNumber(event)" onchange="changesortingorder('<?php echo $modulesorting_list_S['ModuleMenuId']; ?>',this.value)" style="width:100px;"  />
								</td>
							</tr>
							<?php			
							}
							?>	
						</tbody>					
						
                    </table>
                </div>
            </div>
        </div>	
		

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

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


<script type="text/javascript"> 
	$(function () {
		$('#tblresult_modulesorting').DataTable({
          "paging": true,
          "lengthChange": true,
          "searching": true,
          "ordering": false,
          "info": true,
          "autoWidth": false
        });
	});
	
  function validate_all(){ 
    var chkmodulesel=0;
	if(chkmodulesel == 0){	
		var chkModule=document.getElementsByName('modulecheck_list[]');
		for (var i = 0; i < chkModule.length; i++) {
				if(chkModule[i].checked == true){
					if(chkmodulesel ==0){
						chkmodulesel =1;
					}
				}	
		}
		if(chkmodulesel == 0){				
			swal("Failure!", "Please selecte one or more module permission to this menu.", "warning");
			return false;	
		}
		else{
			funSubmt('frmModuleMenu','modulemenu_actions.php','jvalidate','modulemenu','modulemenu_mng.php');
			return true;
		}		
	}     
  }	

  function changesortingorder(modulemenuId,txtval){	  
	  if(txtval !=""){		  
		  $.ajax({
			url        : 'others_actions.php',
			method     : 'POST',
			dataType   : 'json',
			data	   : 'pagename=modulemenusorting&modulemenuId='+modulemenuId+'&sort_value='+txtval+'',			
			success	   : function(response){ 						  		
			}
		});
		  
	  }  
  }
  
</script>

</body>
</html>