(function () {
    global.dashModule
        .controller('uploadDocumentsForOrderPackageCtrl', ['$scope', '$', '$http', '$translate', 'utilsService', 'Upload', uploadDocumentsForOrderPackageCtrl])
        .directive('uploadDocumentsForOrderPackage', [uploadDocumentsForOrderPackageDirective]);

    function uploadDocumentsForOrderPackageDirective() {
        return {
            restrict: 'E'
        };
    }

    function uploadDocumentsForOrderPackageCtrl($scope, $, $http, $translate, utilsService, Upload) {
        $scope.getDocumentTypes = getDocumentTypes;
        $scope.setDocumentTypes = setDocumentTypes;
        $scope.uploadFile = uploadFile;
        $scope.selectFileType = selectFileType;
        $scope.selectPackage = selectPackage;
        $scope.documentTypes = [];
        $scope.fileName = '';
        $scope.selectedFileType = {};
        $scope.selectedPackage = {};
        $scope.isOrderOngoingOrCompleted = isOrderOngoingOrCompleted;

        function getDocumentTypes() {
            $http({
                url: 'documents/api/getDocumentTypes',
                method: 'POST',
                data: $.param({
                    withoutTemplates: true
                })
            }).then(setDocumentTypes, utilsService.onHttpError);
        }

        function setDocumentTypes(response){
            if(response.data && response.data.length){
                $scope.documentTypes = response.data.filter((documentType) => {
                    return documentType.isSpecialType !== 1;
                });
            }
        }

        function uploadFile(file) {
            Upload.upload({
                url: 'orders/api/uploadOrderDocument',
                method: 'POST',
                file: file,
                data: {
                    idOrder: global.getParameterByName('idOrder') || 0,
                    idPackage: $scope.selectedPackage.idPackage || 0,
                    idDocumentType: $scope.selectedFileType.idDocumentType || 0,
                    fileName : $scope.fileName
                }
            }).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);
                    if(messageObj.code === 'success'){
                        $scope.fileName = '';
                    }
                });
            }
        }

        function selectFileType(docType){
            $scope.selectedFileType = docType;
        }

        function selectPackage(packageObj){
            $scope.selectedPackage = packageObj;
        }

        function isOrderOngoingOrCompleted() {
            return ['in-progress', 'production'].includes($scope.$parent.$parent.$parent.ordersInfo.status);
        }
    }
})();
