(function () {
    "use strict";

    angular.module('pos').controller('inventarioAddController', controller);
    controller.$inject = ['$scope', '$state', '$stateParams', '$window', 'inventarioService', 'entidadeAuxiliarService'];

    function controller($scope, $state, $stateParams, $window, inventarioService, entidadeAuxiliarService) {
        $scope.$parent.initializeLayout('Novo Inventário');
                
        $scope.state = {
            data: {
                Setor: {},                
                Emissao: new Date(),
                Filial: {}
            },
            wizardIndex: 0,
            responsaveis: [],
            setores: [],
            filiais: [],
            carregando: false,
            modalResponsavel: false,
            modalSetor: false,
            filtro: { responsavel: "", setor: "" }
        };

        $scope.toggleModalResponsavel = function () {
            $scope.state.modalResponsavel = !$scope.state.modalResponsavel;
        }
        $scope.toggleModalSetor = function () {
            $scope.state.modalSetor = !$scope.state.modalSetor;
        }

        $scope.filterByResponsavel = function (item) {
            return (!$scope.state.filtro.responsavel || $scope.state.filtro.responsavel == "" || item.Nome.toUpperCase().indexOf($scope.state.filtro.responsavel.toUpperCase()) >= 0);
        }

        $scope.filterBySetor = function (item) {
            return (!$scope.state.filtro.setor || $scope.state.filtro.setor == "" || item.Nome.toUpperCase().indexOf($scope.state.filtro.setor.toUpperCase()) >= 0);
        }

        $scope.selecionarResponsavel = function (responsavel) {
            $scope.state.data.Responsavel = responsavel.Nome;
            $scope.toggleModalResponsavel();
        }

        $scope.selecionarSetor = function (setor) {
            $scope.state.data.Setor.Nome = setor.Nome;
            $scope.state.data.Setor.Codigo = setor.Codigo;
            $scope.toggleModalSetor();
        }

        $scope.nextStep = function () {
            if ($scope.state.wizardIndex < 1)
                $scope.state.wizardIndex += 1;
            else
                $scope.save($scope.state.data);
        }

        $scope.previousStep = function () {
            $scope.state.wizardIndex -= 1;
        }

        $scope.initialize = function () {
            $scope.refresh();
            getResponsavel();
            getAllFilial();
            //getSetor();
        };

        $scope.save = function () {
            inventarioService
                .save($scope.state.data)
                .then(
                    function (response) {
                        ;
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            $state.go("state-inventario-edit", { codigo: response.Data.Codigo, filial: response.Data.Filial.Codigo })
                            //$window.history.back();
                        }
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        };

        var getResponsavel = function () {
            entidadeAuxiliarService
                .getAllVendedor()
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.state.responsaveis = response.Data;
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }
        var getSetor = function () {
            entidadeAuxiliarService
                .getAllSetor()
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else
                            $scope.state.setores = response.Data;
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }

        var getAllFilial = function () {
            entidadeAuxiliarService
                .getAllFilial()
                .then(
                    function (response) {
                        if (response.Status < 0)
                            $scope.$parent.toastr.error(response.Message, 'Serviço');
                        else {
                            $scope.state.filiais = response.Data;
                            $scope.state.data.Filial.Codigo = $scope.$parent.config.Filial.Codigo;
                            $scope.state.data.Filial.Nome = $scope.$parent.config.Filial.Nome;
                        }
                    },
                    function (err) { $scope.$parent.toastr.error(err); }
                );
        }


        $scope.refresh = function () {
        };

        /*Inicializa funções*/
        $scope.initialize();
    }

})();