<?php 
include '../include/header.php'; 

				
?>
 <!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary">Dashboard</h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Dashboard</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                          
							
							

	
	<div class="card-body">
              <h3 class="box-title">MENU MASTER</h3>
			  <p align="right"><a href="menu_details2.php"><button class="btn btn-primary">View</button></a> <a href="create_menu.php"><button class="btn btn-primary">Add</button></a></p>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
			<?php
			$id=$_REQUEST['id'];
			$sqln = "select *from tbl_menu_mtr where id = '$id'";
						$resultn = $objhr->viewdata($sqln);
						$rowsn = $resultn->fetch_assoc();
						
			?>
			            <form method="post">
              <div class="box-body">
			  							<div class="row">
											<div class="col-md-6">
                <div class="form-group">
                  <label>MENU NAME :</label>
                  <input type="text" class="form-control" id="m_name" name="m_name" value="<?php echo $rowsn['menu_name']; ?>" placeholder="Enter menu name" required >
                </div>
				
				<div class="form-group">
                  <label>MENU PATH :</label>
                 <select class="form-control" name="m_path" id="m_path">
				 <option value="<?php echo $rowsn['file_path']; ?>"><?php echo $rowsn['file_path']; ?></option>
				 				 <option value="#">#</option>
				 <?php
$dir = $_SERVER['DOCUMENT_ROOT'].'/hr/Admin/';

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
	if($file!='.' and $file!='..'){
     ?>
	 <option value="<?php echo $file; ?>"><?php echo $file; ?></option>
	 <?php }
    }
    closedir($dh);
  }
}
?>
				 </select>
                </div>
				
				<div class="form-group">
                  <label>UNDER MENU ID:</label>
                 <select class="form-control" name="under_menu_id" id="under_menu_id">
				 
				 <?php
				 $under_menu=$rowsn['under_menu_id'];
				 $sqlun = "select *from tbl_menu_mtr where id = '$under_menu'";
						$resultun = $objhr->viewdata($sqlun);
						$rowsun = $resultun->fetch_assoc();
				 ?>
				 <option value="<?php echo $rowsn['under_menu_id']; ?>"><?php echo $rowsun['menu_name']; ?></option>
				  <option value="0">#</option>
				 				 <?php
				 				 
				$sqlud = "select *from tbl_menu_mtr where under_menu_id='0'";
				$resultud = $objhr->viewdata($sqlud);
				if($resultud->num_rows > 0){
				while(	$rowsud = $resultud->fetch_assoc()){
				?>				
           <option value="<?php echo $rowsud['id']; ?>"><?php echo $rowsud['menu_name']; ?></option>	
 		<?php	}					
				}
				 ?>
				 				
				
				 </select>
                </div>
				
				
				<div class="form-group">
                  <label>MENU ORDER:</label>
                 <input type="text" class="form-control" name="m_order" id="m_order" value="<?php echo $rowsn['menu_order']; ?>">
				 
				
	                </div>
					
					
<div class="form-group">
                  <label>PERMISSION TO:</label>
