<?php
include 'koneksi.php'; // Pastikan koneksi.php ada di folder aksi/

$id_po = $_GET['id'];

// Menggunakan transaksi untuk keamanan data
mysqli_begin_transaction($conn);

try {
    // 1. Hapus detail PO terlebih dahulu (menggunakan kolom id_po)
    mysqli_query($conn, "DELETE FROM po_detail WHERE id_po = '$id_po'");
    
    // 2. Hapus header PO (menggunakan kolom id_po)
    $result = mysqli_query($conn, "DELETE FROM purchase_order WHERE id_po = '$id_po'");

    if (!$result) {
        throw new Exception(mysqli_error($conn));
    }

    mysqli_commit($conn);
    echo "<script>alert('PO berhasil dihapus!'); window.location='../purchase-order.php';</script>";
} catch (Exception $e) {
    mysqli_rollback($conn);
    echo "<script>alert('Gagal: " . $e->getMessage() . "'); window.location='../purchase-order.php';</script>";
}
?>