<main class="container py-4 py-md-5">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <div class="d-flex flex-column flex-md-row justify-content-between gap-3 align-items-md-center mb-4">
                <div>
                    <h1 class="h3 fw-bold text-cfc-dark mb-1">Feriados / Datas comemorativas</h1>
                    <p class="text-muted mb-0">Cadastre datas fixas recorrentes todo ano ou datas móveis editáveis, como Carnaval.</p>
                </div>
            </div>

            <div class="alert alert-info rounded-4 shadow-soft border-0">
                <strong>Recorrência:</strong> marque “recorrente todo ano” para datas fixas, como 07/09, 12/10 e 25/12. 
                Para datas móveis, como Carnaval, deixe sem recorrência e edite/cadastre o ano correto.
            </div>

            <div class="card border-0 shadow-soft rounded-4 mb-4">
                <div class="card-body p-4">
                    <h2 class="h5 fw-bold mb-3"><i class="bi bi-calendar-event"></i> <span id="holiday-form-title">Novo feriado ou data comemorativa</span></h2>
                    <form method="post" action="<?= e(url('admin/holidays/save')) ?>" class="row g-3" id="holiday-form">
                        <?= CSRF::input() ?>
                        <input type="hidden" name="holiday_id" id="holiday_id" value="">
                        <div class="col-md-3">
                            <label class="form-label">Data</label>
                            <input type="date" name="holiday_date" id="holiday_date" class="form-control" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Nome do feriado/data comemorativa</label>
                            <input name="name" id="holiday_name" class="form-control" required placeholder="Ex: Independência do Brasil">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Tipo</label>
                            <select name="type" id="holiday_type" class="form-select" data-holiday-type>
                                <option value="national">Nacional</option>
                                <option value="state">Estadual</option>
                                <option value="city">Municipal</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">UF</label>
                            <input name="uf" id="holiday_uf" maxlength="2" class="form-control text-uppercase" placeholder="BA">
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">Cidade</label>
                            <input name="city" id="holiday_city" class="form-control" placeholder="Somente municipal">
                        </div>
                        <div class="col-md-3 d-flex align-items-end">
                            <label class="form-check mb-2">
                                <input class="form-check-input" type="checkbox" name="repeat_yearly" id="holiday_repeat_yearly" value="1">
                                <span class="form-check-label">Recorrente todo ano</span>
                            </label>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <label class="form-check mb-2">
                                <input class="form-check-input" type="checkbox" name="is_active" id="holiday_is_active" value="1" checked>
                                <span class="form-check-label">Ativo</span>
                            </label>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button class="btn btn-success rounded-pill px-4" id="holiday-submit"><i class="bi bi-check2-circle"></i> Salvar</button>
                        </div>
                        <div class="col-md-12">
                            <button type="button" class="btn btn-outline-secondary rounded-pill d-none" id="holiday-cancel-edit" onclick="resetHolidayForm()"><i class="bi bi-x-circle"></i> Cancelar edição</button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card border-0 shadow-soft rounded-4">
                <div class="card-body p-4">
                    <h2 class="h5 fw-bold mb-3">Feriados e datas comemorativas cadastradas</h2>
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead><tr><th>Data base</th><th>Nome</th><th>Tipo</th><th>Local</th><th>Recorrência</th><th>Status</th><th class="text-end">Ações</th></tr></thead>
                            <tbody>
                            <?php foreach ($holidays as $h): ?>
                                <tr>
                                    <td><strong><?= e(date('d/m/Y', strtotime($h['holiday_date']))) ?></strong></td>
                                    <td><?= e($h['name']) ?></td>
                                    <td><span class="badge text-bg-primary"><?= e(Holiday::typeLabel($h['type'])) ?></span></td>
                                    <td><?= e(trim(($h['city'] ?? '') . (!empty($h['uf']) ? ' / ' . $h['uf'] : '')) ?: 'Brasil') ?></td>
                                    <td>
                                        <?php if (!empty($h['repeat_yearly'])): ?>
                                            <span class="badge text-bg-info">Todo ano</span>
                                            <small class="d-block text-muted">Repete em <?= e(date('d/m', strtotime($h['holiday_date']))) ?></small>
                                        <?php else: ?>
                                            <span class="badge text-bg-secondary">Somente <?= e(date('Y', strtotime($h['holiday_date']))) ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= !empty($h['is_active']) ? '<span class="badge text-bg-success">Ativo</span>' : '<span class="badge text-bg-secondary">Inativo</span>' ?></td>
                                    <td class="text-end">
                                        <div class="d-inline-flex flex-wrap gap-2 justify-content-end">
                                            <button type="button" class="btn btn-sm btn-outline-primary rounded-pill"
                                                onclick='editHoliday(<?= json_encode($h, JSON_HEX_APOS|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE) ?>)'>
                                                <i class="bi bi-pencil"></i> Editar
                                            </button>
                                            <form method="post" action="<?= e(url('admin/holidays/delete')) ?>" onsubmit="return confirm('Apagar este feriado/data comemorativa?');">
                                                <?= CSRF::input() ?>
                                                <input type="hidden" name="holiday_id" value="<?= (int)$h['id'] ?>">
                                                <button class="btn btn-sm btn-outline-danger rounded-pill"><i class="bi bi-trash"></i> Apagar</button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (!$holidays): ?><tr><td colspan="7" class="text-center text-muted py-4">Nenhum feriado cadastrado.</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<script>
function editHoliday(h) {
    document.getElementById('holiday-form-title').textContent = 'Editar feriado ou data comemorativa';
    document.getElementById('holiday_id').value = h.id || '';
    document.getElementById('holiday_date').value = h.holiday_date || '';
    document.getElementById('holiday_name').value = h.name || '';
    document.getElementById('holiday_type').value = h.type || 'national';
    document.getElementById('holiday_uf').value = h.uf || '';
    document.getElementById('holiday_city').value = h.city || '';
    document.getElementById('holiday_repeat_yearly').checked = Number(h.repeat_yearly || 0) === 1;
    document.getElementById('holiday_is_active').checked = Number(h.is_active || 0) === 1;
    document.getElementById('holiday-cancel-edit').classList.remove('d-none');
    document.getElementById('holiday-submit').innerHTML = '<i class="bi bi-check2-circle"></i> Atualizar';
    document.getElementById('holiday-form').scrollIntoView({behavior:'smooth', block:'center'});
}
function resetHolidayForm() {
    document.getElementById('holiday-form-title').textContent = 'Novo feriado ou data comemorativa';
    document.getElementById('holiday_id').value = '';
    document.getElementById('holiday_date').value = '';
    document.getElementById('holiday_name').value = '';
    document.getElementById('holiday_type').value = 'national';
    document.getElementById('holiday_uf').value = '';
    document.getElementById('holiday_city').value = '';
    document.getElementById('holiday_repeat_yearly').checked = false;
    document.getElementById('holiday_is_active').checked = true;
    document.getElementById('holiday-cancel-edit').classList.add('d-none');
    document.getElementById('holiday-submit').innerHTML = '<i class="bi bi-check2-circle"></i> Salvar';
}
</script>
