'use strict';

var PurchasesCtrl = ['$scope', '$routeParams', '$location', 'dateFilter', 'purchases', function ($scope, $routeParams, $location, dateFilter, purchases) {
    $scope.info = purchases;
    $scope.show = function () {
        if (angular.isDate($scope.info.StartDate)) {
            $scope.info.StartDate = dateFilter($scope.info.StartDate, 'dd-MMM-yyyy');
        }
        if (angular.isDate($scope.info.FinishDate)) {
            $scope.info.FinishDate = dateFilter($scope.info.FinishDate, 'dd-MMM-yyyy');
        }
        $location.path('/Purchases').search({s:$scope.info.StartDate, f:$scope.info.FinishDate});
    };
}];
PurchasesCtrl.resolve = {
    purchases:['$route', 'Purchases', function ($route, Purchases) {
        var start_date = $route.current.params.s,
            finish_date = $route.current.params.f;

        if (typeof start_date === 'undefined' || typeof finish_date === 'undefined') {
            return Purchases.get({}).$promise;
        } else {
            return Purchases.get({s:start_date, f:finish_date}).$promise;
        }
    }]
};
