<?php
	include("header.php");
	if (isset($_SESSION['user_type']) && $_SESSION['user_type'] == 'superadmin') {
		
		} else {
		
		// If the user is not admin, redirect or show an error
        header("Location: login.php");
		exit();	
	}	
	
?>

<div class="container-fluid mb-3">
	
	<div class="d-flex justify-content-between align-items-center mb-3">
    <h2 class="h3 m-0">All Users</h2>
    <button id="refreshTable" class="btn btn-primary">
        <span id="refreshIcon" class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
        Refresh Data
    </button>
</div>

    <table id="allusers" class="table table-striped">
		<thead>
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Email</th>
				<th>Type</th>
				<th>IP Address</th>
				<th>Last Login</th>
				<th>Created/Updated</th>
				<th>Status</th>
				<th>Actions</th> <!-- New Actions Column -->
			</tr>
		</thead>
		
	</table>
	
</div>

<!-- Include jQuery and DataTables -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/dataTables.bootstrap5.min.js"></script>

<script>
	$(document).ready(function() {
    var table = $('#allusers').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "fetch_users.php",
            "type": "GET"
        },
        "columns": [
            { "data": "id" },
            { "data": "name" },
            { "data": "email" },
            { "data": "user_type" },
            { "data": "ip_address" },
            { "data": "last_login" },
            { "data": "created_updated" },
            { "data": "status" },
            { "data": "actions", "orderable": false }
        ],
        "order": [[0, "asc"]] // Order by ID
    });

    $('#refreshTable').click(function() {
        var refreshIcon = $('#refreshIcon');

        // Show loading spinner
        refreshIcon.removeClass('d-none');

        // Reload DataTable
        table.ajax.reload(function() {
            // Hide spinner after reload completes
            refreshIcon.addClass('d-none');
        });
    });
});

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