<template>
  <div class="search-bar">
    <form action @submit.prevent="onSearch">
      <input
        type="search"
        class="search-input"
        :class="classes"
        :placeholder="placeholder"
        v-model="currentValue"
        v-bind="$attrs"
        @input="onChange"
        ref="formInput"
      />
    </form>
  </div>
</template>

<script>
export default {
  props: {
    value: String,
    focus: { type: Boolean, default: false },
    placeholder: { type: String, default: '' }
  },
  data() {
    return {
      currentValue: this.value,
      debounced: null
    }
  },
  watch: {
    value(value) {
      this.currentValue = value
    }
  },
  computed: {
    formInput() {
      return this.$refs.formInput
    },
    classes() {
      return {
        'is-focus': this.currentValue
      }
    }
  },
  methods: {
    bindFocus() {
      this.formInput.focus()
    },
    onChange() {
      this.$emit('input', this.currentValue)
      this.$emit('change', this.currentValue)
    },
    onSearch() {
      this.$emit('search', this.currentValue)
    }
  },
  mounted() {
    this.bindFocus()
  }
}
</script>

<style lang="scss">
.search-bar {
  padding: 10px 0 15px;
}
.search-input {
  display: block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  background: #f3f4f5 url('~@/assets/img/icon_search.png') no-repeat center center;
  background-size: 15px;
  padding: 0 20px;
  border-radius: 15px;
  &.is-focus {
    background: #f3f4f5;
  }
}
</style>
