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

$pembayaran_id = $_GET['pembayaran_id'] ?? '';
$reservasi_id = $_GET['reservasi_id'] ?? '';

if (!empty($pembayaran_id)) {
    $stmt = $conn->prepare("SELECT p.*, r.booking_tgl, r.booking_jam, r.final_harga, r.foto_syarat_promo, 
                            pk.paket_nm AS nama_paket, u.lengkap_nm AS nama_lengkap, u.whatsapp_no AS no_hp 
                            FROM dat_pembayaran p 
                            JOIN dat_reservasi r ON p.reservasi_id = r.reservasi_id 
                            JOIN mst_paket pk ON r.paket_id = pk.paket_id 
                            JOIN mst_user u ON r.user_id = u.user_id 
                            WHERE p.pembayaran_id = ?");
    $stmt->bind_param("s", $pembayaran_id);
} else {
    $stmt = $conn->prepare("SELECT p.pembayaran_id, p.metode_id, p.pengirim_nm, p.asal_bank, p.transfer_bukti, p.validasi_st,
                            r.reservasi_id, r.booking_tgl, r.booking_jam, r.final_harga, r.foto_syarat_promo, 
                            pk.paket_nm AS nama_paket, u.lengkap_nm AS nama_lengkap, u.whatsapp_no AS no_hp 
                            FROM dat_reservasi r 
                            LEFT JOIN dat_pembayaran p ON r.reservasi_id = p.reservasi_id 
                            JOIN mst_paket pk ON r.paket_id = pk.paket_id 
                            JOIN mst_user u ON r.user_id = u.user_id 
                            WHERE r.reservasi_id = ?");
    $stmt->bind_param("s", $reservasi_id);
}

$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $row['invoice'] = $row['reservasi_id'];
    echo json_encode(["status" => "sukses", "data" => [$row]]);
} else {
    echo json_encode(["status" => "gagal", "pesan" => "Data tidak ditemukan"]);
}
$stmt->close();
?>