(function () {
    'use strict';

    angular.module('fehlerberichtApp')
        .controller('ChangelogCtrl', function ($scope, $http) {
            var webservice = 'db.php?';

            $scope.Typen = ["neu", "akt", "entf"];

            $scope.Status = {
                "Historie": [],
                "Liste": []
            };

            $scope.Filter = {
                "Konto_Nr": "",
                "Konto_Bezeichnung": "",
                "Konto_Art": "",
                "Datum": "",
                "Status": "",
                "Quelle": ""
            };

            $scope.Domaene = {
                "Konto_Nr": [],
                "Konto_Bezeichnung": [],
                "Konto_Art": ["GuV", "Bilanz"],
                "Datum": [],
                "Status": $scope.Typen,
                "Quelle": []
            };

            $scope.selectedDate = {"datum": "heute"};
            $scope.selectedField = "Konto_1";

            $http.get(webservice + "a=changelog&kunde=").success(function (data) {
                $scope.Status = data;
                $scope.selectedDate = $scope.Status.Historie[$scope.Status.Historie.length - 1];
                $scope.Status.Liste = _.sortBy($scope.Status.Liste, function (entry) {
                    return entry.Konto_Nr;
                });
                $scope.Domaene.Datum = _.chain($scope.Status.Liste).pluck('Datum').unique().sort().value();
                $scope.Domaene.Quelle = _.chain($scope.Status.Liste).pluck('Quelle').unique().sort().value();
            });

            $scope.badge = function (typ) {
                if (typ === "neu") {
                    return "label-success";
                }
                if (typ === "akt") {
                    return "label-warning";
                }
                return "label-danger";
            };

            /**
             * @return {string}
             */
            $scope.Kontoart = function (art) {
                return (art === "1") ? "Bilanz" : "GuV";
            };

            /**
             * @return {string}
             */
            $scope.Seit = function (datum) {
                if (moment(datum, "DD.MM.YYYY").isSame(moment(), 'day')) {
                    return "heute";
                }
                return moment(datum, "DD.MM.YYYY").from(moment().startOf('day'));
            };
        });
})();