(function () {
    global.dashModule
        .controller('bidsViewCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', bidsViewController])
        .directive('bidsView', [bidsViewDirective]);

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

    function bidsViewController($scope, $http, $, $translate, utilsService) {
        $scope.getBids = getBids;
        $scope.showBidMarginDialog = showBidMarginDialog;
        $scope.closeBidMarginDialog = closeBidMarginDialog;
        $scope.showBidRemoveDialog = showBidRemoveDialog;
        $scope.closeRemoveDialog = closeRemoveDialog;
        $scope.isCommercialLead = isCommercialLead;
        $scope.filterBids = filterBids;
        $scope.isUsedText = isUsedText;
        $scope.filteredBids = [];
        $scope.bidType = 'all';
        let bids = [];
        $scope.isMarginsDialogVisible = {};
        $scope.isRemoveDialogVisible = {};
        $scope.HEADER_COL = 'bid-header col-lg-4 col-md-5 col-sm-6 col-xs-6';
        $scope.LABEL_COL = 'col-lg-8 col-md-7 col-sm-6 col-xs-6';
        let userType = '';

        function getBids() {
            $http({
                method: 'POST',
                url: 'bids/api/getBids',
                data: $.param({
                    filter: {}
                })
            }).then(setBids, utilsService.onHttpError);
        }

        function setBids(response) {
            if (response.data && response.data.bids) {
                bids = response.data.bids;
                $scope.filteredBids = bids;
                userType = response.data.userType;
            }
        }

        function isCommercialLead() {
            return userType === 'commercial_lead';
        }

        function showBidMarginDialog(idBid) {
            $scope.$evalAsync(() => {
                $scope.isMarginsDialogVisible[idBid] = !$scope.isMarginsDialogVisible[idBid];
            });
        }

        function closeBidMarginDialog() {
            $scope.$evalAsync(() => {
                $scope.isMarginsDialogVisible = {};
            });
        }

        function showBidRemoveDialog(idBid) {
            $scope.$evalAsync(() => {
                $scope.isRemoveDialogVisible[idBid] = !$scope.isRemoveDialogVisible[idBid];
            });
        }

        function closeRemoveDialog() {
            $scope.$evalAsync(() => {
                $scope.isRemoveDialogVisible = {};
            });
        }

        function isSearchedValue(bid, searchText){
            const regExpresion = new RegExp(searchText, 'i');
            return regExpresion.test(String(bid.bidNumber)) ||
                regExpresion.test(bid.packageName) ||
                regExpresion.test(bid.customer) ||
                regExpresion.test(bid.commercialLead);
        }

        function filterBids(filterType) {
            if (filterType === 'all') {
                $scope.filteredBids = bids;
                $scope.searchText = '';
            }else if (filterType === 'available') {
                $scope.filteredBids = bids.filter((bid) => {
                    return bid.status === 'available' && isSearchedValue(bid, $scope.searchText);
                });
            }else if (filterType === 'used') {
                $scope.filteredBids = bids.filter((bid) => {
                    return bid.isUsed === 1 && isSearchedValue(bid, $scope.searchText);
                });
            }else if (filterType === 'expired') {
                $scope.filteredBids = bids.filter((bid) => {
                    return bid.status === 'expired' && isSearchedValue(bid, $scope.searchText);
                });
            }else{
                $scope.filteredBids = bids.filter((bid) => {
                    return isSearchedValue(bid, $scope.searchText);
                });
            }
        }

        function isUsedText(bid){
            return bid.isUsed === 1 ? 'Yes' : 'No';
        }
    }
})();
