<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>User Master</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">
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 

	$action=$_REQUEST['action'];
	if($action=='save'){

		if($psave==1){
		$l_read=$_POST['l_read'];
		$l_uid=md5($_POST['l_read']);
		$l_pass=md5($_POST['l_pass']);
		$l_type=$_POST['l_type'];
		$l_name=$_POST['l_name'];
		$user_id=$_POST['user_id'];
		$zone_id=$_POST['zone_id'];
		

		$chk=f(q("select * from tbl_login where l_read='$l_read'"));
		if($chk['l_read']==''){
	$res=mysqli_query($con,"INSERT INTO tbl_login SET l_read='$l_read',l_uid='$l_uid',l_pass='$l_pass',l_type='$l_type',l_act='1',l_name='$l_name',user_id='$user_id',zone_id='$zone_id'");



	//echo "INSERT INTO tbl_login SET l_read='$l_read',l_uid='$l_uid',l_pass='$l_pass',l_type='$l_type',l_act='1'";
	if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Saved Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Saving Data','','error')
             </script>

				<?} }else{ ?>

						<script type="text/javascript">
			            Swal.fire('Username already exists','','error')
			             </script>

			<?	} 

				}else{
					?>
					<script type="text/javascript">
			            Swal.fire('You do not have permission to access this page','','error')
			             </script>
					<?
				}

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

		if($pupdate==1){
		$l_read=$_POST['l_read'];
		$l_uid=md5($_POST['l_read']);
		$l_pass=md5($_POST['l_pass']);
		$l_type=$_POST['l_type'];
		$l_name=$_POST['l_name'];
		$cid=$_REQUEST['cid'];
		$user_id=$_POST['user_id'];
		$zone_id=$_POST['zone_id'];

		if($l_pass==''){

			$res=mysqli_query($con,"UPDATE tbl_login SET l_type='$l_type',l_name='$l_name',user_id='$user_id',zone_id='$zone_id' WHERE l_id='$cid'");
		}else{

$res=mysqli_query($con,"UPDATE tbl_login SET l_type='$l_type',l_pass='$l_pass' WHERE l_id='$cid'");


}



	if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Updated Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Updating Data','','error')
             </script>

				<? }
		}else{

			?>
			<script type="text/javascript">
		            Swal.fire('You do not have permission to access this page','','error')
		             </script>
			<?
		}

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

				$cid=$_REQUEST['cid'];
				$edit=f(q("SELECT * FROM tbl_login WHERE l_id='$cid'"));




	}elseif($action=='delete'){
		if($ltype=='1'){
		$cid=$_REQUEST['cid'];

		$getad=f(q("SELECT * FROM tbl_login WHERE l_id='$cid'"));
		if($getad['l_uid']==md5('admin')){



		}else{

			$res=mysqli_query($con,"DELETE FROM tbl_login WHERE l_id='$cid'");

			if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Deleted Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Deleting Data','','error')
             </script>

				<?
				}
			}
		}else{
			?>
			<script type="text/javascript">
		            Swal.fire('You do not have permission to access this page','','error')
		             </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>User Master</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">User Master</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">
					<form action="<?=$_SERVER['PHP_SELF']?>?action=<? if($action=='edit'){?>update&cid=<?=$cid?><? }else{?>save<?} ?>" 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">User Name</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="l_read" placeholder="Enter Username" value="<?=$edit['l_read']?>">
							</div>
						</div>
						
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Full Name</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" name="l_name" type="text" value="<?=$edit['l_name']?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Type</label>
							<div class="col-sm-12 col-md-10">
								<select name="l_type" class="form-control">
									<option value="0">Choose</option>
									<?php 

										$q=mysqli_query($con,"SELECT * FROM tbl_utype ORDER BY id");
										while($arrq=mysqli_fetch_array($q)){
											?>
											<option value="<?=$arrq['id']?>" <? if($edit['l_type']==$arrq['id']){?> selected <? } ?>><?=$arrq['names']?></option>
											<?
										}
									
									?>
									
								</select>
							</div>
						</div>
						<div class="form-group row" id="areadiv">
							<label class="col-sm-12 col-md-2 col-form-label">Area</label>
							<div class="col-sm-12 col-md-10">
								<select name="user_id" class="form-control">
									<option value="0">Choose</option>
									<?
										$getar=mysqli_query($con,"SELECT * FROM tbl_area ORDER BY arname");
										while($arrr=mysqli_fetch_array($getar)){
									?>
									<option value="<?=$arrr['arid']?>" <? if($edit['user_id']==$arrr['arid']){?> selected <? } ?>><?=$arrr['arname']?></option>

									<? } ?>
								</select>
							</div>
						</div>
						<div class="form-group row" id="areadiv">
							<label class="col-sm-12 col-md-2 col-form-label">Company</label>
							<div class="col-sm-12 col-md-10">
								<select name="zone_id" class="form-control">
									<option value="0">Choose</option>
									<?
										$getar=mysqli_query($con,"SELECT * FROM tbl_company ORDER BY c_name");
										while($arrr=mysqli_fetch_array($getar)){
									?>
									<option value="<?=$arrr['c_id']?>" <? if($edit['zone_id']==$arrr['c_id']){?> selected <? } ?>><?=$arrr['c_name']?></option>

									<? } ?>
								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Password</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="l_pass" value="">
							</div>
						</div>
						<!-- <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Financial Year</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" value="<?=$edit['finyear']?>" type="text" name="finyear">
							</div>
						</div> -->
						<!-- <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Number</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" value="100" type="number">
							</div>
						</div>
						<div class="form-group row">
							<label for="example-datetime-local-input" class="col-sm-12 col-md-2 col-form-label">Date and time</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control datetimepicker" placeholder="Choose Date anf time" type="text">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Date</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control date-picker" placeholder="Select Date" type="text">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Month</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control month-picker" placeholder="Select Month" type="text">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Time</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control time-picker" placeholder="Select time" type="text">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Select</label>
							<div class="col-sm-12 col-md-10">
								<select class="custom-select col-12">
									<option selected="">Choose...</option>
									<option value="1">One</option>
									<option value="2">Two</option>
									<option value="3">Three</option>
								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Color</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" value="#563d7c" type="color">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Input Range</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" value="50" type="range">
							</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">User 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>Username</th>
									<th>Type</th>
									<!-- <th>Address</th>
									<th>Email</th> -->
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?
									$getc=mysqli_query($con,"SELECT * FROM tbl_login WHERE l_read<>'admin' AND l_type<>'2' ORDER BY l_name");
									while($arr=mysqli_fetch_array($getc)){
								?>
								<tr>
									<td class="table-plus"><?=$arr['l_name']?></td>
									<td><?=$arr['l_read']?></td>
									<td><? if($arr['l_type']=='1'){ echo "Administrator"; }elseif($arr['l_type']=='2'){echo "Lineman";}elseif($arr['l_type']=='3'){echo "Customer";}elseif($arr['l_type']=='4'){echo "Accounts";}s?></td>
									
									<td>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=edit&cid=<?=$arr['l_id']?>">EDIT</a><br>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=delete&cid=<?=$arr['l_id']?>" 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>