<?php
require_once 'config.php';
checkLogin();

if (!isset($_GET['id']) || !isset($_GET['pasaran'])) {
    $_SESSION['error_message'] = 'Invalid request!';
    header("Location: add_result.php");
    exit();
}

$id = (int)$_GET['id'];
$pasaran = sanitizeInput($_GET['pasaran']);

try {
    // Ambil data result sebelum dihapus untuk logging
    $stmt = $db->prepare("SELECT market, draw_number, draw_date FROM lottery_results WHERE id = ?");
    $stmt->execute([$id]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$result) {
        $_SESSION['error_message'] = 'Result not found!';
        header("Location: add_result.php?pasaran=" . urlencode($pasaran));
        exit();
    }
    
    // Hapus result
    $deleteStmt = $db->prepare("DELETE FROM lottery_results WHERE id = ?");
    $deleteStmt->execute([$id]);
    
    // Log activity
    logAdminActivity('DELETE_RESULT', "Market: {$result['market']}, Draw: {$result['draw_number']}, Date: {$result['draw_date']}");
    
    $_SESSION['success_message'] = 'Result deleted successfully!';
} catch (PDOException $e) {
    $_SESSION['error_message'] = 'Failed to delete result: ' . $e->getMessage();
}

header("Location: add_result.php?pasaran=" . urlencode($pasaran));
exit();
?>