<?php if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }

// Handle actions (create, delete, fetch)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action == 'create') {
        $description = $_POST['description'];
        $formtypes = implode(',', $_POST['formtypes'] ?? []); // Convert selected checkboxes to a comma-separated list
        $status = $_POST['status'] ?? 0; // Default status is 0

        $stmt = $con->prepare("INSERT INTO dd_nature (description, formtypes, status) VALUES (?, ?, ?)");
        $stmt->bind_param('ssi', $description, $formtypes, $status);
        $stmt->execute();
        $stmt->close();

        echo json_encode(['status' => 'success']);
        exit();
    }

    if ($action == 'delete') {
        $id = $_POST['id'];

        $stmt = $con->prepare("DELETE FROM dd_nature WHERE id = ?");
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $stmt->close();

        echo json_encode(['status' => 'success']);
        exit();
    }

    if ($action == 'fetch') {
        $result = mysqli_query($con, "SELECT * FROM dd_nature");
        $data = mysqli_fetch_all($result, MYSQLI_ASSOC);

        echo json_encode($data);
        exit();
    }
}
?>