/**
 * @author monitour
 * created on 27.07.2017
 */
(function () {
    'use strict';

    angular.module('Monitour.pages.sites')
        .controller('sitesListCtrl', sitesListCtrl);

    /** @ngInject */
    function sitesListCtrl($scope, SitesService, $http, $filter,  $state, SweetAlert) {
        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 = [];
        }

        vm.checkpoints = [];
        vm.totalCheckpoints = 0;
        vm.checkpointsPerPage = 2; // this should match however many results your API puts on one page
        getResultsPage(1);

        vm.pagination = {
            current: 1
        };

        vm.pageChanged = function (newPage) {
            getResultsPage(newPage);
        };
        function getResultsPage(pageNumber) {
            var itemsPerPage = vm.checkpointsPerPage;
            // this is just an example, in reality this stuff should be in a service
            SitesService.GetAll(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) {
                    vm.status = 'Unable to load customer data: ' + error.message;
                });


        }

        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;

                SitesService.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;
                });


            };
        }

        var confirmtext = $filter('translate')('checkpointlist.deleteconfirm');
        var deletethisuser = $filter('translate')('checkpointlist.deletethisuser');
        var yesdeletethisuser = $filter('translate')('checkpointlist.yesdeletethisuser');
        var canceltext = $filter('translate')('checkpointlist.canceltext');
        var cancelledtext = $filter('translate')('checkpointlist.cancelledtext');
        var userdeleted = $filter('translate')('checkpointlist.userdeleted');
        var deleted = $filter('translate')('checkpointlist.deleted');
        var successtext = $filter('translate')('checkpointlist.successtext');

        vm.deleteCheckpoint =  function Delete(id, index) {

            SweetAlert.swal({

                title: confirmtext,
                text: deletethisuser,
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: yesdeletethisuser,
                cancelButtonText: canceltext,
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {

                    CheckpointsService.Delete(id)
                        .then(function (response) {
                            vm.rowCollection.splice(index, 1);
                            SweetAlert.swal(deleted, userdeleted,successtext);

                            console.log(response);
                        }, function (error) {
                            $scope.status = 'Unable to load customer data: ' + error.message;
                        });

                }
                else
                {

                    SweetAlert.swal("Cancelled", cancelledtext, "error");
                }
            });

        }
    }

})();
