<?php
error_reporting(0);
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">create user</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
           <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row justify-content-center">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="form-validation">
                                    <form class="form-valide" method="post">
									  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Menu Name <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control" id="m_name" name="m_name" placeholder="Enter menu name" required >
                                            </div>
                                        </div>
										
                                        <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Menu Path <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                              <select class="form-control" name="m_path" id="m_path">
				 <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>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Under Menu Id <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                                 <select class="form-control" name="under_menu_id" id="under_menu_id">
				  <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>
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">Menu Order <span class="text-danger">*</span></label>
                                            <div class="col-lg-6">
                                               <input type="text" class="form-control" name="m_order" id="m_order">
                                            </div>
                                        </div>
										
										
										  <div class="form-group row">
                                            <label class="col-lg-4 col-form-label" for="val-username">PERMISSION TO :<span class="text-danger"></span></label>
                                            <div class="col-lg-6">
                                              <?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()){
				if($rowsu['id']!='1'){?>				
           
		    <input type="checkbox" class='name' name="name[]"  value="<?php echo $rowsu['id']; ?>"><?php echo $rowsu['user_type']; ?>
 		<?php	
		}			}		
				}
				 ?>
                                            </div>
                                        </div>
										
										
                                        
                                        <div class="form-group row">
                                            <div class="col-lg-8 ml-auto">
                                                <button type="submit" class="btn btn-primary" name="save">Submit</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
			
<?php
if(isset($_POST['save'])){
$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($resultt!="")
	{	?>
	<script>
	alert('Success');
	location='';
	</script>
		
		
<?php	}
	
		}
	
	}
  

?>
<?php
include('../include/footer.php');
?>
<script>
/*function chk(){
var u_name = $('#u_name').val();
$.ajax({
		type: "POST",
		url: "chk_user_name.php",
		data: "user="+u_name,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var res = data[0];
				
				if(res==1){
				alert('User Name Already Exist.');
				$('#u_name').val('');
				$('#u_name').focus();
				}
				
				
				
			}
			});
   

}*/
</script>