<script>
    export default {
        props: [
            'postcode',
            'address_list'
        ],
        data: function () {

            return {
                loaded: 0,
                visible: false,
                paf_address: ''
            };
        },
        methods: {
            clearError: function (field) {
                if (this.$get(field)) {
                    this.$set(field, 0);
                }
            },
            hasField: function (field) {
                try {
                    return eval('this.' + field);
                } catch (err) {
                    if (err instanceof ReferenceError) {
                        return false;
                    }
                }
            },
            hasAddresses: function () {
                if (typeof this.address_list === 'undefined') {
                    return 0;
                }
                return Object.keys(this.address_list.length);
            },
            setPafAddress: function () {
                var address_values = this.paf_address.split(',');

                $('#billing-address-1').val(address_values[0].replace('|', ','));
                $('#billing-address-2').val(address_values[1].replace('|', ','));
                $('#billing-city').val(address_values[2]);
            },
            triggerPAF: function (type) {
                var vm = this;
                this.busy = true;

                this.$http.post('/address-finder', {
                    postcode: this.postcode,
                    as_json: true
                }).then(function (response) {
                    this.busy = false;
                    this.address_list = response.data.addresses;
                    // We need to ensure that paf_address for address is updated so we reset it
                    vm.paf_address = false;
                    vm.paf_address = '';
                });
            }
        }
    }
</script>