/**
 * @author monitour
 * created on 27.07.2017
 */
(function() {
	'use strict';
	angular.module('Monitour.pages.billing').controller('billListCtrl', billListCtrl);
	/** @ngInject */
	function billListCtrl($scope,  $http,    $localStorage ,$state, SweetAlert,  $translate) {
		var vm = this;
		
       vm.filter = filter;


        vm.clienttoggle = function (client) {
            vm.client = client;
            console.log(vm.client.clientName);
        }
        vm.sitetoggle = function (site) {
            vm.site = site;
            console.log(vm.site.siteName);
        }
        vm.devicetoggle = function (device) {
            vm.device = device;
            console.log(vm.device);
        }
        vm.date = {
            startDate: moment().subtract(1, "days"),
            endDate: moment()
        };
        //Watch for date changes
        $scope.$watch('date', function (newDate) {
            console.log('New date set: ', newDate);
        }, false);

        $scope.checked = false;
        $scope.size = '100px';
        $scope.toggle = function () {
            $scope.checked = !$scope.checked
        }
        vm.reset = function () {
            vm.client = [];
            vm.site = [];
        }
		function filter() {


            if (!vm.client) {
                console.log('no client');
                vm.clientselected = null;
                var client = vm.clientselected;
            } else {
                console.log('yes client');

                vm.clientselected = vm.client.client;
                var client = vm.clientselected;
            }


            if (!vm.site) {

                vm.siteselected = null;

                var site =  vm.siteselected;
            } else {


                vm.siteselected = vm.site.siteId;

                var site =  vm.siteselected;

            }
            vm.loading = true;

            getResultsPage(1);


            vm.pageChanged = function (newPage) {
                getResultsPage(newPage);
            };
            function getResultsPage(pageNumber) {
                var itemsPerPage = vm.checkpointsPerPage;

                CheckpointsService.GetFilters(client, site, pageNumber, itemsPerPage).then(function (response) {

                    vm.checkpoints = response.data[0].result;
                    vm.totalCheckpoints = response.data[0].count;
                    console.log(vm.totalCheckpoints);
                    console.log(vm.checkpoints);
                }, function (error) {
                    $scope.status = 'Unable to load customer data: ' + error.message;
                });


            };
        }
	}
})();