(function () {
    global.dashModule
        .controller('customersViewCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', customersViewController])
        .directive('customersView', [customersViewDirective]);

    function customersViewDirective() {
        return {
            restrict: 'E',
            templateUrl: 'customers/html/customersViewTemplate'
        };
    }

    function customersViewController($scope, $http, $, $translate, utilsService) {
        $scope.viewCustomersIntit = viewCustomersIntit;
        $scope.saveDefaultOrderType = saveDefaultOrderType;
        $scope.saveCustomerOrderTypes = saveCustomerOrderTypes;
        $scope.isSameCompany = isSameCompany;

        const RESELLER_ORDER_TYPE = '2';

        function viewCustomersIntit(){
            getCustomers();
            getOrderTypes();
        }

        function getCustomers() {
            const params = $.param({
                idPackage: global.getParameterByName('idPackage') || 0
            });

            $http({
                method: 'POST',
                url: 'customers/api/getComercialLeadCustomers',
                data: params
            }).then(showCustomers, utilsService.onHttpError);
        }

        function showCustomers(response) {
            if (response.data) {
                $scope.customers = response.data.customers;
                $scope.defaultIdOrderType = response.data.defaultIdOrderType;

                $scope.customers.forEach(customer => {
                    if(isSameCompany(customer.isSameCompanyAsCl)){
                        customer.idOrderType = RESELLER_ORDER_TYPE;
                    }
                });
            }
        }

        function getOrderTypes() {
            $http({
                method: 'GET',
                url: 'customers/api/getOrderTypes'
            }).then(setOrderTypes, utilsService.onHttpError);
        }

        function setOrderTypes(response) {
            if(response.data) {
                $scope.orderTypes = response.data;
            }
        }

        function saveDefaultOrderType(){
            const params = $.param({
                defaultIdOrderType: $scope.defaultIdOrderType
            });

            $http({
                method: 'POST',
                url: 'customers/api/saveDefaultOrderType',
                data: params
            }).then(showUpdateDefaultMessage, utilsService.onHttpError);
        }

        function saveCustomerOrderTypes(){
            const params = $.param({
                customers: JSON.stringify($scope.customers)
            });

            $http({
                method: 'POST',
                url: 'customers/api/saveCustomersOrderTypes',
                data: params
            }).then(showUpdateDefaultMessage, utilsService.onHttpError);
        }

        function showUpdateDefaultMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    let translatedMessage = $translate.instant('customers.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                });
            }
        }

        function isSameCompany(isSameCompanyAsCl){
            return parseInt(isSameCompanyAsCl) === 1;
        }
    }
})();
