<?php
    include('session.php'); // Ensure session and database connection are included

    header('Content-Type: application/json');

    $response = array(); // Array to store the response message

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $recordId = trim($_POST['recordid']);

        // Validate inputs
        if (empty($recordId)) {
            $response['error'] = 'Rekod tidak sah.';
            echo json_encode($response);
            exit;
        }

        $stmt = $conn->prepare("DELETE FROM $tblpanitia WHERE AutoIndex = ?");
        
        if ($stmt === false) {
            $response['error'] = "RALAT: Penyediaan SQL gagal.";
            echo json_encode($response);
            exit;
        }

        $stmt->bind_param("s", $recordId);

        if ($stmt->execute()) {
            $response['success'] = "Rekod telah dipadam!";
        } else {
            $response['error'] = "RALAT: Gagal padam kini rekod.";
        }

        $stmt->close();
    } else {
        $response['error'] = 'Kaedah permintaan tidak sah.';
    }

    // Close the database connection
    $conn->close();

    // Return the response as JSON
    echo json_encode($response);
?>
