<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Page pagename</title>
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/dataTables.bootstrap4.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/responsive.dataTables.css">
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 
    $tid=$_REQUEST['tid'];
	$action=$_REQUEST['action'];
    $gettype=f(q("SELECT * FROM tbl_utype WHERE id='$tid'"));
	if($action=='save'){
		$pagename=$_POST['pagename'];
        $pageview=$_POST['pageview'];
        $savep=$_POST['savep'];
        $upp=$_POST['upp'];
        $delp=$_POST['delp'];
        $allpage=$_POST['allpage'];
		$blockpage=$_POST['blockpage'];

        if($psave=="1"){

        if($allpage=='1'){

									$delp=mysqli_query($con,"DELETE FROM tbl_access WHERE utype='$tid'");
                                    $ress=mysqli_query($con,"SELECT * FROM tbl_pages");
                                    while($roww=mysqli_fetch_array($ress)){ 

                                        $inscom=mysqli_query($con,"INSERT INTO tbl_access SET pageid='$roww[id]',utype='$tid',pview=1,psave=1,pupdate=1,pdelete=1");




                                    }

                                    echo "<script>alert('All pages are set');</script>";







        }elseif($blockpage=='1'){

				$delp=mysqli_query($con,"DELETE FROM tbl_access WHERE utype='$tid'");
				$ress=mysqli_query($con,"SELECT * FROM tbl_pages");
                                    while($roww=mysqli_fetch_array($ress)){ 

                                        $inscom=mysqli_query($con,"INSERT INTO tbl_access SET pageid='$roww[id]',utype='$tid',pview=0,psave=0,pupdate=0,pdelete=0");




                                    }

                                    echo "<script>alert('All pages are set');</script>";



		
		}else{


				$delp=mysqli_query($con,"DELETE FROM tbl_access WHERE utype='$tid' AND pageid='$pagename'");
                $inscom=mysqli_query($con,"INSERT INTO tbl_access SET pageid='$pagename',utype='$tid',pview='$pageview',psave='$savep',pupdate='$upp',pdelete='$delp'");

                echo "<script>alert('Data saved');</script>";
        }

		

                
                    $res=mysqli_query($con,"INSERT INTO tbl_pages SET pagename='$pagename'");
                }else{

                    echo "<script>alert('You have no permission to save data');</script>";
                }

			
				

			



	}elseif($action=='update'){
		$pagename=$_POST['pagename'];
        $pageview=$_POST['pageview'];
        $savep=$_POST['savep'];
        $upp=$_POST['upp'];
        $delp=$_POST['delp'];
        $allpage=$_POST['allpage'];
		
		$cid=$_REQUEST['cid'];
        if($pupdate=="1"){

                if($allpage=='1'){

                                    $delp=mysqli_query($con,"DELETE FROM tbl_access WHERE utype='$tid'");
                                    $ress=mysqli_query($con,"SELECT * FROM tbl_pages");
                                    while($roww=mysqli_fetch_array($ress)){ 

                                        $inscom=mysqli_query($con,"INSERT INTO tbl_access SET pageid='$roww[id]',utype='$tid',pview=1,psave=1,pupdate=1,pdelete=1");




                                    }

                                    echo "<script>alert('All pages are set');</script>";

        }else{



                $inscom=mysqli_query($con,"UPDATE tbl_access SET pageid='$pagename',utype='$tid',pview='$pageview',psave='$savep',pupdate='$upp',pdelete='$delp' WHERE id='$cid'");

                echo "<script>alert('Data Updated');</script>";
        }





	
        }else{

            echo "<script>alert('You have no permission to update data');</script>";
        }

	}elseif($action=='edit'){
		$cid=$_REQUEST['cid'];
        if($pupdate=="1"){
		$edit=f(q("SELECT * FROM tbl_access WHERE id='$cid'"));
        
        }else{

            echo "<script>alert('You have no permission to update data');</script>";
        }


	}elseif($action=='delete'){

		$cid=$_REQUEST['cid'];

        if($pdelete=="1"){
		$res=mysqli_query($con,"DELETE FROM tbl_access WHERE id='$cid'");
        echo "<script>alert('Data Deleted');</script>";
        }else{

            echo "<script>alert('You have no permission to delete data');</script>";
        }
	}

	


	?>
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-12">
							<div class="title">
								<h4>Set Page Permission <?php echo $gettype['names']?></h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.php">Home</a></li>
									<li class="breadcrumb-item active" aria-current="page">Access Permission</li>
								</ol>
							</nav>
						</div>
						<div class="col-md-6 col-sm-12 text-right">
							<!-- <div class="dropdown">
								<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown">
									January 2018
								</a>
								<div class="dropdown-menu dropdown-menu-right">
									<a class="dropdown-item" href="#">Export List</a>
									<a class="dropdown-item" href="#">Policies</a>
									<a class="dropdown-item" href="#">View Assets</a>
								</div>
							</div> -->
						</div>
					</div>
				</div>
				<!-- Default Basic Forms Start -->
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<small style="color: red;"><?=$msg?></small>
					<form action="<?=$_SERVER['PHP_SELF']?>?action=<? if($action=='edit'){?>update&cid=<?=$cid?><? }else{?>save<?} ?>&tid=<?php echo $tid?>" method="POST">
					
					<div class="clearfix" style="min-height: 30px;"></div>
					
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Select Page</label>
							<div class="col-sm-12 col-md-10">
								<select class="custom-select2 form-control" name="pagename">
                                    <option value="">Select Page</option>
                                    <?php   
                                    $res=mysqli_query($con,"SELECT * FROM tbl_pages");
                                    while($row=mysqli_fetch_array($res)){ ?>
                                    <option value="<?=$row['id']?>" <?php if($edit['pageid']==$row['id']){?>selected<?php } ?>><?php echo $row['dispname']?></option>

                                    <?php } ?>
                                </select>

							</div>
						</div>
                         <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">View Permission</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="pageview">
                                    <option value="0" <?php if($edit['pview']=="0"){?>selected<?php } ?>>No</option>
                                    <option value="1" <?php if($edit['pview']=="1"){?>selected<?php } ?>>Yes</option>
                                    
                                </select>

							</div>
						</div>
                        
                         <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Save Permission</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="savep">
                                    <option value="0" <?php if($edit['psave']=="0"){?>selected<?php } ?>>No</option>
                                    <option value="1" <?php if($edit['psave']=="1"){?>selected<?php } ?>>Yes</option>
                                    
                                </select>

							</div>
						</div>
                         <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Update Permission</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="upp">
                                    <option value="0" <?php if($edit['pupdate']=="0"){?>selected<?php } ?>>No</option>
                                    <option value="1" <?php if($edit['pupdate']=="1"){?>selected<?php } ?>>Yes</option>
                                    
                                </select>

							</div>
						</div>
                         <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Delete Permission</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="delp">
                                    <option value="0" <?php if($edit['pdelete']=="0"){?>selected<?php } ?>>No</option>
                                    <option value="1" <?php if($edit['pdelete']=="1"){?>selected<?php } ?>>Yes</option>
                                    
                                </select>

							</div>
						</div>
                        <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Give Access To All Page</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="allpage">
                                    <option value="0">No</option>
                                    <option value="1">Yes</option>
                                    
                                </select>

							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Block Access To All Page</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="blockpage">
                                    <option value="0">No</option>
                                    <option value="1">Yes</option>
                                    
                                </select>

							</div>
						</div>

						
						
						<div class="clearfix">
						<div class="pull-left">
							<!-- <h4 class="text-blue">Default Basic Forms</h4>
							<p class="mb-30 font-14">All bootstrap element classies</p> -->
						</div>
						<div class="pull-right">
							<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save</button>
							<!-- <a href="#basic-form1" class="btn btn-primary btn-sm scroll-click" rel="content-y"  data-toggle="collapse" role="button"><i class="fa fa-code"></i> Source Code</a> -->
						</div>
					</div>
					</form>
					<div class="collapse collapse-box" id="basic-form1" >
						
					</div>
				</div>
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<div class="clearfix mb-20">
						<div class="pull-left">
							<h5 class="text-blue">Access List</h5>
						</div>
					</div>
					<div class="row">
						<table class="stripe hover multiple-select-row data-table-export nowrap">
							<thead>
								<tr>
									<th class="table-plus datatable-nosort">Name</th>
									<th class="table-plus datatable-nosort">View</th>
									<th class="table-plus datatable-nosort">Save</th>
                                    <th class="table-plus datatable-nosort">Update</th>
                                    <th class="table-plus datatable-nosort">Delete</th>
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?php
									$getc=mysqli_query($con,"SELECT * FROM tbl_access WHERE utype='$tid'");
									while($arr=mysqli_fetch_array($getc)){
                                        $pagen=f(q("SELECT * FROM tbl_pages WHERE id='$arr[pageid]'"));
								?>
								<tr>
									<td class="table-plus"><?php echo $pagen['dispname']?></td>
									<td class="table-plus"><?php if($arr['pview']=='1'){?> Yes <?php }else{?> No <?php } ?></td>
                                    <td class="table-plus"><?php if($arr['psave']=='1'){?> Yes <?php }else{?> No <?php } ?></td>
                                    <td class="table-plus"><?php if($arr['pupdate']=='1'){?> Yes <?php }else{?> No <?php } ?></td>
									<td class="table-plus"><?php if($arr['pdelete']=='1'){?> Yes <?php }else{?> No <?php } ?></td>
									<td>
									<a href="<?php echo $_SERVER['PHP_SELF']?>?action=edit&cid=<?php echo $arr['id']?>&tid=<?php echo $tid?>">EDIT</a><br>
									<a href="<?php echo $_SERVER['PHP_SELF']?>?action=delete&cid=<?php echo $arr['id']?>&tid=<?php echo $tid?>" onclick="return confirm('Are you sure you want to delete this item?');">DELETE</a><br>
									

									</td>
								</tr>
								<? } ?>	
							</tbody>
						</table>
					</div>
				</div>
				

			</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?php include('include/script.php'); ?>

	<script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
	<script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
	<!-- buttons for Export datatable -->
	<script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
	<script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
	<script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
	<script>
		$('document').ready(function(){
			$('.data-table').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
			});
			$('.data-table-export').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
				dom: 'Bfrtip',
				buttons: [
				'copy', 'csv', 'pdf', 'print'
				]
			});
			var table = $('.select-row').DataTable();
			$('.select-row tbody').on('click', 'tr', function () {
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
				}
				else {
					table.$('tr.selected').removeClass('selected');
					$(this).addClass('selected');
				}
			});
			var multipletable = $('.multiple-select-row').DataTable();
			$('.multiple-select-row tbody').on('click', 'tr', function () {
				$(this).toggleClass('selected');
			});
		});
	</script>
</body>
</html>