'use strict';

var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'reconcile', 'Reconcile', 'Account', function ($scope, $routeParams, $location, asDate, reconcile, Reconcile, Account) {
    $scope.info = reconcile;
    $scope.show = function () {
        var id = $scope.info.Account.LedgerID,
            startDate = asDate($scope.info.StartDate),
            finishDate = asDate($scope.info.FinishDate);
        if (id === $routeParams.id && startDate === $routeParams.StartDate && finishDate === $routeParams.FinishDate) {
            Reconcile.get({id: id, StartDate: startDate, FinishDate: finishDate}, function (data) {
                $scope.info = data;
            });
        } else {
            $location.path('/Reconcile/' + id).search('StartDate', startDate).search('FinishDate', finishDate);
        }
    };
    $scope.save = function () {
        var i, l,
            len = $scope.info.Body.length;
        for (i = 0, l = len; i < l; i++) {
            $scope.info.Body[i].ReconcileDate = asDate($scope.info.Body[i].ReconcileDate);
        }
        $scope.info.$save({StartDate: $routeParams.StartDate, FinishDate: $routeParams.FinishDate}, function (u, putResponseHeaders) {
            $scope.toasts.push({Type: 'Success', Message: ''});
            $scope.info = u;
        }, function (data, status) {
            $scope.toasts.push({Type: 'Danger', Message: data.data});
        });
    };

    $scope.downloadTable = function () {
        var table = $('#gvGrid'),
            html = table.clone().wrap('<div></div>').parent().html();
        html = html.replace(/á/g, '&aacute;');
        window.open('data:application/vnd.ms-excel;charset=UTF-8,' + encodeURIComponent(html));
    };

    if ($routeParams.id) {
        $('#gvGrid').focus();
    } else {
        $('#txtLedger').focus();
    }

    $scope.selected = -1;
    $scope.setSelected = function (index) {
        $scope.selected = index;
    };

    $scope.shortcuts = {
        'up': function (e) {
            if ($scope.selected > 0) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected -= 1;
                });
                $("#" + $scope.selected).scrollintoview({duration: 'fast'});
                e.preventDefault();
            }
        },
        'down': function (e) {
            if ($scope.selected < $scope.info.Body.length - 1) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected += 1;
                });
                $("#" + $scope.selected).scrollintoview({duration: 'fast'});
                e.preventDefault();
            }
        },
        'enter': function (e) {
            var path = $scope.info.Body[$scope.selected].Url.replace(/^(?:\/\/|[^\/]+)*/, "");
            $scope.$apply(function () {
                $location.path(path).search('StartDate', null).search('FinishDate', null);
            });
        }
    };

    $scope.accounts = function ($viewValue) {
        return Account.autocomplete({term: $viewValue, count: 20, r: true}).$promise;
    };
}];

ReconcileCtrl.resolve = {
    reconcile: ['$route', 'Reconcile', function ($route, Reconcile) {
        var id = $route.current.params.id,
            startDate = $route.current.params.StartDate,
            finishDate = $route.current.params.FinishDate;

        if (angular.isUndefined(id)) {
            return Reconcile.get({}).$promise;
        } else {
            return Reconcile.get({id: id, StartDate: startDate, FinishDate: finishDate}).$promise;
        }
    }]
};


