(function () {
    global.dashModule
        .controller('procurementCtrl', ['$scope', '$', '$http', '$translate', 'Upload', 'utilsService', 'ordersUtilsService', procurementCtrl])
        .directive('procurement', [procurementDirective]);

    function procurementDirective() {
        return {
            restrict: 'E',
            templateUrl: 'orders/html/procurementTemplate'
        };
    }

    function procurementCtrl($scope, $, $http, $translate, Upload, utilsService, ordersUtilsService) {
        const step = $scope.$parent.step;
        const idOrder = step.idOrder;
        $scope.getSuppliersByPackageOrder = getSuppliersByPackageOrder;
        $scope.uploadFile = uploadFile;
        $scope.removeOrderDocument = removeOrderDocument;
        $scope.isDialogVisible = {};
        $scope.showHideDialog = showHideDialog;
        $scope.selectPackage = selectPackage;
        $scope.selectedPackage = {};

        function selectPackage(packageObj, idSupplier){
            $scope.selectedPackage[idSupplier] = packageObj;
        }

        function getSuppliersByPackageOrder() {
            const params = $.param({
                idOrder,
                documentType: 'configuration'
            });

            $http({
                method: 'POST',
                data: params,
                url: 'orders/api/getSuppliersByPackageOrder'
            }).then(setProductEstimations, utilsService.onHttpError);
        }

        function setProductEstimations(response) {
            if (response.data) {
                $scope.suppliersData = response.data;
                $.each($scope.suppliersData, (name, details) => {
                    details.documents.forEach(docDetails => {
                        $scope.isDialogVisible[docDetails.idDocument] = false;
                    });
                });
            }
        }

        function uploadFile(file, idSupplier) {
            Upload.upload({
                url: 'orders/api/uploadConfigurationDocument',
                method: 'POST',
                file: file,
                data: {
                    idPackage : $scope.selectedPackage[idSupplier] && $scope.selectedPackage[idSupplier].idPackage || 0,
                    idOrder,
                    idSupplier
                }
            }).then(displayMessage, utilsService.onHttpError);
        }

        function displayMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const translatedMessage = $translate.instant('orders.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                    getSuppliersByPackageOrder();
                    ordersUtilsService.getOrderInfo();
                });
            }
        }

        function removeOrderDocument(document) {
            const params = $.param({
                idOrder,
                idPackage: document.idPackage,
                idDocument: document.idDocument
            });

            $http({
                method: 'POST',
                data: params,
                url: 'orders/api/removeOrderDocument'
            }).then(displayMessage, utilsService.onHttpError);
        }

        function showHideDialog(idDocument) {
            $scope.$evalAsync(() => {
                $scope.isDialogVisible[idDocument] = !$scope.isDialogVisible[idDocument];
            });
        }
    }
})();
