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

<html>
<head>
	<title>Simcard Records</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 href="assets/css/dashboard.css" 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">

	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
 	 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    
<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 SIMCARDS </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 SIMCARD </button>
				
				<a href="clients.php"> <button type="submit" class="btn btn-info"><b> CLIENTS </b></button> </a>
				<a href="devices.php"> <button type="submit" class="btn btn-info"><b> DEVICES </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="modal fade" id="approve_modal" aria-hidden="true" style="margin-left: 30%;">
					
					<form method="POST" action="" >
							
                			<div class="modal-header alert-success">
                    			<h3>Add SimCards </h3>
                			</div>
							<div class="modal-body ">    
    							<table class="datatable-1 table table-striped">
      								<tr>
									<td>Simcard ID </td>
									  <td><input type="text" name="simID" placeholder="Simcard ID" required/></td></tr>

      								<tr>
									<td>Issd NO </td>
									  <td><input type="text" name="iccdNo" placeholder="Issd No"/></td></tr>
									
     								<tr>
									<td>Provider </td>
									 <td><input type="text" name="provider" placeholder="Provider"/></td>
									</tr>

      			<!--					<tr>-->
									<!--<td>Status </td><td><select name="status">-->
									<!--	<option value="Active"> Active </option>                             -->
									<!--	<option value="Inactive"> Inactive </option> -->
									<!--	<option value="Suspended"> Suspended </option> -->
        	<!--							</select></td>-->
									<!--</tr>-->
      								<tr><td><input type="submit" name="submit"class="btn btn-primary" value="Add SimCard"/></td></tr>

      								<?php
									if(isset($_POST['submit'])){

										$simID=$_POST['simID'];
										$iccdNo=$_POST['iccdNo'];
										$provider=$_POST['provider'];
								// 		$status=$_POST['status'];

  										if($simID!='' && $iccdNo!=''  && $provider!='' ){
											$query="INSERT INTO simcard_tbl (simID,iccdNo,provider,status,store) values ('$simID','$iccdNo','$provider','Active','available')";
											$result=mysqli_query($link,$query);
										if($result){
												$msg = "Data added successfully";
											
 											}else{
      											$error = "Data is available, please try again!";
  											}
 											}else{
      											$error = "please fill all required detail!";
  											}
 											}
									?>
                    			</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>
							<?php if($error){?>
								<div class="alert alert-danger"><strong>Data</strong>:<?php  echo htmlentities($error); ?> </div>
							<?php } ?>
							<?php if($msg){?>
								<div class="alert alert-success"><strong>Data</strong>:<?php  echo htmlentities($msg); ?> </div>
							<?php } ?>
						</form>
	    			</div>
	    			</div>
	    			</div>



					<!-- modal update -->
					<div class="form-group">
        			<div class="modal fade" id="approve_modals" aria-hidden="true" style="margin-left: 30%;">
					<center>
						<form method="POST" action="">
                			<div class="modal-header alert-success">
                    			<h3>Update Fleet </h3>
                			</div>
							<div class="modal-body ">    
    							<table class="datatable-1 table  table-striped">
      								<tr><td>SimID </td><td><input type="varchar" id="s" name="simIDD" /></td></tr>
      								<tr><td>ISSD </td><td><input type="varchar" id="i" name="iccdNoo"/></td></tr>
      								<tr><td>provider </td><td><input type="varchar" id="p" name="providerr" /></td></tr>
      								<tr><td>Status </td><td><select id="st" name="statuss">
										<option value="Active"> Active </option>                             
										<option value="Inactive"> Inactive </option> 
										<option value="Suspended"> Suspended </option> 
										<option value="checkup"> checkup </option> 
        								</select></td>
                                    </td></tr>
      								<tr><td> store</td><td><input type="varchar" id="sto"name="storee"  readonly/></td></tr>
      								
      								<tr><td><input type="submit" class="btn btn-info" name="submit" value="Update"/></td></tr>

      								<?php
									if(isset($_POST['submit'])){
                                        
										$simID=$_POST['simIDD'];
										$iccdNo=$_POST['iccdNoo'];
										$provider=$_POST['providerr'];
										$status=$_POST['statuss'];
										
  										if($simID!='' && $iccdNo!='' && $provider!='' && $status!=''){
											$query1="UPDATE simcard_tbl SET simID='$simID', iccdNo='$iccdNo', provider='$provider', status='$status' where simID='$simID'";
											$result1=mysqli_query($link,$query1);
											if($result1){
												$_SESSION['status'] = "<div class='alert-success'><p> Data has been added successfully. </p> </div>";
         										}
 											}else{
      											echo "please fill all required detail";
  											}
										}
									?>
                    			</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>
				
	<table id="example" cellpadding="0" cellspacing="0" border="0" class="datatable-1 table table-bordered table-striped	 display" width="100%">
		<thead>
			<tr>
				<th scope="col-sm-2">Simcard ID</th>
				<th scope="col">Iccd NO </th>
				<th scope="col">Provider </th>
				<th scope="col">Status </th>
				<th scope="col">Store </th>
				<th scope="col">Action </th>
			</tr>
		</thead> 
			<tbody>
			  	<?php		  
					$result = mysqli_query($link,"select * from simcard_tbl" );
						while($row = mysqli_fetch_array($result)) {
				?>

				<tr>
				 <td><?php echo $row['simID']; ?></td>
				 <td><?php echo $row['iccdNo']; ?></td>
				 <td><?php echo $row['provider']; ?></td>
				 <td><?php echo $row['status']; ?></td>
				 <td><?php echo $row['store']; ?></td>
				 <td>
				 	<button class="btn btn-primary approves" data-toggle="modal" type="button"><i class="glyphicon glyphicon-edit"></i></button>
					 <a href="simcard_delete.php?simID=<?php echo $row['simID']; ?>" <button class="btn btn-danger" type="button"><i class="glyphicon glyphicon-trash"></i></button></a>
					<?php  }  ?>
					</td>
				</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(){
		$('.approves').on('click',function(){
		$('#approve_modals').modal('show');
		$tr = $(this).closest('tr');
		var data = $tr.children("td").map(function(){ 
			return $(this).text();
		}).get();
			console.log(data);
			$('#s').val(data[0]);
			$('#i').val(data[1]);
			$('#p').val(data[2]);
			$('#st').val(data[3]);
			$('#sto').val(data[4]);
		});
	});
	</script> 

</body>
</html>