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

$pembayaran_id = $_POST['pembayaran_id'] ?? '';

if(empty($pembayaran_id)){
    echo json_encode([
        "status"=>"gagal",
        "pesan"=>"ID tidak valid"
    ]);
    exit;
}

$conn->begin_transaction();

try{
    // Update status pembayaran
    $query = "
    UPDATE dat_pembayaran 
    SET validasi_st='Expired' 
    WHERE pembayaran_id=?
    ";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $pembayaran_id);
    $stmt->execute();

    // Update status reservasi
    $query2 = "
    UPDATE dat_reservasi r 
    JOIN dat_pembayaran p ON r.reservasi_id = p.reservasi_id 
    SET r.reservasi_st='Batal' 
    WHERE p.pembayaran_id=?
    ";
    $stmt2 = $conn->prepare($query2);
    $stmt2->bind_param("s", $pembayaran_id);
    $stmt2->execute();

    $conn->commit();

    echo json_encode([
        "status"=>"sukses",
        "pesan"=>"Booking dibatalkan otomatis"
    ]);

}catch(Exception $e){
    $conn->rollback();
    echo json_encode([
        "status"=>"gagal",
        "pesan"=>$e->getMessage()
    ]);
}
$conn->close();
?>