(function () {
    global.dashModule
        .controller('addBidCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', addBidController])
        .directive('addBid', [addBidDirective]);

    function addBidDirective() {
        return {
            restrict: 'E',
            templateUrl: 'bids/html/addBidTemplate'
        };
    }

    function addBidController($scope, $http, $, $translate, utilsService) {
        $scope.initAddBid = initAddBid;
        $scope.bid = {};
        $scope.onCLSelect = onCLSelect;
        $scope.onCustomerSelect = onCustomerSelect;
        $scope.onPackageSelect = onPackageSelect;
        $scope.onPayTypeSelect = onPayTypeSelect;
        $scope.isAddSupBidDialogVisible = false;
        $scope.isLinkSupBidDialogVisible = false;
        $scope.showSupBidDialog = showSupBidDialog;
        $scope.showLinkBidDialog = showLinkBidDialog;
        $scope.linkSuppliers = linkSuppliers;
        $scope.saveBid = saveBid;
        $scope.supplierBids = [];

        function initAddBid() {
            getClCustomers();
        }

        function getClCustomers() {
            $http({
                method: 'GET',
                url: 'bids/api/getClCustomers'
            }).then(setClCustomers, utilsService.onHttpError);
        }

        function setClCustomers(response) {
            if (response.data) {
                $scope.clCustomers = response.data;
            }
        }

        function getPackages(idCommercialLead, idCustomer) {
            $http({
                method: 'POST',
                url: 'bids/api/getPackages',
                data: $.param({
                    idCommercialLead,
                    idCustomer
                })
            }).then(setPackages, utilsService.onHttpError);
        }

        function setPackages(response) {
            if (response.data) {
                $scope.packages = response.data;
            }
        }

        function getPaymentTypes(idCommercialLead, idCustomer, idPackage) {
            $http({
                method: 'POST',
                url: 'bids/api/getPayTypes',
                data: $.param({
                    idCommercialLead,
                    idCustomer,
                    idPackage
                })
            }).then(setPayTypes, utilsService.onHttpError);
        }

        function setPayTypes(response) {
            if (response.data) {
                $scope.payTypes = response.data;
            }
        }

        function onCLSelect(){
            $scope.selectedCustomer = null;
        }

        function onCustomerSelect() {
            $scope.selectedPackage = null;
            $scope.bid.idCustomerInstance = $scope.selectedCustomer ? $scope.selectedCustomer.idCustomerInstance : 0;

            if ($scope.selectedCl.idCommercialLead && $scope.selectedCustomer.idCustomer) {
                getPackages($scope.selectedCl.idCommercialLead, $scope.selectedCustomer.idCustomer);
            }
        }

        function onPackageSelect() {
            $scope.selectedPayType = null;
            $scope.bid.idPackage = $scope.selectedPackage ? $scope.selectedPackage.idPackage : 0;
            if($scope.selectedCl.idCommercialLead && $scope.selectedCustomer.idCustomer && $scope.selectedPackage){
                getPaymentTypes($scope.selectedCl.idCommercialLead, $scope.selectedCustomer.idCustomer, $scope.selectedPackage.idPackage);
            }
        }

        function onPayTypeSelect() {
            $scope.bid.idPaymentType = $scope.selectedPayType ? $scope.selectedPayType.idPaymentType : 0;
        }

        function showSupBidDialog() {
            $scope.$evalAsync(() => {
                $scope.isAddSupBidDialogVisible = !$scope.isAddSupBidDialogVisible;
            });
        }

        function showLinkBidDialog() {
            $scope.$evalAsync(() => {
                $scope.isLinkSupBidDialogVisible = !$scope.isLinkSupBidDialogVisible;
            });
        }

        function linkSuppliers(params){
            $scope.bid.supplierBids = params.selectedBids;
        }

        function saveBid(){
            $http({
                method: 'POST',
                url: 'bids/api/addBid',
                data: $.param({
                    bid: JSON.stringify($scope.bid)
                })
            }).then(displayMessage, utilsService.onHttpError);
        }

        function displayMessage(response){
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const translatedMessage = $translate.instant('bids.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                    if(messageObj.code === 'success'){
                        $scope.$evalAsync(() => {
                            $scope.bid = {};
                        });
                    }
                });
            }
        }
    }
})();
