<?php
require 'shikanisha.kts.php';

$response = []; // Initialize response array

if ($conn->connect_error) {
    $response['success'] = false;
    $response['message'] = "Connection failed: " . $conn->connect_error;
    echo json_encode($response);
    exit;
}

$id = $_POST['id'];

// Prepare and execute the DELETE statement
$sql = "DELETE FROM expense_type WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id); // Bind the parameter to prevent SQL injection

if ($stmt->execute()) {
    // Check if any rows were affected (meaning a delete occurred)
    if ($stmt->affected_rows > 0) {
        $response['success'] = true;
        $response['message'] = "Expense deleted successfully.";
    } else {
        $response['success'] = false;
        $response['message'] = "No expense found with the provided ID.";
    }
} else {
    $response['success'] = false;
    $response['message'] = "Error executing query: " . $stmt->error;
}

// Close the statement and connection
$stmt->close();
$conn->close();

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