myApp.controller('mutualSettlementsController', function ($scope, $http, $sce) {
    $scope.allCounterparties = null;
    $scope.allContracts = null;
    $scope.dateRangeFrom = null;
    $scope.dateRangeTo = null;
    if (typeof allCounterparties !== 'undefined'){
        $scope.allCounterparties = allCounterparties;
        $scope.allCounterpartiesLength = Object.keys($scope.allCounterparties).length;
    }
    if (typeof allContracts !== 'undefined'){
        $scope.allContracts = allContracts;
        $scope.allCountractsLength = Object.keys($scope.allContracts).length;
    }

    $scope.getMutualSettlementsArray = function () {
        var form = {};
        form.period = $scope.dateRangeFrom+' - '+$scope.dateRangeTo;
        form.counterparty = +$scope.counterparty;
        form.contract = +$scope.contract;
        $http.get('/get-mutual-settlements', {params: form}).success(function (response) {
            if (response.status === 'error'){
                swal(
                    translate('Error', mutualSettlementsVocabulary),
                    response.message,
                    'error'
                );
                return null;
            }
            $scope.data = {
                data: null,
                sumBeforeDebit: +response.data.sumBefore >= 0 ? response.data.sumBeforeWithCurrency : null,
                sumBeforeCredit: +response.data.sumBefore < 0 ? response.data.sumBeforeWithCurrency : null,
                sumAfterDebit: +response.data.sumAfter >= 0 ? response.data.sumAfterWithCurrency : null,
                sumAfterCredit: +response.data.sumAfter < 0 ? response.data.sumAfterWithCurrency : null,
            };
            //prepare data to show
            $scope.data.data = response.data.data.map(function (item) {
                return {
                    date: item.date,
                    time: item.time,
                    sumPositive: item.sumDebit,
                    link: item.link ? $sce.trustAsHtml('<a href="'+item.link+'" target="_blank">'+item.name+'</a>') : item.name,
                    sumNegative: item.sumCredit,
                }
            });
        });
    };

    $scope.changeCounterparty = function(id){
        $http.get('/get-contracts', {params: {counterpartyId: id}}).success(function (response) {
            if (response.status === 'success'){
                $scope.allContracts = response.data;
                $scope.contract = String(response.data[0].id);
                setTimeout(function () {
                    $('.contract-data').selectpicker('val', String(response.data[0].id));
                    $('.contract-data').selectpicker('refresh');
                },1);
            }

        })
    };
    //init date range
    $scope.initDateRange = function () {
        var currentDate = new Date();
        // dateFrom = first day of month, current month, current year
        var from = [
            '01',
            $scope.getDateWithZero(currentDate.getMonth()+1),
            currentDate.getFullYear()
        ];
        var to = [
            $scope.getDateWithZero(currentDate.getDate()),
            $scope.getDateWithZero(currentDate.getMonth()+1),
            currentDate.getFullYear()
        ];
        $scope.dateRangeFrom = from.join('.');
        $scope.dateRangeTo = to.join('.');
        document.querySelector('#mutualSettlementsPeriod').value = $scope.dateRangeFrom;
        document.querySelector('#w0-2').value = $scope.dateRangeTo;
    };
    $scope.getDateWithZero = function (date) {
        return +date < 10 ? '0'+date : date;
    };
    $scope.initDateRange();
    $(document).on('change', '#mutualSettlementsPeriod', function (e) {
        $scope.dateRangeFrom = $(this).val();
    });
    $(document).on('change', '#w0-2', function (e) {
        $scope.dateRangeTo = $(this).val();
    });
    //initial data
    setTimeout(function () {
        $scope.getMutualSettlementsArray();
    })
});