<br>


        <?php
								 
				$sqlu = "select *from tbl_user_type order by id asc";
				$resultu = $objhr->viewdata($sqlu);
				if($resultu->num_rows > 0){
				while(	$rowsu = $resultu->fetch_assoc()){
				$user_type=$rowsu['id'];
				
				if($rowsu['id']!='1'){?>				
        
		    <input type="checkbox" class='name' name="name[]"  <?php $sqlchk = "select * from tbl_menu_permission where menu_id = '$id' and user_type_id = '$user_type' and status = 1 ";
				$resultchk = $objhr->viewdata($sqlchk); 
					if($resultchk->num_rows > 0){?> checked="checked" <?php }?>  value="<?php echo $rowsu['id']; ?>"><?php echo $rowsu['user_type']; ?>
					
					
					
					
 		<?php	
		}			}		
				}
				 ?>
     
				 
				
	                </div>			
				<div class="form-group">
                <input type="submit" name="update_menu" value="Update" class="btn  btn-primary">
                </div>
				
				<?php
			
				if(isset($_POST['update_menu'])){
				$menu = $_POST['m_name'];
		$pathh = $_POST['m_path'];
		$under_menu=$_POST["under_menu_id"];
		$menu_order=$_POST["m_order"];

		
		 $sql_perc = "UPDATE `tbl_menu_permission` SET `status` = '2' WHERE `menu_id` = '$id' and  `user_type_id` != '1';";
		 $resultcc = $objhr->updation($sql_perc);
	
		
	 $counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */
  for($x=0; $x<=$counter; $x++){

    if(!empty($_POST["name"][$x])){
   $user_typee = mysql_real_escape_string($_POST["name"][$x]);
  
     $sqlckp = "select *from tbl_menu_permission where menu_id = '$id' and user_type_id = '$user_typee'";
   
	$resultckp=$objhr->viewdata($sqlckp);
	
	if($resultckp->num_rows > 0){
	
		$sql_per = "UPDATE `tbl_menu_permission` SET `status` = '1' WHERE `menu_id` = '$id' and  `user_type_id` = '$user_typee';";
		 $result = $objhr->updation($sql_per);
		
		 
	 }
	else{
	$sqlckpp = "select *from tbl_menu_permission where menu_id = '$id'";
   
	$resultckpp=$objhr->viewdata($sqlckpp);
	
	if($resultckpp->num_rows > 0){
	
	$str_admin = "INSERT INTO `tbl_menu_permission` (`menu_id`, `user_type_id`, `status`) VALUES ('$id', '$user_typee', '1');";
   
	$resultt=$objhr->viewdata($str_admin);
	
	
	 }
	
	
	}
	
	
	
	} 
	
	
	
	
    } /* END OF CHECKING THE CHECKBOX IF SELECTED */
	
	
		 $sql_menu = "UPDATE `tbl_menu_mtr` SET `menu_name` = '$menu', `file_path` = '$pathh', `under_menu_id` = '$under_menu', `menu_order` = '$menu_order' WHERE `id` = '$id'";
		 $resultmenu = $objhr->updation($sql_menu);
		 if($resultmenu){
		 ?>
		 <script>alert('success');
		 location="";
		 </script>
		 
		 <?php
		 
		 }
	
	}
	
	
	
	
	
	
	
   
 
	
						
						
					
			
				?>
				
				
				
				
				</div><!-- /.box-body -->
											</div>
										</div>
										
            </form>
          </div><!-- /.box -->

<?php

  
?>



		  <?php
		 
		  		 // if(isset($_POST['submit'])){
		//$menu = $_POST['m_name'];
		//$pathh = $_POST['m_path'];
		//$under_menu=$_POST["under_menu_id"];
		//$menu_order=$_POST["m_order"];
	
	
	//$str = "INSERT INTO `tbl_menu_mtr` (`menu_name`, `file_path`, `under_menu_id`, `menu_order`) VALUES ('$menu', '$pathh', '$under_menu', '$menu_order');";
	//$result=$objhr->viewdata($str);
	//if($result!="")
	//{
	//echo "success";
	//}} 
		  ?>


<?php
if(isset($_POST['submit'])){

$menu = $_POST['m_name'];
		$pathh = $_POST['m_path'];
		$under_menu=$_POST["under_menu_id"];
		$menu_order=$_POST["m_order"];

	
	
	
	$strr = "INSERT INTO `tbl_menu_mtr` (`menu_name`, `file_path`, `under_menu_id`, `menu_order`) VALUES ('$menu', '$pathh', '$under_menu', '$menu_order');";
	
	$last_id=$objhr->insertion_last_id_view($strr);
	
		if($last_id!="")
	{
	 	
  $counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */

  for($x=0; $x<=$counter; $x++){

    if(!empty($_POST["name"][$x])){
      $user_type = mysql_real_escape_string($_POST["name"][$x]);
   $str = "INSERT INTO `tbl_menu_permission` (`menu_id`, `user_type_id`, `status`) VALUES ('$last_id', '$user_type', '1');";
   
	$result=$objhr->viewdata($str);
	} 
	
    } /* END OF CHECKING THE CHECKBOX IF SELECTED */
$str_admin = "INSERT INTO `tbl_menu_permission` (`menu_id`, `user_type_id`, `status`) VALUES ('$last_id', '1', '1');";
   
	$resultt=$objhr->viewdata($str_admin);
	if($result!="")
	{		echo "success";
	}
	
		}
	
	
  } /* END OF FOR LOOP */

?>
		  									</div>
				
							 </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->

 <!----------------------------------------------------------------- Main content End -------------------------------------------------------------->
  <!-- /.content-wrapper -->
  <?php include '../include/footer.php'; ?>
