<?php
// Include database connection file
require_once 'shikanisha.kts.php';

// Check if the expense ID is provided in the POST request
if (isset($_POST['expense_id'])) {
    // Get the expense ID and sanitize it
    $expense_id = intval($_POST['expense_id']);
    
    // Prepare SQL delete query
    $query = "DELETE FROM expenses WHERE expense_id = ?";
    $stmt = $conn->prepare($query);
    
    if ($stmt) {
        // Bind the expense ID to the query
        $stmt->bind_param("i", $expense_id);
        
        // Execute the query
        if ($stmt->execute()) {
            // Send success response
            echo json_encode(['status' => 'success']);
        } else {
            // Send error response if query fails
            echo json_encode(['status' => 'error', 'message' => 'Could not delete expense.']);
        }
        $stmt->close();
    } else {
        // Send error response if preparation fails
        echo json_encode(['status' => 'error', 'message' => 'Query preparation failed.']);
    }
} else {
    // Send error response if expense_id is not provided
    echo json_encode(['status' => 'error', 'message' => 'Expense ID not provided.']);
}

$conn->close();
?>
