<?= $this->session->flashdata('pesan'); ?>
<div class="card shadow-sm mb-4 border-bottom-primary">
    <div class="card-header bg-white py-3">
        <div class="row">
            <div class="col">
                <h4 class="h5 align-middle m-0 font-weight-bold text-primary">
                    Mantenedor de Usuarios
                </h4>
            </div>
            <div class="col-auto">
                <a href="<?= base_url('user/add') ?>" class="btn btn-sm btn-primary btn-icon-split">
                    <span class="icon">
                        <i class="fa fa-user-plus"></i>
                    </span>
                    <span class="text">
                        Crear nuevo Usuario
                    </span>
                </a>
            </div>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-striped  table-hover nowrap DataTable" id="TableUsuarios">
            <thead>
                <tr>
                    <th>Id Usuario</th>
                    <th>Nombre Usuario (Login Sistema)</th>
                    <th>Nombre Completo</th>
                    <th>Entidad (Empresa)</th>
                    <th>Grupo</th>
                    <th>Estado</th>
                    <th>Creado el</th>
                    <th>Acción</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                if ($users) :
                    foreach ($users as $user) :
                        ?>
                        <tr>
                            <td><?= $user['id_user']; ?></td>
                            <td><?= $user['username']; ?></td>
                            <td><?= $user['name']; ?></td>
                            <td>
                                <?php foreach ($entidades as $j) : ?>
                                    <?php if( $j['id_entidad'] == $user['id_entidad'] ) { echo $j['nombre_entidad'];  } ?>
                                <?php endforeach; ?>
                            </td>                            
                            <td> <?php if($user['role'] == "abogado") {echo "Abogado";} else {echo"Administrador";} ?> </td>
                            <td> <?php if($user['is_active'] == "1") {echo "Habilitado";} else {echo"Bloqueado";} ?> </td>      
                            <td><?= $user['created_at']; ?></td>                   
                            <td>
                                <a href="<?= base_url('user/edit/') . $user['id_user'] ?>" class="btn btn-sm btn-info"><i class="fa fa-fw fa-edit"></i> Editar</a>
                                <a onclick="return confirm('Seguro de eliminar?')" href="<?= base_url('user/delete/') . $user['id_user'] ?>" class="btn btn-sm btn-danger"><i class="fa fa-fw fa-trash"></i> Eliminar</a>
                                <a href="<?= base_url('user/toggle/') . $user['id_user'] ?>" class="btn btn-sm <?= $user['is_active'] ? 'btn-danger' : 'btn-success' ?>" title="<?= $user['is_active'] ? 'Bloquear Usuario' : 'Habilitar Usuario' ?>"><i class="fa fa-fw fa-power-off"></i> <?= $user['is_active'] ? 'Bloquear' : 'Habilitar' ?> </a>
                            </td>
                        </tr>
                    <?php endforeach;
                    else : ?>
                    <tr>
                        <td colspan="8" class="text-center">Please add a new user</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>