'use strict';

var ProfitLossCtrl = ['$scope', '$location', 'dateFilter', 'profit_loss', function ($scope, $location, dateFilter, profit_loss) {
    $scope.info = profit_loss;
    $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('/ProfitLoss').search({StartDate: $scope.info.StartDate, FinishDate: $scope.info.FinishDate});
    };
    $('#txtStartDate').focus();
}];

ProfitLossCtrl.resolve = {
    profit_loss: ['$q', '$route', 'ProfitLoss', function ($q, $route, ProfitLoss) {
        var deferred = $q.defer();

        var start_date = $route.current.params.StartDate;
        var finish_date = $route.current.params.FinishDate;

        var successCb = function (result) {
            deferred.resolve(result);
        };

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