<template>
    <div class="row">
        <div class="col no-padding">
            <div class="form-group form-group-default required">
                <label class="muted">Address Line 1</label>
                <input class="form-control" name="street_one" v-model="address.street_one" @change="changeAddress()" autocomplete="off">
            </div>
            <div class="form-group form-group-default">
                <label class="muted">Address Line 2</label>
                <input class="form-control" name="street_two" v-model="address.street_two" @change="changeAddress()" autocomplete="off">
            </div>
            <div class="row">
                <div class="col p-l-0">
                    <div class="form-group form-group-default required">
                        <label class="muted">City</label>
                        <input class="form-control" name="city" v-model="address.city" @change="changeAddress()" autocomplete="off">
                    </div>
                </div>
                <div class="col p-r-0">
                    <div class="form-group form-group-default required">
                        <label class="muted">State</label>
                        <input class="form-control" name="state" v-model="address.state" @change="changeAddress()" autocomplete="off">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-5 p-l-0">
                    <div class="form-group form-group-default required">
                        <label class="muted">Post Code</label>
                        <input class="form-control" name="post_code" v-model="address.post_code" @change="changeAddress()" autocomplete="off">
                    </div>
                </div>
                <div class="col p-r-0">
                    <div class="form-group form-group-default form-group-default-select2 required">
                        <label class="muted">Country</label>
                        <select-component v-on:selectChange="changeAddress()" :options="countriesList.countries" v-model="address.country" :value="address.country"></select-component>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        inject: ['countriesList'],
        props: {
            'data': {
                type: Object,
                default: null
            }
        },
        data() {
			return {
				address : {
					street_one: '',
					street_two: '',
					city: '',
					state: '',
					post_code: '',
					country: ''
				}

			}
		},
		created() {
            if(this.data !== null){
                this.address = this.data;
            }

        },
		mounted() {
            Event.$on('resetAddress', () => {
				this.address.street_one = '';
				this.address.street_two = '';
				this.address.city = '';
				this.address.state = '';
				this.address.post_code = '';
				this.address.country = '';
            });
        },
        methods: {
            resetAddress(){
                this.address.street_one = '';
                this.address.street_two = '';
                this.address.city = '';
                this.address.state = '';
                this.address.post_code = '';
                this.address.country = '';
            },
            changeAddress() {
                this.$emit('changeAddress', this.address)
            }
        }
    }
</script>
