limitless.directive('plantAvailability', ['Zupit.Core.AppService', 'config', 'Zupit.Core.HttpClient', 'Zupit.Core.NotificationService', function (app, config, $http, $notifier) { return { restrict: 'AE', replace: true, templateUrl: config.baseUrl + 'app/components/plantAvailabilityDirectiveView.html', scope: { plantId: '=', startDate: '=', endDate: '=', saveData: '=', refreshData: '=', onSave: '&' }, controller: function ($scope) { $scope.state = { availabilityItems: [] }; $scope.reload = function () { if ($scope.startDate != undefined && $scope.endDate != undefined && $scope.plantId != undefined) { $http.post('api/trader/availability/range/' + $scope.plantId, { startDate: $scope.startDate, endDate: $scope.endDate }).then(function (ret) { if (ret.succeded) $scope.state.availabilityItems = ret.data; }); } } $scope.isSaving = false; $scope.save = function () { if ($scope.isSaving) return; $http.post('api/companies/plant/availability', { plantId: $scope.plantId, dailyAvailabilities: _.map($scope.state.availabilityItems, function (e) { return { date: e.start, dailyAvailability: e.availableTrucks }; }) }).then(function (ret) { $scope.onSave({ result: ret }); $scope.isSaving = false; }); } $scope.$watch('saveData', function (newVal) { if (newVal) { $scope.save(); $scope.saveData = false; } }); $scope.$watch('refreshData', function (newVal) { if (newVal) { $scope.reload(); $scope.refreshData = false; } }); } } } ]);