angular.module('RDash')
    .controller('paidHistoryController', ['$scope', 'paidHistoryService', 'toaster', 'ngDialog', 'DTOptionsBuilder', function ($scope, paidHistoryService, toaster, ngDialog, DTOptionsBuilder) {
        $scope.paids = [];

        $scope.getPaids = function (date) {
            var Dates = {
                StartDate: date.StartDate,
                EndDate: date.EndDate
            };
            paidHistoryService.getPaids(Dates).then(function (response) {
                if (response.data.length === 0) {
                    toaster.pop('note', "", "No hay registros en la Base de Datos");
                    $scope.paids = response.data;
                } else {
                    $scope.paids = response.data;
                }
            }, function (response) {
                toaster.pop('error', "ERROR", "Falló la conexión con el servidor");
            });
        };
        $scope.dtOptions = DTOptionsBuilder.newOptions()
            //.withDOM('lBrtip')
            .withPaginationType('full_numbers')
            .withOption('order', [1, 'asc']).withOption('lengthMenu', [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]])
            // Active Buttons extension
            .withButtons([
                'colvis',
                'copy',
                'print',
                'excel',
                'pdf'
            ]);

        $scope.getTotal = function () {
            var total = 0;
            angular.forEach($scope.paids, function (value, index) {
                console.log(value.TotalAdeudo);
                total += value.TotalAdeudo;
            })
            return total;
        };

        var GetData = function () {
            paidHistoryService.getChart().then(function (response) {
                $scope.data = response.data;
                $scope.chartData = [
                    {
                        key: "Chart",
                        values: $scope.data
                    }
                ];
            }, function (response) {
                toaster.pop('error', "ERROR", "Falló la conexión con el servidor");
            });
        };

        $scope.chartOptions = {
            chart: {
                type: 'discreteBarChart',
                height: 600,
                // margin: {
                //     top: 20,
                //     right: 20,
                //     bottom: 50,
                //     left: 55
                // },
                x: function (d) { return d.label; },
                y: function (d) { return d.value; },
                showValues: true,
                valueFormat: function (d) {
                    return d3.format('.0f')(d);
                },
                duration: 500,
                xAxis: {
                    axisLabel: 'Concesionaria'
                },
                yAxis: {
                    axisLabel: 'Dias Promedio de Pago',
                    axisLabelDistance: 20
                }
            }
        };
        GetData();
    }]);