(function () {
    global.dashModule
        .controller('gadgetAssignedOrdersCtrl', ['$scope', '$http', '$', 'utilsService', 'dashboardsFiltersService', gadgetAssignedOrdersCtrl])
        .directive('gadgetAssignedOrders', [gadgetAssignedOrdersDirective]);

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

    function gadgetAssignedOrdersCtrl($scope, $http, $, utilsService, dashboardsFiltersService) {
        $scope.getStatusIcon = utilsService.getStatusIcon;
        $scope.getAssignedOrdersInfo = getAssignedOrdersInfo;
        $scope.filterService = dashboardsFiltersService;

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

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

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

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