(function () {
    "use strict";

    angular.module('pos').controller('omniOffHouseController', controller);
    controller.$inject = ['$scope', '$window', 'OMNIDashBoardService', 'pedidoService'];

    function controller($scope, $window, omnidashboardService, pedidoService) {
        $scope.$parent.initializeLayout('OMNI');

        $scope.omni = {
            data: [],
            carregando: true,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                chaves: '',
                cpf: '',
                status: '',
                emissao: {
                    de: '',
                    ate: ''
                },
                total: {
                    de: '',
                    ate: ''
                },
                sortexpression: '',
                skip: 0,
                take: 20,
                statusOmni: 0,
                tipoOMNI: 1
            }
        };
        $scope.omniConcluidos = {
            data: [],
            carregando: true,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                chaves: '',
                cpf: '',
                status: '',
                emissao: {
                    de: '',
                    ate: ''
                },
                total: {
                    de: '',
                    ate: ''
                },
                sortexpression: '',
                skip: 0,
                take: 20,
                statusOmni: 2,
                tipoOMNI: 1
            }
        };

        $scope.getPedidosOmni = function () {
            $scope.omni.filtro.skip = 0;
            $scope.omni.carregando = true;
            $scope.omni.vazio = false;

            pedidoService.getAll($scope.omni.filtro)
                .then(
                    function (response) {

                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            
                            angular.copy(angular.fromJson(response.Data), $scope.omni.data);
                        }

                        $scope.omni.carregando = false;
                        $scope.omni.vazio = ($scope.omni.data.length == 0);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.countPedidosOmni = function () {
            pedidoService.count($scope.omni.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.omni.count = response.Data;
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }
        $scope.getMorePedidosOmni = function () {
            $scope.omni.filtro.skip += $scope.omni.filtro.take;
            pedidoService.getAll($scope.omni.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.omni.data = $scope.omni.data.concat(response.Data);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.getPedidosOmniConcluido = function () {
            $scope.omniConcluidos.filtro.skip = 0;
            $scope.omniConcluidos.carregando = true;
            $scope.omniConcluidos.vazio = false;

            pedidoService.getAll($scope.omniConcluidos.filtro)
                .then(
                    function (response) {

                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            
                            angular.copy(angular.fromJson(response.Data), $scope.omniConcluidos.data);
                        }

                        $scope.omniConcluidos.carregando = false;
                        $scope.omniConcluidos.vazio = ($scope.omniConcluidos.data.length == 0);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.countPedidosOmniConcluido = function () {
            pedidoService.count($scope.omniConcluidos.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.omniConcluidos.count = response.Data;
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }
        $scope.getMorePedidosOmniConcluido = function () {
            $scope.omniConcluidos.filtro.skip += $scope.omniConcluidos.filtro.take;
            pedidoService.getAll($scope.omniConcluidos.filtro)
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.omniConcluidos.data = $scope.omniConcluidos.data.concat(response.Data);
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        $scope.tabs = { pendentes: 0, concluidos: 1 };
        $scope.currentTab = 0;
        $scope.setCurrentTab = function (tabindex) {
            $scope.currentTab = tabindex;


            if (tabindex == 0) {

                $scope.getPedidosOmni();
                $scope.countPedidosOmni();
            } else if (tabindex == 1) {

                $scope.getPedidosOmniConcluido();
                $scope.countPedidosOmni();
            }
        };
        $scope.refresh = function () {

            $scope.setCurrentTab($scope.currentTab);
            $scope.countPedidosOmni();
        }

        $scope.refresh();

    }



})();
