<?php
	error_reporting(0);
	include "db_connection/db_connection.php";
	include "config/config.php";

	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
//print_r($_POST);
	if(isset($_POST['submit'])){
	$dept_id = $_POST['dept_id'];
	$del = mysqli_query($connect,"delete from menu_rights where  dept_id='".$dept_id."'");
	$mnu = mysqli_query($connect,"SELECT * FROM `menu_pages`");

	while($res = mysqli_fetch_array($mnu,MYSQLI_ASSOC)){
		$page_id = $res['menu_page_id'];
		$addr = 0;
		$delr =0;
		$viewr=0;
		$editr=0;
		if(@$_POST['chkadd'.$page_id]=='1'){
			$addr = 1;
		}
		if(@$_POST['chkdel'.$page_id]=='1'){
			$delr = 1;
		}

		if(@$_POST['chkedit'.$page_id]=='1'){
			$editr = 1;
		}
		if(@$_POST['chkview'.$page_id]=='1'){
			$viewr = 1;
		}

		$ins = mysqli_query($connect,"insert into menu_rights(dept_id,menu_id,sub_id,page_id,add_rights,edit_rights,status_rights,view_rights,redirects_to) values('".$dept_id."','','','".$page_id."','".$addr."','".$editr."','".$delr."','".$viewr."','".$_REQUEST['menu_page']."')");
	}
	}
	if(isset($_POST['category_type'])!=''){
	$sel_mny = mysqli_query($connect,"select * from menu_rights where dept_id='".$_POST['category_type']."'");

	$sel_mny1 = mysqli_fetch_array(mysqli_query($connect,"select redirects_to from menu_rights where dept_id='".$_POST['category_type']."' limit 0,1"),MYSQLI_ASSOC);
	$_POST['menu_page']=$sel_mny1['redirects_to'];
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title><?php echo $PROJECT_TITLE ?></title>
<script type="text/javascript" src="js/master_cat_JS.js"></script>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<!--[if IE]> <link href="css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="js/plugins/forms/ui.spinner.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.mousewheel.js"></script>
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript" src="js/plugins/charts/excanvas.min.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.orderBars.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.pie.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.resize.js"></script>

<script type="text/javascript" src="js/plugins/tables/jquery.sortable.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.resizable.js"></script>

<script type="text/javascript" src="js/plugins/forms/autogrowtextarea.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.uniform.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.inputlimiter.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.autotab.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.chosen.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.dualListBox.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.cleditor.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.ibutton.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine.js"></script>

<script type="text/javascript" src="js/plugins/uploader/plupload.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html4.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html5.js"></script>
<script type="text/javascript" src="js/plugins/uploader/jquery.plupload.queue.js"></script>

<script type="text/javascript" src="js/plugins/wizards/jquery.form.wizard.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.validate.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.form.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.collapsible.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.breadcrumbs.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.tipsy.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.progress.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.timeentry.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.colorpicker.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.jgrowl.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fancybox.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fileTree.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.sourcerer.js"></script>

<script type="text/javascript" src="js/plugins/others/jquery.fullcalendar.js"></script>
<script type="text/javascript" src="js/plugins/others/jquery.elfinder.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.easytabs.min.js"></script>
<script type="text/javascript" src="js/files/bootstrap.js"></script>
<script type="text/javascript" src="js/files/functions.js"></script>
<!--<script type="text/javascript" src="js/plugins/tables/jquery.dataTables.js"></script>-->
<link href="https://cdn.datatables.net/2.0.5/css/dataTables.dataTables.css" rel="stylesheet" type="text/css" />
<!--<script type="text/javascript" src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/2.0.5/js/dataTables.js"></script>-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<script>
$.noConflict();

jQuery(document).ready(function(){
  jQuery("#myInput").on("keyup", function() {
    var value = jQuery(this).val().toLowerCase();
    jQuery("#example tbody tr").filter(function() {
      jQuery(this).toggle(jQuery(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>


</head>

<body onload="document.getElementById('listpage').focus();">

<!-- Top line begins -->
<?php include "header_footer/header.php"; ?>
<!-- Top line ends -->

<!-- Sidebar begins -->
<?php include "menus/left_menu.php"; ?>
<!-- Sidebar ends -->
<!-- Content begins -->
<div id="content">
    <?php include "menus/top_menu.php"; ?>
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="home.php">Dashboard</a></li>
                <li><a href="#">Master Rights</a></li>
             </ul>
        </div>
        <?php include "menus/top_left_menu.php"; ?>
    </div>
    <!-- Main content -->
    <div class="wrapper">
	<?php if($admin_flagg=='1'){ ?>
	<div class="fluid">
                <fieldset>
                    <div class="widget">
                        <div class="whead">
						<h6>Menu Privileges</h6><div class="clear"></div></div>
                        <div class="formRow">
						 <form id="validate" name="checklist" class="main" method="post">
							<div class="grid2"><label>Departments:<span class="req">*</span></label></div>
                           <div class="grid4 searchDrop" id="category_type_div">
                                <select data-placeholder="" class="select" name="dept_id" id="dept_id" style="width:200px;" onchange="$('#validate').submit();">
                                    <option value="">Choose a Department...</option>
                                    <?php
			$SLCT_cad = "SELECT  * FROM master_dept WHERE statu_s='active' ORDER BY master_dept_name ASC ";
			//echo $SLCT;
			$QURY_cad = mysqli_query($connect,$SLCT_cad);
			while($FTCH_cad = mysqli_fetch_object($QURY_cad)){
				?>
				<option value="<?php echo $FTCH_cad->master_dept_id ?>" <?php if($_POST['dept_id']==$FTCH_cad->master_dept_id){ echo "selected"; } ?> ><?php echo $FTCH_cad->master_dept_name; ?></option>
				<?php
				}
				?>
                                </select>
                            </div>
                               </form>
							   <form name="rightsform" method="post">
	<input type="hidden" name="dept_id" id="dept_id" value="<?php echo $_POST['dept_id'];?>">
                            <div class="grid2"><label>Redirect Page:<span class="req">*</span></label></div>
							<div class="grid4 searchDrop" id="category_type_div">
                                <select name="menu_page" id="menu_page" class="select">
                                    <option value="">Choose a Page...</option>
                                    <?php
									$SLCT_cad = "SELECT  * FROM menu_pages where 1 order by menu_page_id";
									//echo $SLCT;
			$QURY_cad = mysqli_query($connect,$SLCT_cad);
			while($FTCH_cad = mysqli_fetch_object($QURY_cad)){
									?>
                                    <option value="<?php echo $FTCH_cad->menu_page_id ?>" <?php if($_POST['menu_page']==$FTCH_cad->menu_page_id){ echo "selected"; } ?> ><?php echo $FTCH_cad->page_name; ?></option>
                                    <?php
									}
									?>
                                </select>
                            </div>
                            <div class="clear"></div>
                            <div class="clear"></div>
                        </div>
		<div class="formRow">
      <!-- <table cellpadding="2" cellspacing="1" border="0" class="dTable" id="dynamic">-->
	  <input id="myInput" type="text" placeholder="Search.." style="width:250px; float:right;">

	  <table id="example"   style="width:100%">

			 <thead>
				<tr>
					<!--<th style="text-align:center; width:10%;">S.No</th>-->
					<th style="text-align:left; width:62%; background-color:#EFEFEF;">Page Name</th>
					<th style="width:10%; background-color:#EFEFEF; text-align:center;">Add</th>
					<!--<th style="width:10%;">Delete</th>-->
					<th style="width:10%; background-color:#EFEFEF;text-align:center;">View</th>
					<th style="width:10%; background-color:#EFEFEF;text-align:center;">Update</th>
					<th style="width:10%; background-color:#EFEFEF;text-align:center;">Access</th>
				</tr>
				 </thead>
				 <tbody>
			 <?php
				$SLCT = "SELECT * FROM menu_pages where parent_id='0' ORDER BY menu_page_id ASC";
				//echo $SLCT;
				$QURY = mysqli_query($connect,$SLCT);
				$loop=1;
			if(mysqli_num_rows($QURY)>0){
			while($FTCH = mysqli_fetch_object($QURY)){
				$mnu_id = $FTCH->menu_page_id;
				$addrh1 =0;
				$delrh1 =0;
				$viewrh1 =0;
			$sel_mny1 = mysqli_fetch_array(mysqli_query($connect,"select * from menu_rights where dept_id='".$_POST['category_type']."' and page_id='".$mnu_id."'"),MYSQLI_ASSOC);
			$addrh1 =$sel_mny1['add_rights'];
			$statrh1 =$sel_mny1['status_rights'];
			$viewrh1 =$sel_mny1['view_rights'];
			$editrh1 =$sel_mny1['edit_rights'];
			?>
			  <tr style="border-bottom:1px solid #dfdfdf; border-top:1px solid #dfdfdf;">
				<!--<td style="text-align:center;width:10%;"><?php echo $loop;?></td>-->
				<td  style="width:71%;"><b><?php echo $FTCH->page_name; ?><?php if($FTCH->page_type=='employee'){?>
				(CAD Login)
				<?php } ?></b></td>
				<td align="center" style="width:5%;text-align:center; ">
				<?php if($FTCH->add_option==1){ ?>
				<input type="checkbox"  value="1"  <?php if($addrh1==1){ echo "checked"; }?> class="clsadd<?php echo $loop;?>" name="chkadd<?php echo $mnu_id;?>" id="chkadd<?php echo $loop;?>" onchange="chkdadd('<?php echo $loop;?>');">
				<?php } ?>
				</td>
				<td align="center" style="width:5%; text-align:center;">
				<?php if($FTCH->view_option==1){ ?>
				<input type="checkbox" <?php if($viewrh1==1){ echo "checked"; }?> value="1" class="clsview<?php echo $loop;?>" name="chkview<?php echo $mnu_id;?>"  id="chkview<?php echo $loop;?>" onchange="chkdview('<?php echo $loop;?>');">
				<?php } ?>
				</td>
				<td align="center" style="width:5%; text-align:center;">
				<?php if($FTCH->edit_option==1){ ?>
				<input type="checkbox" <?php if($editrh1==1){ echo "checked"; }?>value="1" class="clsedit<?php echo $loop;?>" name="chkedit<?php echo $mnu_id;?>" id="chkedit<?php echo $loop;?>" onchange="chkdedit('<?php echo $loop;?>');">
				<?php } ?>
				</td>
				<td align="center" style="width:5%; text-align:center;">
				<?php if($FTCH->status_option==1){ ?>
				<input type="checkbox" <?php if($statrh1==1){ echo "checked"; }?>value="1" class="clssta<?php echo $loop;?>" name="chksta<?php echo $mnu_id;?>" id="chksta<?php echo $loop;?>" onchange="chkdsta('<?php echo $loop;?>');">
				<?php } ?>
				</td>
			  </tr>
			 <!-- <tr>
			  <td colspan="5"  >
			  <table cellpadding="0" cellspacing="0" border="0" class="dataTable" width="100%" style="border:none;">-->
			  <?php
			$sub_pge = "SELECT * FROM menu_pages where parent_id='".$FTCH->menu_page_id."'";
			$QURYsub = mysqli_query($connect,$sub_pge);
			$loopsub=1;
		//	if(mysqli_num_rows($QURYsub)>0){
			while($FTCHsub = mysqli_fetch_object($QURYsub)){ 
			$mnu_id1 = $FTCHsub->menu_page_id;
			$addrh =0;
			$delrh =0;
			$viewrh =0;
			$sel_mny = mysqli_fetch_array(mysqli_query($connect,"select * from menu_rights where dept_id='".$_POST['dept_id']."' and page_id='".$mnu_id1."'"),MYSQLI_ASSOC);
			$addrh = $sel_mny['add_rights'];
			$editrh = $sel_mny['edit_rights'];
			$viewrh = $sel_mny['view_rights'];
			$statrh = $sel_mny['status_rights'];
			?>
			<tr style="background-color:#fff;">
				<td style="width:67%;">
				<?php echo $FTCHsub->page_name; ?>
				</td>
				<td style="text-align:center; width:10%;">
				<?php if($FTCHsub->add_option==1){ ?>
				<input type="checkbox" <?php if($addrh==1){ echo "checked"; }?> value="1" class="clsadd<?php echo $loop;?>" name="chkadd<?php echo $mnu_id1;?>" id="chkadd<?php echo $loop;?>" >
				<?php } ?>
				</td>
				<td style="text-align:center; width:10%;">
				<?php if($FTCHsub->view_option==1){ ?>
				<input type="checkbox" <?php if($viewrh==1){ echo "checked"; }?> value="1" class="clsview<?php echo $loop;?>" name="chkview<?php echo $mnu_id1;?>" id="chkview<?php echo $loop;?>" >
				<?php } ?>
				</td>
				<td style="text-align:center; width:10%;">
				<?php if($FTCHsub->edit_option==1){ ?>
				<input type="checkbox" <?php if($editrh==1){ echo "checked"; } ?> value="1" class="clsedit<?php echo $loop;?>" name="chkedit<?php echo $mnu_id1;?>" id="chkedit<?php echo $loop;?>">
				<?php } ?>
				</td>
				<td style="text-align:center; width:5%;">
				<?php if($FTCHsub->status_option==1){ ?>
				<input type="checkbox" <?php if($statrh==1){ echo "checked"; } ?> value="1" class="clssta<?php echo $loop;?>" name="chksta<?php echo $mnu_id1;?>" id="chksta<?php echo $loop;?>">
				<?php } ?>
				</td>
			</tr>
			<?php $loopsub++;} //}?>
				<!--</table> 
				</td>
			  </tr>-->
			  <?php $loop++; } }?>
			  </tbody>
		</table>

                            <div class="clear"></div>
                        </div>
                        <div class="formRow">
						<input type="submit" value="Submit" class="buttonM bBlack formSubmit" name="submit"   style="float: right; margin-left:20px;"/>
						 </form>
						<div class="clear"></div></div>
                        <div class="clear"></div>
                    </div>
                </fieldset>
           
        </div>
  		<?php } else {  echo ACCESS_MSG;} ?>
    </div>
</div>
<!-- Content ends -->
<script>
function chkdadd(lp){
	if($("#chkadd"+lp).is(":checked")){
		$(".clsadd"+lp).prop("checked", true);
	} else {
		$(".clsadd"+lp).prop("checked", false);
	}
}
function chkdedit(lp){	
	if($("#chkedit"+lp).is(":checked")){
		$(".clsedit"+lp).prop("checked", true);
	} else {
		$(".clsedit"+lp).prop("checked", false);
	}
}
function chkdview(lp){
	if($("#chkview"+lp).is(":checked")){
		$(".clsview"+lp).prop("checked", true);
	} else {
		$(".clsview"+lp).prop("checked", false);
	}
}
function chkdsta(lp){
	if($("#chksta"+lp).is(":checked")){
		$(".clssta"+lp).prop("checked", true);
	} else {
		$(".clssta"+lp).prop("checked", false);
	}
}
new DataTable('#example', {
    info: false,
    ordering: false,
    paging: false
});

</script>
</body>
</html>