(function () {
    global.dashModule
        .controller('suppliersProductsAddEditCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', suppliersProductsAddEditCtrl])
        .directive('suppliersProductsAddEdit', [suppliersProductsAddEditDirective]);

    function suppliersProductsAddEditDirective() {
        return {
            restrict: 'E',
            templateUrl: 'suppliers/html/suppliersProductsAddEditFormTemplate'
        };
    }

    function suppliersProductsAddEditCtrl($scope, $http, $, $translate, utilsService) {
        $scope.productsFormInit = productsFormInit;
        $scope.addEditSupplierProducts = addEditSupplierProducts;
        $scope.isAddAction = isAddAction;
        $scope.getProductCategories = getProductCategories;
        $scope.isPriceRecurring = isPriceRecurring;
        $scope.data = $scope.data || {};
        $scope.virtual = $scope.virtual || {};
        $scope.countries = [];
        $scope.suppliers = [];
        $scope.productCategories = [];
        $scope.selectedCategory = '';

        function productsFormInit(action) {
            getCountries();
            getSuppliers();
            getProductCategories();
            $scope.formAction = action;
        }

        function isAddAction() {
            return $scope.formAction === 'ADD';
        }

        function isPriceRecurring() {
            return $scope.data.isPriceRecurring === '1';
        }

        function getCountries() {
            const params = $.param({getArray : true});
            $http({
                method: 'POST',
                data: params,
                url: 'countries/api/getAllCountries'

            }).then(showCountries, utilsService.onHttpError);
        }

        function showCountries(response) {
            $scope.countries = response.data;
        }

        function getSuppliers() {
            $http({
                method: 'GET',
                url: 'suppliers/api/getSuppliers'

            }).then(showSuppliers, utilsService.onHttpError);
        }

        function showSuppliers(response) {
            $scope.suppliers = response.data.data;
        }

        function getProductCategories(selectedCategory = '') {
            $scope.selectedCategory = selectedCategory;
            $http({
                method: 'GET',
                url: 'suppliers/api/getProductCategories'
            }).then(showProductCategories, utilsService.onHttpError);
        }

        function showProductCategories(response) {
            $scope.productCategories = response.data;

            if ($scope.selectedCategory) {
                const categorySelected = $scope.productCategories.find(productTypes => {
                    return productTypes.category === $scope.selectedCategory;
                });

                $scope.data.productCategory = {
                    id: categorySelected.id
                };
            }
        }

        function addEditSupplierProducts() {
            const params = $.param($scope.data);
            const url = 'suppliers/api/' + $scope.formAction.toLowerCase() + 'SupplierProduct';

            $http({
                method: 'POST',
                url: url,
                data: params
            }).then(showUpdateMessage, utilsService.onHttpError);
        }

        function showUpdateMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const key = messageObj.key ? $translate.instant('suppliers.tables.headers.' + messageObj.key) : '';
                    let translatedMessage = $translate.instant('suppliers.messages.' + messageObj.message);
                    translatedMessage = key !== '' ? key + ': ' + translatedMessage : translatedMessage;
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                    if (messageObj.code === 'success') {
                        if (typeof $scope.onUpdated !== 'undefined') {
                            $scope.onUpdated();
                        } else {
                            Object.keys($scope.data).forEach(productData => {
                                if (productData !== 'idCountry' && productData !== 'idSupplier') {
                                    $scope.data[productData] = '';
                                }
                            });
                        }
                    }

                });
            }
        }
    }
})();
