<?php
$first = DateTime::createFromFormat('Y-m-d', $month . '-01') ?: new DateTime('first day of this month');
$prev = (clone $first)->modify('-1 month')->format('Y-m');
$next = (clone $first)->modify('+1 month')->format('Y-m');
?>
<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <h1 class="h3 fw-bold text-cfc-dark mb-4">Controle de Disponibilidade</h1>
            <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
                <h2 class="h5 fw-bold">Criar horários em lote</h2>
                <form method="post" action="<?= e(url('admin/availability/save')) ?>" class="row g-3">
                    <?= CSRF::input() ?>
                    <div class="col-md-4"><label class="form-label">Profissional</label><select class="form-select" name="professional_id" required><?php foreach ($professionals as $p): ?><option value="<?= (int)$p['id'] ?>"><?= e($p['name']) ?> - <?= e($p['type']) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-4"><label class="form-label">Data inicial</label><input type="date" name="start_date" class="form-control" value="<?= e(date('Y-m-d')) ?>" required></div>
                    <div class="col-md-4"><label class="form-label">Data final</label><input type="date" name="end_date" class="form-control" value="<?= e(date('Y-m-d', strtotime('+30 days'))) ?>" required></div>
                    <div class="col-md-8"><label class="form-label">Dias da semana</label><div class="d-flex flex-wrap gap-2">
                        <?php $names=[1=>'Seg',2=>'Ter',3=>'Qua',4=>'Quint',5=>'Sex',6=>'Sab',7=>'Dom']; foreach ($names as $n=>$label): ?>
                            <label class="btn btn-outline-primary rounded-pill"><input class="form-check-input me-1" type="checkbox" name="weekdays[]" value="<?= $n ?>" <?= $n<=5?'checked':'' ?>> <?= e($label) ?></label>
                        <?php endforeach; ?>
                    </div></div>
                    <div class="col-md-2"><label class="form-label">Duração (100 min hora/aula)</label><input type="number" name="duration" class="form-control" value="100" min="50" step="10"></div>
                    <div class="col-md-2"><label class="form-label">Horários</label><textarea name="hours" class="form-control" rows="3">06:00
07:40
09:20
11:00
13:00
14:40
16:20
</textarea></div>
                    <div class="col-12"><button class="btn btn-primary rounded-pill px-4">Liberar Horários</button></div>
                </form>
            </div></div>
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h2 class="h5 fw-bold mb-0">Horários de <?= e($first->format('m/Y')) ?></h2>
                <div class="btn-group"><a class="btn btn-outline-primary" href="<?= e(url('admin/availability', ['month'=>$prev])) ?>">Anterior</a><a class="btn btn-outline-primary" href="<?= e(url('admin/availability', ['month'=>$next])) ?>">Próximo</a></div>
            </div>
            <div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4">
                <div class="table-responsive"><table class="table align-middle"><thead><tr><th>Data</th><th>Horário</th><th>Profissional</th><th>Status</th><th></th></tr></thead><tbody>
                    <?php foreach ($slots as $slot): ?>
                        <?php $busy = !empty($slot['appointment_id']); ?>
                        <tr><td><?= e(date('d/m/Y', strtotime($slot['start_at']))) ?></td><td><?= e(date('H:i', strtotime($slot['start_at']))) ?> - <?= e(date('H:i', strtotime($slot['end_at']))) ?></td><td><?= e($slot['professional_name']) ?></td><td><span class="badge <?= $busy?'text-bg-secondary':'text-bg-success' ?>"><?= $busy?'ocupado/bloqueado':'livre' ?></span></td><td class="text-end"><div class="d-flex flex-wrap gap-2 justify-content-end"><?php if ($busy): ?><form method="post" action="<?= e(url('admin/availability/unlock')) ?>" onsubmit="return confirm('Desbloquear está aula? Depois ela poderá ser removida.');"><?= CSRF::input() ?><input type="hidden" name="slot_id" value="<?= (int)$slot['id'] ?>"><input type="hidden" name="month" value="<?= e($month) ?>"><button class="btn btn-sm btn-warning rounded-pill"><i class="bi bi-unlock"></i> Desbloquear</button></form><?php else: ?><form method="post" action="<?= e(url('admin/availability/delete')) ?>" onsubmit="return confirm('Remover este horário livre?');"><?= CSRF::input() ?><input type="hidden" name="slot_id" value="<?= (int)$slot['id'] ?>"><input type="hidden" name="month" value="<?= e($month) ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Remover</button></form><?php endif; ?></div></td></tr>
                    <?php endforeach; ?>
                </tbody></table></div>
            </div></div>
        </div>
    </div>
</main>
