<?php
session_start();
require 'koneksi.php';

if (!isset($_SESSION['admin_id'])) {
    header("Location: index");
    exit();
}

if (!isset($_GET['id'])) {
    $_SESSION['error'] = "ID kategori tidak valid";
    header("Location: historybelanja");
    exit();
}

$id = (int)$_GET['id'];
try {
    $pdo->beginTransaction();

    // Hapus detail dulu
    $pdo->prepare("DELETE FROM historybelanja_detail WHERE historybelanja_id = ?")->execute([$id]);

    // Hapus header
    $pdo->prepare("DELETE FROM historybelanja WHERE id = ?")->execute([$id]);

    $pdo->commit();
    $_SESSION['success'] = "Riwayat belanja berhasil dihapus.";
} catch (Exception $e) {
    $pdo->rollBack();
    $_SESSION['error'] = "Gagal menghapus data: " . $e->getMessage();
}

header("Location: historybelanja");
exit();
