app.controller("dupCadController", function ($scope,$modal,cadastroFactory,notificationFactory) {

    $scope.repetirCadastro = function (elem) {
        if (elem == null) {
            notificationFactory.success("Seleccione um Inventário");
            return;
        }

        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);

            
            $scope.$parent.cancel("recarregar");
            
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        if (elem.Aquisicao.Valor == "" || isNaN(elem.Aquisicao.Valor))
            elem.Aquisicao.Valor = 0;
        if (elem.Aquisicao.ValorRenda == "" || isNaN(elem.Aquisicao.ValorRenda))
            elem.Aquisicao.ValorRenda = 0;
        if (elem.Aquisicao.ValorResidual == "" || isNaN(elem.Aquisicao.ValorResidual))
            elem.Aquisicao.ValorResidual = 0;
        if (elem.Aquisicao.NumeroRendas == "" || isNaN(elem.Aquisicao.NumeroRendas))
            elem.Aquisicao.NumeroRendas = 0;
        if (elem.Abate.Receita == "" || isNaN(elem.Abate.Receita))
            elem.Abate.Receita = 0;

        cadastroFactory.duplicaCadastro(elem).success(successCallback).error(errorCallback);

    };

    $scope.confirmarDuplicarCadastro = function (row) {

        if (row == null)
            return;

        row.NumRepetir = 1;
        

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalDuplicarInv.html?v=' + new Date(),
            controller: 'modalController',
            resolve: {
                items: function () { return row; }
            }
        });

        modalInstance.result.then(function (item) {

            $scope.repetirCadastro(item);

        }, function () {

        });
    };
        

});