app.controller("amortizacoesController", function ($scope, notificationFactory, globalService, httpRequestFactory, ngProgress, $upload) {
    $scope.ano = new Date().getFullYear();
    $scope.apenasBem = false;
        

    $scope.gravarAmortizacoes = function () {

        var getSuccessCallback = function (data, status) {
            notificationFactory.success(data);
            ngProgress.complete();
        };

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

        var dados = {
            Ano: $scope.ano,
            Mes: $scope.selectedMes.mes
        };
        httpRequestFactory.postData('../api/cadastro/recalcular/amortizacoes/mensal', dados).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };
        

    $scope.strMax = function (str, max) {
        if (str == null)
            return "";
        var c = "";
        if (str.length > max)
            c = "...";
        return str.substr(0, max) + c;
    };

    $scope.mostrarMes = function () {
        var Data = new Date();
        return Data.getFullYear() >= 2016;
    };

    $scope.lstMeses = [];
    $scope.carregarMeses = function () {
        $scope.lstMeses = globalService.devListaMeses();
        $scope.selectedMes = $scope.selMesNumero(new Date().getMonth() + 1);
    };

    $scope.selMes = function (row) {
        $scope.selectedMes = row;
    };

    $scope.selMesNumero = function (mes) {
        var row = null;
        for (i = 0; i < 12; i++) {
            if ($scope.lstMeses[i].mes == mes) {
                row = $scope.lstMeses[i];
                break;
            }
        }
        return row;
    };

    //$scope.onFileSelectExcel = function ($files) {
    //    var validExts = new Array(".xlsx", ".xls");

    //    $scope.files = [];
    //    if ($files.length > 0) {
    //        for (var i = 0; i < $files.length; i++) {
    //            var $file = $files[i];
    //            var fileName = $file.name;
    //            var fileExt = fileName.substring(fileName.lastIndexOf('.'));
    //            console.log(validExts.indexOf(fileExt));
    //            if (validExts.indexOf(fileExt) > -1) {
    //                $scope.files.push($file);
    //            }
    //            else {

    //                $('#btUpload').val(null);
    //                var input = $('#btUpload');
    //                input.replaceWith(input.val(null).clone(true));
    //            }
    //        }
    //    }
    //};
    //Upload
    $scope.myDocFile = "";
    $scope.lstDadosBensExcel = [];
    $scope.onFileVidaUtilBemExcel = function ($files, info) {
        //if ($files.length.length != 1) {
        //    notificationFactory.error("Só é permitido um ficheiro de cada vez.")
        //    return;
        //}
        
        var $file = $files[0];
        (function (index) {
                $upload.upload({
                    url: "../api/cadastro/carregar/BensVidaUtil/excel/",
                    method: "POST",
                    file: $file
                }).progress(function (evt) {
                    // get upload percentage
                    $scope.uploadprogress = parseInt(100.0 * evt.loaded / evt.total);

                }).success(function (data, status, headers, config) {
                    if (data.Success == false) {
                        notificationFactory.error(data.Message);
                    }
                    else
                        $scope.lstDadosBensExcel = data.Dados;

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

         })(0);
        
    };


    $scope.recalcularAmortizacoesVidaUtilInovarPorExcel = function (lstDados, acesso) {
        if ($scope.lstDadosBensExcel.length == 0) {
            notificationFactory.error("Não existem dados para recalcular");
            return;
        }

        var getSuccessCallback = function (data, status) {
            $scope.lstDadosBensExcel = [];
            notificationFactory.success(data);
            ngProgress.complete();
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
            ngProgress.reset();
        };
                
        //httpRequestFactory.postData('../api/cadastro/recalcular/gravarAmortizacoes/VidaUtil/inovar', lstDados).success(getSuccessCallback).error(errorCallback);
        var h = { pass: acesso };
        httpRequestFactory.postDataHeader('../api/cadastro/recalcular/gravarAmortizacoes/VidaUtil/inovar', lstDados,h).success(getSuccessCallback).error(errorCallback);
        ngProgress.start();
    };

});