'use strict';

var ServiceChargeController = ['$scope', '$location', '$routeParams', 'numberFilter', 'asDateFilter', '$modal', 'voucher', function ($scope, $location, $routeParams, numberFilter, asDate, $modal, voucher) {
    $scope.voucher = voucher;

    $scope.TotalPoints = function (){
        return _($scope.voucher.ServiceCharges).map(function (item) {
            return item.DaysWorked * Number(item.Points);
        }).reduce(function  (sum, item) {
            return sum + item;
        });
    };
    $scope.PointValue = function  () {
        return Math.round($scope.voucher.ServiceCharge * 100 / $scope.TotalPoints()) / 100;
    };
    $scope.less = function (employee) {
        if (employee.Points >= 1) {
            employee.Points -= 1;
        }
    };
    $scope.more = function (employee) {
        employee.Points += 1;
    };

    $scope.$watch('voucher.Date', function (newDate, oldDate) {
        var nDate = asDate(newDate),
            oDate = asDate(oldDate);


        if (nDate !== oDate && !angular.isUndefined(nDate)) {
            if (angular.isUndefined($routeParams.id)) {
                return voucher.$get({type: 'Service Charge', Date: nDate}).$promise;
            }
        }
    }, true);

    $scope.employeeAmount = function (employee) {
        return Number(employee.Points) * employee.DaysWorked * $scope.PointValue();
    }
    $scope.preventAlteration = function (voucher) {
        if (angular.isUndefined($scope.perms)) {
            return false;
        } else if (angular.isUndefined(voucher.VoucherID)) {
            return !$scope.perms['Service Charge'];
        } else if (voucher.Posted && !$scope.perms['Edit Posted Vouchers']) {
            return true;
        } else if (voucher.User.UserID !== $scope.auth.UserID && !$scope.perms["Edit Other User's Vouchers"]) {
            return true;
        } else {
            return false;
        }
    };

    $scope.save = function () {
        $scope.voucher.Date = asDate($scope.voucher.Date);
        $scope.voucher.$save({type: 'Service Charge'}, function (u) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/ServiceCharge/' + u.VoucherID);
        }, function (data) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.delete = function () {
        $scope.voucher.$delete(function () {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $location.path('/ServiceCharge').replace();
        }, function (data) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.post = function () {
        $scope.voucher.$post(function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.confirm = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Delete Voucher";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.delete();
        });
    };
}];


ServiceChargeController.resolve = {
    voucher: ['$route', 'Voucher', function ($route, Voucher) {
        var id = $route.current.params.id;

        if (angular.isUndefined(id)) {
            return Voucher.get({type: 'Service Charge'}).$promise;
        } else {
            return Voucher.get({id: id}).$promise;
        }
    }]
};
