<?php
// File: api_koneksi/reservasi/get_booked_times.php
header('Content-Type: application/json');
include '../config/koneksi.php';

date_default_timezone_set('Asia/Jakarta');

if ($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') {
    $tanggal = $_REQUEST['tanggal'] ?? '';

    if (empty($tanggal)) {
        echo json_encode(["status" => "gagal", "pesan" => "Tanggal tidak boleh kosong!"]);
        exit;
    }

    // Query booked times that are active (not Batal or Dibatalkan) for that date
    $query = "SELECT booking_jam FROM dat_reservasi WHERE booking_tgl = ? AND reservasi_st NOT IN ('Batal', 'Dibatalkan') AND deleted_st = 0";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $tanggal);
    $stmt->execute();
    $result = $stmt->get_result();

    $booked_times = array();
    while ($row = $result->fetch_assoc()) {
        $booked_times[] = $row['booking_jam'];
    }
    $stmt->close();

    echo json_encode([
        "status" => "sukses",
        "data" => $booked_times
    ]);
} else {
    echo json_encode(["status" => "gagal", "pesan" => "Metode request tidak valid"]);
}
$conn->close();
?>
