//const _SelectCultureName = "Français";

// Déclaration des variables Pays, province et ville
let _SelectCountryName = 'canada';
let _SelectStateName = 'québec';
let _SelectMunicipalityName = 'joliette';

function Address() {
    const self = this;

    this.getCountry = function() {
        const lUrlApi = getRootApi() + 'address/country/';
        console.log(lUrlApi);

        return $.ajax({
            type: 'GET',
            contentType: 'application/json',
            url: lUrlApi,
            // async: false,
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            $('#country').empty(); // Clear the table body.
            const fLen = data.length;
            let lSelect = '';
            let lValue = '-1';
            for (let lIdx = 0; lIdx < fLen; lIdx++) {
                lSelect = lSelect + '<option value="' + data[lIdx].id + '">' + data[lIdx].message + '</option>';
                if (data[lIdx].message.toLowerCase() === _SelectCountryName){
                    lValue = data[lIdx].id;
                }
            }
            document.getElementById('country').innerHTML = lSelect;
            $('#country').val(lValue);
            self.getState();
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
                _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };

    this.getState = function() {
        let lCountryId = '0';
        $('select#country option:selected').each(function() {
            lCountryId = $(this).val();
        });

        const lUrlApi = getRootApi() + 'address/country/' + lCountryId + '/state/';
        console.log(lUrlApi);

        return $.ajax({
            type: 'GET',
            contentType: 'application/json',
            url: lUrlApi,
            // async: false,
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            $('#state').empty(); // Clear the table body.
            const fLen = data.length;
            let lSelect = '';
            let lValue = '-1';
            for (let lIdx = 0; lIdx < fLen; lIdx++) {
                lSelect = lSelect + '<option value="' + data[lIdx].id + '">' + data[lIdx].message + '</option>';
                if (data[lIdx].message.toLowerCase() === _SelectStateName){
                    lValue = data[lIdx].id;
                }
            }
            document.getElementById('state').innerHTML = lSelect;
            $('#state').val(lValue);
            self.getMunicipality();
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
                _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };

    this.getMunicipality = function() {
        let lStateId = '0';
        $('select#state option:selected').each(function() {
            lStateId = $(this).val();
        });

        const lUrlApi = getRootApi() + 'address/country/state/' + lStateId + '/city/';
        console.log(lUrlApi);

        return $.ajax({
            type: 'GET',
            contentType: 'application/json',
            url: lUrlApi,
            // async: false,
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            $('#city').empty(); // Clear the table body.
            const fLen = data.length;
            let lSelect = '';
            let lValue = '0';
            for (let lIdx = 0; lIdx < fLen; lIdx++) {
                lSelect = lSelect + '<option value="' + data[lIdx].id + '">' + data[lIdx].message + '</option>';
                if (data[lIdx].message.toLowerCase() === _SelectMunicipalityName.toLowerCase()){
                    lValue = data[lIdx].id;
                }
            }
            document.getElementById('city').innerHTML = lSelect;
            $('#city').val(lValue);
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
                _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };
}

const _Address = new Address();

$( document ).ready(function() {
    _Address.getCountry();

    $('#country').on('change', function(e) {
        _Address.getState();
    });

    $('#state').on('change', function(e) {
        _Address.getMunicipality();
    });
});
