window.onload = function() {
    function getCzechlogisticCountry() {
        return $('#czechlogistic_country');
    }

    function getCzechlogisticPackageType() {
        return $('#czechlogistic_type');
    }

    function getCzechlogisitcCarrier() {
        return $('#czechlogistic_carrier');
    }

    function disableCzechlogisticInputs(disable = true) {
        getCzechlogisticCountry().attr('disabled', disable);
        getCzechlogisitcCarrier().attr('disabled', disable);
        getCzechlogisticPackageType().attr('disabled', disable);
    }

    function czechlogisticInit() {
        if (getCzechlogisticCountry().val() === undefined && getCzechlogisticCountry().val() === null) {
            getCzechlogisticCountry().val([]);
            getCzechlogisitcCarrier().val([]);
            getCzechlogisticPackageType().val([]);
        }
    }

    czechlogisticInit();

    getCzechlogisticCountry().on('change', function() {
        getCzechlogisitcCarrier().val([]);
        getCzechlogisticPackageType().val([]);
        czechlogisticLoadCarriers($(this).val());
    });

    getCzechlogisitcCarrier().on('change', function() {
        getCzechlogisticPackageType().val([]);
        czechlogisticLoadParcelTypes($(this).val());
    })

    function czechlogisticLoadCarriers(countryId) {
        if (countryId === undefined || countryId === null) return;

        $.ajax({
            url: paxyCarriersUrl,
            data: {
                'countryId': countryId
            },
            success: function (resp) {
                handleCarriersResponse(resp);
            },
            error: function (resp) {

            }
        })
    }

    function handleCarriersResponse(resp) {
        var czechlogisitcCarrier = getCzechlogisitcCarrier();
        czechlogisitcCarrier.html('');
        czechlogisitcCarrier.append(`<option></option>`);
        if (resp.carriers !== undefined && resp.carriers !== null && resp.carriers.length > 0) {
            resp.carriers.map(function (carrier) {
                czechlogisitcCarrier.append(`<option value="${carrier.id}">${carrier.name}</option>`);
            })
        }

    }

    function czechlogisticLoadParcelTypes(carrierId) {
        if (carrierId === undefined || carrierId === null) return;

        $.ajax({
            url: paxyDimensionsUrl,
            data: {
                'countryId': getCzechlogisticCountry().val(),
                'carrierId': carrierId,
            },
            success: function (resp) {
                handleParcelTypesResponse(resp);
            },
            error: function (resp) {

            }
        })
    }

    function handleParcelTypesResponse(resp) {
        var czechlogisitcParcelType = getCzechlogisticPackageType();
        czechlogisitcParcelType.html('');
        if (resp.dimensions !== undefined && resp.dimensions !== null && resp.dimensions.length > 0) {
            resp.dimensions.map(function (dimension) {
                czechlogisitcParcelType.append(`<option value="${dimension.id}">${dimension.name}</option>`);
            });
        }
    }

}

