'use strict';

var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'dateFilter', 'reconcile', 'Reconcile', 'Account', function ($scope, $routeParams, $location, dateFilter, reconcile, Reconcile, Account) {
    $scope.info = reconcile;
    $scope.show = function () {
        var id = $scope.info.Account.LedgerID;
        var start_date = angular.isDate($scope.info.StartDate) ? dateFilter($scope.info.StartDate, 'dd-MMM-yyyy') : $scope.info.StartDate;
        var finish_date = angular.isDate($scope.info.FinishDate) ? dateFilter($scope.info.FinishDate, 'dd-MMM-yyyy') : $scope.info.FinishDate;
        if (id === $routeParams.id && start_date === $routeParams.StartDate && finish_date === $routeParams.FinishDate) {
            Reconcile.get({id: id, StartDate: start_date, FinishDate: finish_date}, function (data) {
                $scope.info = data;
            });
        } else {
            $location.path('/Reconcile/' + id).search('StartDate', start_date).search('FinishDate', finish_date);
        }
    };
    $scope.save = function () {
        var i, l,
            len = $scope.info.Body.length;
        for (i = 0, l = len; i < l; i++) {
            if (angular.isDate($scope.info.Body[i].ReconcileDate)) {
                $scope.info.Body[i].ReconcileDate = dateFilter($scope.info.Body[i].ReconcileDate, 'dd-MMM-yyyy');
            }
        }
        $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 () {
            if ($scope.selected > 0) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected -= 1;
                });
            }
        },
        'down': function () {
            if ($scope.selected < $scope.info.Body.length - 1) {
                $scope.$apply(function () {
                    $scope.selected = $scope.selected += 1;
                });
            }
        },
        'alt+r': function () {
            console.log('alt+r');
        },
        'enter': function () {
            var path = $scope.info.Body[$scope.selected].Url.replace(/^(?:\/\/|[^\/]+)*/, "");
            $scope.$apply(function () {
                $location.path(path).search('StartDate', null).search('FinishDate', null);
            });
        },
        'alt+u': function () {
            console.log('alt+u');
        }
    };

    $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,
            start_date = $route.current.params.StartDate,
            finish_date = $route.current.params.FinishDate;

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


