'use strict';

var PurchaseEntriesCtrl = ['$scope', '$location', 'dateFilter', 'purchase_entries', function ($scope, $location, dateFilter, purchase_entries) {
    $scope.info = purchase_entries;

    $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('/PurchaseEntries').search({StartDate: $scope.info.StartDate, FinishDate: $scope.info.FinishDate});
    };
    $('#txtStartDate').focus();
}];

PurchaseEntriesCtrl.resolve = {
    purchase_entries: ['$route', 'PurchaseEntries', function ($route, PurchaseEntries) {
        var start_date = $route.current.params.StartDate,
            finish_date = $route.current.params.FinishDate;

        if (typeof start_date === 'undefined' || typeof finish_date === 'undefined') {
            return PurchaseEntries.get({}).$promise;
        } else {
            return PurchaseEntries.get({StartDate: start_date, FinishDate: finish_date}).$promise;
        }
    }]
};
