<template>
  <div>
    <input
      ref="autocomplete"
      type="text"
      v-model="autocompleteText"
      placeholder="Enter Location"
      class="form-control"
    />
  </div>
</template>

<script>
/* eslint-disable */

export default {
  props: ['searchText', 'hidden', 'index'],
  model: {
      prop: 'hidden',
      event: 'blur'
  },
  data() {
    return {
      autocompleteText: ''
    }
  },
  watch: {
    searchText () {
      this.autocompleteText = this.searchText
    },
    autocompleteText () {
      this.autocompleteText.length ? this.$emit('validSearch', false) : this.$emit('validSearch', true)
    }
  },
  mounted() {
    this.autocompleteText = this.searchText
    this.autocomplete = new google.maps.places.Autocomplete(
      this.$refs.autocomplete,
      { types: ["geocode"] }
    );

    this.autocomplete.addListener("place_changed", () => {
      let place = this.autocomplete.getPlace()
      this.$emit('placeChanged', place, this.index)
      this.$emit('blur', place.formatted_address)
    });
  }
};
</script>

<style lang="scss" scoped>
  input {
    width: 100%;
    border: 1px solid #acb5bd;
    border-radius: 8px;
    height: 30px;
  }
</style>
