<?php if (!empty($_SESSION['error'])): ?>
    <div class="alert alert-danger"><?= $_SESSION['error']; ?></div>
    <?php unset($_SESSION['error']); ?>
<?php endif; ?>

<?php if (!empty($_SESSION['success'])): ?>
    <div class="alert alert-success"><?= $_SESSION['success']; ?></div>
    <?php unset($_SESSION['success']); ?>
<?php endif; ?>



<h2 class="mb-2 page-title">Usuarios de la Comunidad</h2>
<p class="card-text">Lista de usuarios de la comunidad educativa.</p>
<div class="row mb-3">
    <div class="col text-right">
        <a href="<?= BASE_HREF ?>users/create-comunity-user" class="btn btn-primary">
            <span class="fe fe-plus fe-12 mr-2"></span> Registrar Usuario
        </a>
    </div>
</div>

<div class="row my-4">
    <div class="col-md-12">
        <div class="card shadow">
            <div class="card-body">
                <table class="table datatables" id="dataTable-1">
                    <thead class="thead-dark">
                        <tr>
                            <th>N°</th>
                            <th>Nombre</th>
                            <th>DNI</th>
                            <th>R. Social</th>
                            <th>RUC</th>
                            <th>Teléfono</th>
                            <th>Correo</th>
                            <th>Dirección</th>
                            <th>Estado</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($users as $index => $user): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= htmlspecialchars($user->name) . ' ' .  htmlspecialchars($user->paternal_surname) . ' ' . htmlspecialchars($user->maternal_surname) ?></td>
                                <td><?= htmlspecialchars($user->dni) ?></td>
                                <td><?= htmlspecialchars($user->company_name) ?></td>
                                <td><?= htmlspecialchars($user->ruc) ?></td>
                                <td><?= htmlspecialchars($user->phone) ?></td>
                                <td><?= htmlspecialchars($user->email) ?></td>
                                <td><?= htmlspecialchars($user->address) ?></td>
                                <td>
                                    <?php if ($user->active == 1): ?>
                                        <span class="badge badge-success">Activo</span>
                                    <?php else: ?>
                                        <span class="badge badge-danger">Inactivo</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div class="dropdown">
                                        <button class="btn btn-sm dropdown-toggle more-horizontal" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <span class="text-muted sr-only">Acciones</span>
                                        </button>
                                        <div class="dropdown-menu dropdown-menu-right">
                                            <a class="dropdown-item" href="<?= BASE_HREF ?>comunity-users/<?= $user->id ?>/edit">Editar</a>
                                            <form action="<?= BASE_ACTION ?>comunity-users/<?= $user->id ?>/delete" method="POST" style="display: inline;" onsubmit="return confirm('¿Estás seguro?');">
                                                <input type="hidden" name="_method" value="DELETE">
                                                <button type="submit" class="dropdown-item text-danger">Eliminar</button>
                                            </form>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#dataTable-1').DataTable({
            autoWidth: true,
            "lengthMenu": [
                [10, 15, 20, -1],
                [10, 15, 20, "All"]
            ]
        });
    });
</script>