query(" SELECT h.*, m.nome AS modalidade_nome, m.cor AS modalidade_cor, m.icon AS modalidade_icon, TIME_FORMAT(h.hora_inicio, '%H:%i') AS hora_inicio_fmt, TIME_FORMAT(h.hora_fim, '%H:%i') AS hora_fim_fmt FROM horario h JOIN modalidades m ON m.m_id = h.id_modalidade WHERE h.deleted_at IS NULL ORDER BY FIELD(h.dia_semana,'segunda','terça','quarta','quinta','sexta','sábado','domingo'), h.hora_inicio ASC ")->fetchAll(PDO::FETCH_ASSOC); } function db_horario_get(PDO $pdo, int $id): ?array { $stmt = $pdo->prepare(" SELECT h.*, TIME_FORMAT(h.hora_inicio, '%H:%i') AS hora_inicio_fmt, TIME_FORMAT(h.hora_fim, '%H:%i') AS hora_fim_fmt, m.nome AS modalidade_nome FROM horario h JOIN modalidades m ON m.m_id = h.id_modalidade WHERE h.id = ? AND h.deleted_at IS NULL LIMIT 1 "); $stmt->execute([$id]); return $stmt->fetch(PDO::FETCH_ASSOC) ?: null; } function db_horario_create(PDO $pdo, array $data): int { $stmt = $pdo->prepare(" INSERT INTO horario (id_modalidade, dia_semana, hora_inicio, hora_fim, local) VALUES (?, ?, ?, ?, ?) "); $stmt->execute([ (int) $data['id_modalidade'], $data['dia_semana'], $data['hora_inicio'], $data['hora_fim'], $data['local'] ?? '', ]); return (int) $pdo->lastInsertId(); } function db_horario_update(PDO $pdo, int $id, array $data): bool { $stmt = $pdo->prepare(" UPDATE horario SET id_modalidade = ?, dia_semana = ?, hora_inicio = ?, hora_fim = ?, local = ? WHERE id = ? "); $stmt->execute([ (int) $data['id_modalidade'], $data['dia_semana'], $data['hora_inicio'], $data['hora_fim'], $data['local'] ?? '', $id, ]); return $stmt->rowCount() > 0; } // Soft delete para a lixeira (config/functions_trash.php). function db_horario_delete(PDO $pdo, int $id): bool { return trash_delete($pdo, 'horario', $id); } // Reschedule only: day + start/end time (used by the calendar drag-and-drop). // Modalidade and local are left untouched. Times must be HH:MM[:SS]; the caller // is responsible for validating the day against the allowed set. function db_horario_move(PDO $pdo, int $id, string $dia_semana, string $hora_inicio, string $hora_fim): bool { $stmt = $pdo->prepare(" UPDATE horario SET dia_semana = ?, hora_inicio = ?, hora_fim = ? WHERE id = ? "); $stmt->execute([$dia_semana, $hora_inicio, $hora_fim, $id]); return $stmt->rowCount() >= 0; } // Modalidades dropdown (id => nome + cor) for horario create/edit forms. function db_modalidades_options(PDO $pdo): array { return $pdo->query(" SELECT m_id, nome, cor FROM modalidades WHERE deleted_at IS NULL ORDER BY nome ")->fetchAll(PDO::FETCH_ASSOC); }