var app = angular.module("budgetListApp", []);
app.controller("BudgetListController", function ($scope, $http) {
    $scope.loading = true;
    $scope.error = null;
    $scope.budgets = [];

    $scope.monthNames = ["Januari", "Februari", "Mars", "April", "Maj", "Juni",
        "Juli", "Augusti", "September", "Oktober", "November", "December"];

    $scope.getBudgetUrl = function (b) {
        if (b.name) {
            return "/budget/" + encodeURIComponent(b.name);
        }
        return "/budget/" + b.year + "/" + b.month;
    };

    $http.get("/api/budget/list")
        .then(res => {
            $scope.budgets = res.data;
        })
        .catch(err => {
            console.error("Kunde inte hämta budgetlista:", err);
            $scope.error = "Fel vid laddning av budgetar.";
        })
        .finally(() => {
            $scope.loading = false;
        });
});
