<?php
date_default_timezone_set('Asia/Jakarta'); // Misalnya, mengatur zona waktu ke WIB
$page_title = 'All User';
require_once('includes/load.php');
// Kode lainnya seperti memuat pengguna, memeriksa izin, dll.
?>

<?php
// Checkin What level user has permission to view this page
page_require_level_array([1, 4]);
//pull out all user form database
$all_users = find_all_user();

// Calculate user statistics for the dashboard
$total_users = count($all_users);
$active_users = 0;
$inactive_users = 0;
foreach ($all_users as $user) {
    if ($user['status'] == '1') {
        $active_users++;
    } else {
        $inactive_users++;
    }
}
?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-4">
        <div class="panel panel-info">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-user"></i> Total Users
            </div>
            <div class="panel-body text-center">
                <h2><?php echo $total_users; ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="panel panel-success">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-ok-sign"></i> Active Users
            </div>
            <div class="panel-body text-center">
                <h2><?php echo $active_users; ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="panel panel-danger">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-remove-sign"></i> Inactive Users
            </div>
            <div class="panel-body text-center">
                <h2><?php echo $inactive_users; ?></h2>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Users</span>
                </strong>
                <a href="add_user.php" class="btn btn-info pull-right">Add New User</a>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th class="text-center" style="width: 50px;">#</th>
                            <th>Name</th>
                            <th>Username</th>
                            <th class="text-center" style="width: 15%;">User Role</th>
                            <th class="text-center" style="width: 10%;">Status</th>
                            <th style="width: 20%;">Last Login</th>
                            <th class="text-center" style="width: 100px;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($all_users as $a_user): ?>
                            <tr>
                                <td class="text-center"><?php echo count_id(); ?></td>
                                <td><?php echo remove_junk(ucwords($a_user['name'])) ?></td>
                                <td><?php echo remove_junk(ucwords($a_user['username'])) ?></td>
                                <td class="text-center"><?php echo remove_junk(ucwords($a_user['group_name'])) ?></td>
                                <td class="text-center">
                                    <?php if ($a_user['status'] === '1'): ?>
                                        <span class="label label-success"><?php echo "Active"; ?></span>
                                    <?php else: ?>
                                        <span class="label label-danger"><?php echo "Deactive"; ?></span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo read_date($a_user['last_login']) ?></td>
                                <td class="text-center">
                                    <div class="btn-group">
                                        <a href="edit_user.php?id=<?php echo (int)$a_user['id']; ?>" class="btn btn-xs btn-warning" data-toggle="tooltip" title="Edit">
                                            <i class="glyphicon glyphicon-pencil"></i>
                                        </a>
                                        <a href="delete_user.php?id=<?php echo (int)$a_user['id']; ?>" class="btn btn-xs btn-danger" data-toggle="tooltip" title="Remove">
                                            <i class="glyphicon glyphicon-remove"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>