<?php
		include_once 'include/connection.php'; 
		session_start();
		$uid=$_SESSION['uID'];
			 if(empty($uid)){
				header("location:index.php");
			 }
?>

<html>
<head>
	<title>Fleet clients</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
 
	<link type="text/css" href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

	<link type="text/css" href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
	<link type="text/css" href="images/icons/css/font-awesome.css" rel="stylesheet">
	<link type="text/css" href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600' rel='stylesheet'>
	
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.6.2/css/buttons.bootstrap.min.css">
    
	<script language="javascript" type="text/javascript">
	var popUpWin=0;
	function popUpWindow(URLStr, left, top, width, height)
	{
	if(popUpWin)
	{
	if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr,'popUpWin', 'toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+600+',height='+600+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	}

	</script>
</head>
<body>

	<div class="container-fluid"> 
		<div class="row">
        	<div class="col-sm-12">
            	<center>
                	<div class="alert alert-success" role="alert">
                    	<h2><span class="badge badge-secondary">CREATE Clients </span></h2>
                        	DASHBOARD
                	</div>
            	</center>   
				<a href="adminDashboard.php"> <button class="btn btn-primary " type="button">HOME</button></a>
				<button class="btn btn-info approve" data-toggle="modal" type="button" >ADD CLIENTS </button>
				
				 <a href="devices.php"> <button type="submit" class="btn btn-info"><b> DEVICES </b></button></a>
                <a href="simcards.php"> <button type="submit" class="btn btn-info"><b> SIMCARDS </b></button></a>
                <a href="fleet_view.php"> <button type="submit" class="btn btn-info"><b> FLEETS </b></button></a>
                <a href="hosting_view.php"> <button type="submit" class="btn btn-info"><b> HOSTINGS </b></button></a>

			  	<div class="form-group">
        			<div class="modal fade" id="approve_modal" aria-hidden="true" style="margin-left: 30%">
					<center>
						<form method="POST" action="">
                			<div class="modal-header alert-success">
                    			<h3>Add Clients </h3>
                			</div>
							<div class="modal-body ">    
							<table class="datatable-1 table table-bordered table-striped" >
      								<tr><td>ClientName</td><td><input type="text" name="clientName" placeholder="Client Name"/></td></tr>
      								<tr><td>CompanyName </td><td><input type="text" name="companyName" placeholder="company Name"/></td></tr>
      								<tr><td>phoneNo </td><td><input type="text" name="phoneNo" placeholder="Phone Number"/></td></tr>
      								<tr><td>email </td><td><input type="text" name="email" placeholder="Email"/></td></tr>
      								<tr><td>Region </td><td><input type="text" name="region" placeholder="region"/></td></tr>
      								<tr><td><input type="submit" name="submit" value="Add client" class="btn btn-primary" /></td></tr>

      								<?php
									if(isset($_POST['submit'])){
								
										$clientName=$_POST['clientName'];
										$companyName=$_POST['companyName'];
										$phoneNo=$_POST['phoneNo'];
										$email=$_POST['email'];
										$region=$_POST['region'];
								
  										if($clientName!='' && $companyName!='' && $phoneNo!='' && $region!=''  ){
											$query="INSERT INTO client_tbl(clientName,companyName,phoneNo,email,region) values ('$clientName','$companyName','$phoneNo','$email','$region')";
											$result=mysqli_query($link,$query);
										}
									}
									?>
                    			</table> 
                			</div>
							<div style="clear:both;"></div>
							<div class="modal-footer">
								<button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
							</div>
						</form>
	    			</center>
				</div>

				<!--edit client-->

				<div class="form-group">
        			<div class="modal fade" id="edit_modal" aria-hidden="true" style="margin-left: 30%">
					<center>
						<form method="POST" action="">
                			<div class="modal-header alert-success">
                    			<h3>edit Clients </h3>
                			</div>
							<div class="modal-body ">    
							<table class="datatable-1 table table-bordered table-striped" >
							        <tr><td>ClientID</td><td><input type="text" id="a" name="clientID1" readonly /></td></tr>
      								<tr><td>ClientName</td><td><input type="text" id="b" name="clientName1" placeholder="Client Name"/></td></tr>
      								<tr><td>CompanyName </td><td><input type="text" id="c" name="companyName1" placeholder="company Name"/></td></tr>
      								<tr><td>phoneNo </td><td><input type="text" id="d" name="phoneNo1" placeholder="Phone Number"/></td></tr>
      								<tr><td>email </td><td><input type="text" id="e" name="email1" placeholder="Email"/></td></tr>
      								<tr><td>Region </td><td><input type="text" id="f" name="region1" placeholder="region"/></td></tr>
      								<tr><td><input type="submit" name="submit"  value="Edit client" class="btn btn-primary" /></td></tr>

      								<?php
									if(isset($_POST['submit'])){
										$clientID1=$_POST['clientID1'];
										$clientName1=$_POST['clientName1'];
										$companyName1=$_POST['companyName1'];
										$phoneNo1=$_POST['phoneNo1'];
										$email1=$_POST['email1'];
								        $region1=$_POST['region1'];
  										if($clientName1!='' && $companyName1!='' && $phoneNo1!='' ){
											$query45="UPDATE client_tbl SET clientName='$clientName1',companyName='$companyName1',phoneNo='$phoneNo1',email='$email1',region='$region1' WHERE clientID='$clientID1'";
											$result45=mysqli_query($link,$query45);
										}
									}
									?>
                    			</table> 
                			</div>
							<div style="clear:both;"></div>
							<div class="modal-footer">
								<button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
							</div>
						</form>
	    			</center>
				</div>

			<!--edit client ends-->	




									
	<table id="example" cellpadding="0" cellspacing="0" border="0" class="datatable-1 table table-bordered table-striped	 display" width="100%">
		<thead>
			<tr>
			<th>ClientID </th>
                <th>ClientName </th>
                <th>CompanyName </th>
                <th>phoneNo </th>
                <th>email </th>
                <th>region </th>
				<th>Action</th>
			</tr>
		</thead> 
			<tbody>
			  	<?php		  
					$result = mysqli_query($link,"select * from client_tbl" );
						while($row = mysqli_fetch_array($result)) {
				?>

				<tr>
				<td><?php echo $row['clientID']; ?></td>
				 <td><?php echo $row['clientName']; ?></td>
				 <td><?php echo $row['companyName']; ?></td>
				 <td><?php echo $row['phoneNo']; ?></td>
				 <td><?php echo $row['email']; ?></td>
				 <td><?php echo $row['region']; ?></td>
				 <td><button class="btn btn-info edit" data-toggle="modal" type="button"  ><span class="glyphicon glyphicon-edit"></span> </button>
					<button target="_blank" rel="noopener noreferrer" class="btn btn-danger" > <a href="client_delete.php?clientID=<?php echo $row['clientID'];?>" style="color:white;"><span class="glyphicon glyphicon-trash"></span></a> 
				<?php  }  ?>

				</tr>
			</tbody>
	</table>
  			</div>
  		</div>
	</div>
</div>
		
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.colVis.min.js"></script>
	
	 <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
	
	

	<script>
    $(document).ready(function() {
        var table = $('#example').DataTable( {
            lengthChange: false,
            buttons: [ 'excel']
        } );
     
        table.buttons().container()
            .appendTo( '#example_wrapper .col-sm-6:eq(0)' );
    } );
	</script>

  
<script>
	$(document).ready(function(){
		$('.approve').on('click',function(){
		$('#approve_modal').modal('show');
		$tr = $(this).closest('tr');
		var data = $tr.children("td").map(function(){ 
			return $(this).text();
		}).get();
			console.log(data);
			$('#H').val(data[0]);
		});
	});
	</script> 


<script>
	$(document).ready(function(){
		$('#example').on('click','.edit',function(){
		$('#edit_modal').modal('show');
		$tr = $(this).closest('tr');
		var data = $tr.children("td").map(function(){ 
			return $(this).text();
		}).get();
			console.log(data);
			$('#a').val(data[0]);
			$('#b').val(data[1]);
			$('#c').val(data[2]);
			$('#d').val(data[3]);
			$('#e').val(data[4]);
		    $('#f').val(data[5]);
			
		});
	});
	</script> 

</body>
</html>