<template>
  <div class="search-bar">
    <form action="" @submit.prevent="onSearch">
      <input
        type="search"
        class="search-input"
        :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
    }
  },
  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;
}
.search-input {
  display: block;
  width: 100%;
  height: 32px;
  line-height: 32px;
  background: url('~@/assets/img/icon_search.png') no-repeat 22px center;
  background-size: 16px;
  padding-left: 50px;
  border: 1px solid #f1f1f1;
  border-radius: 16px;
}
</style>
