(function () {
    global.dashModule
        .controller('gadgetOrderCentralCtrl', ['$scope', '$http', '$', 'dashboardsFiltersService', 'utilsService', gadgetOrderCentralCtrl])
        .directive('gadgetOrderCentral', [gadgetOrderCentraltDirective]);

    function gadgetOrderCentraltDirective() {
        return {
            restrict: 'E',
            templateUrl: 'dashboards/html/orderCentralTemplate'
        };
    }

    function gadgetOrderCentralCtrl($scope, $http, $, dashboardsFiltersService, utilsService) {
        $scope.getStatusIcon = utilsService.getStatusIcon;
        $scope.getOrderCentralInfo = getOrderCentralInfo;
        $scope.filterService = dashboardsFiltersService;

        dashboardsFiltersService.registerOnReloadData($scope.gadget.module, getOrderCentralInfo);

        function getOrderCentralInfo(filters, sortBy) {
            const params = $.param({
                filters : JSON.stringify(filters) || null,
                sortBy : JSON.stringify(sortBy) || null
            });

            return $http({
                method: 'POST',
                url: 'dashboards/api/getOrderCentralInfo',
                data: params
            }).then(setGadgetInfo, utilsService.onHttpError);
        }

        function setGadgetInfo(response) {
            $scope.orders = (response.data && response.data.length) ? response.data : [];
        }
    }
})();
