<?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">Días Feriados</h2>
<p class="card-text">Lista de días festivos a considerar en el sistema.</p>
<div class="row mb-3">
    <div class="col text-right">
        <a href="<?= BASE_HREF ?>holidays/create" class="btn btn-primary">
            <span class="fe fe-plus fe-12 mr-2"></span> Nuevo Feriado
        </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>Fecha de Feriado</th>
                            <th>Estado</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($holidays as $index => $holiday): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= htmlspecialchars($holiday->description) ?></td>
                            <td>
                                <?php
                                    $fecha = sprintf('%02d/%02d', $holiday->holiday_day, $holiday->holiday_month);
                                    if (!empty($holiday->specific_year)) {
                                        $fecha .= '/' . $holiday->specific_year;
                                    } else {
                                        $fecha .= ' (anual)';
                                    }
                                    echo htmlspecialchars($fecha);
                                ?>
                            </td>
                            <td>
                                <?php if ($holiday->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 ?>holidays/<?= $holiday->id ?>/edit">Editar</a>
                                        <form action="<?= BASE_ACTION ?>holidays/<?= $holiday->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>
