<?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">Estados de trámite</h2>
<p class="card-text">Lista de estados registradas en el sistema.</p>
<div class="row mb-3">
    <div class="col text-right">
        <a href="<?= BASE_HREF ?>states/create" class="btn btn-primary">
            <span class="fe fe-plus fe-12 mr-2"></span> Crear Estado de Trámite
        </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>Descripción</th>
                            <th>Color</th>
                            <th>% Inicio</th>
                            <th>% Fin</th>
                            <th>Estado</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($states as $index => $state): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= htmlspecialchars($state->description) ?></td>
                            <td>
                                <span class="badge" style="background-color: <?= htmlspecialchars($state->color) ?>; color: #000;">
                                    <?= htmlspecialchars($state->color) ?>
                                </span>
                            </td>
                            <td><?= htmlspecialchars($state->start_percentage) ?>%</td>
                            <td><?= htmlspecialchars($state->end_percentage) ?>%</td>
                            <td>
                                <?php if ($state->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 ?>states/<?= $state->id ?>/edit">Editar</a>
                                        <form action="<?= BASE_ACTION ?>states/<?= $state->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>
