<?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">Modalidades</h2>
<p class="card-text">Lista de modalidades registradas en el sistema.</p>
<div class="row mb-3">
    <div class="col text-right">
        <a href="<?= BASE_HREF ?>modalities/create" class="btn btn-primary">
            <span class="fe fe-plus fe-12 mr-2"></span> Crear Modalidad
        </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>Abreviación</th>
                            <th>Descripción</th>
                            <th>Estado</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($modalities as $index => $modality): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= htmlspecialchars($modality->abbreviation) ?></td>
                            <td><?= htmlspecialchars($modality->description) ?></td>
                            <td>
                                <?php if ($modality->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 ?>modalities/<?= $modality->id ?>/edit">Editar</a>
                                        <form action="<?= BASE_ACTION ?>modalities/<?= $modality->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>
