<?php
// admin/delete-shipment.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$id = intval($_POST['delete_id'] ?? 0);
if ($id > 0) {
    try {
        // Delete tracking history first
        $stmt = $pdo->prepare("DELETE FROM shipment_tracking WHERE shipment_id = ?");
        $stmt->execute([$id]);
        
        // Delete shipment
        $stmt = $pdo->prepare("DELETE FROM shipments WHERE id = ?");
        $stmt->execute([$id]);
        
        echo json_encode(['success' => true]);
    } catch (Exception $e) {
        echo json_encode(['success' => false, 'message' => $e->getMessage()]);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid ID']);
}
?>